diff --git a/cli/factory.rs b/cli/factory.rs index 42d98e2343..28788093b1 100644 --- a/cli/factory.rs +++ b/cli/factory.rs @@ -684,6 +684,7 @@ impl CliFactory { .unsafely_ignore_certificate_errors() .clone(), unstable: self.options.unstable(), + maybe_package_json_deps: self.options.maybe_package_json_deps(), }) } } diff --git a/cli/standalone/mod.rs b/cli/standalone/mod.rs index d7547f847a..5ab39d7195 100644 --- a/cli/standalone/mod.rs +++ b/cli/standalone/mod.rs @@ -458,6 +458,7 @@ pub async fn run( unsafely_ignore_certificate_errors: metadata .unsafely_ignore_certificate_errors, unstable: metadata.unstable, + maybe_package_json_deps: package_json_deps_provider.deps().cloned(), }, ); diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index edd100d7db..09330a80c2 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -448,6 +448,27 @@ itest!(permissions_outside_package { http_server: true, }); +itest!(run_existing_npm_package { + args: "run --allow-read --node-modules-dir npm:@denotest/bin", + output: "npm/run_existing_npm_package/main.out", + envs: env_vars_for_npm_tests(), + http_server: true, + temp_cwd: true, + cwd: Some("npm/run_existing_npm_package/"), + copy_temp_dir: Some("npm/run_existing_npm_package/"), +}); + +itest!(run_existing_npm_package_with_subpath { + args: + "run --allow-read --node-modules-dir npm:@denotest/bin/cli-esm dev --help", + output: "npm/run_existing_npm_package_with_subpath/main.out", + envs: env_vars_for_npm_tests(), + http_server: true, + temp_cwd: true, + cwd: Some("npm/run_existing_npm_package_with_subpath/"), + copy_temp_dir: Some("npm/run_existing_npm_package_with_subpath/"), +}); + #[test] fn parallel_downloading() { let (out, _err) = util::run_and_collect_output_with_args( diff --git a/cli/tests/testdata/npm/registry/@denotest/bin/0.5.0/package.json b/cli/tests/testdata/npm/registry/@denotest/bin/0.5.0/package.json index caa2ef5388..1b077a52ed 100644 --- a/cli/tests/testdata/npm/registry/@denotest/bin/0.5.0/package.json +++ b/cli/tests/testdata/npm/registry/@denotest/bin/0.5.0/package.json @@ -1,5 +1,5 @@ { - "name": "@deno/bin", + "name": "@denotest/bin", "version": "0.5.0", "bin": "./cli.mjs" } diff --git a/cli/tests/testdata/npm/registry/@denotest/bin/0.6.0/package.json b/cli/tests/testdata/npm/registry/@denotest/bin/0.6.0/package.json index db50464bc1..a9ff2d9463 100644 --- a/cli/tests/testdata/npm/registry/@denotest/bin/0.6.0/package.json +++ b/cli/tests/testdata/npm/registry/@denotest/bin/0.6.0/package.json @@ -1,4 +1,4 @@ { - "name": "@deno/bin", + "name": "@denotest/bin", "version": "0.6.0" } diff --git a/cli/tests/testdata/npm/registry/@denotest/bin/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/bin/1.0.0/package.json index 78a1abff2f..27118c21ab 100644 --- a/cli/tests/testdata/npm/registry/@denotest/bin/1.0.0/package.json +++ b/cli/tests/testdata/npm/registry/@denotest/bin/1.0.0/package.json @@ -1,5 +1,5 @@ { - "name": "@deno/bin", + "name": "@denotest/bin", "version": "1.0.0", "bin": { "cli-esm": "./cli.mjs", diff --git a/cli/tests/testdata/npm/run_existing_npm_package/main.out b/cli/tests/testdata/npm/run_existing_npm_package/main.out new file mode 100644 index 0000000000..baf7b7afd9 --- /dev/null +++ b/cli/tests/testdata/npm/run_existing_npm_package/main.out @@ -0,0 +1,3 @@ +Download http://localhost:4545/npm/registry/@denotest/bin +Download http://localhost:4545/npm/registry/@denotest/bin/0.5.0.tgz +Initialize @denotest/bin@0.5.0 diff --git a/cli/tests/testdata/npm/run_existing_npm_package/package.json b/cli/tests/testdata/npm/run_existing_npm_package/package.json new file mode 100644 index 0000000000..1cd6d749dc --- /dev/null +++ b/cli/tests/testdata/npm/run_existing_npm_package/package.json @@ -0,0 +1,6 @@ +{ + "name": "run-existing-npm-package", + "dependencies": { + "@denotest/bin": "0.5.0" + } +} diff --git a/cli/tests/testdata/npm/run_existing_npm_package_with_subpath/main.out b/cli/tests/testdata/npm/run_existing_npm_package_with_subpath/main.out new file mode 100644 index 0000000000..bec78df941 --- /dev/null +++ b/cli/tests/testdata/npm/run_existing_npm_package_with_subpath/main.out @@ -0,0 +1,5 @@ +Download http://localhost:4545/npm/registry/@denotest/bin +Download http://localhost:4545/npm/registry/@denotest/bin/1.0.0.tgz +Initialize @denotest/bin@1.0.0 +dev +--help diff --git a/cli/tests/testdata/npm/run_existing_npm_package_with_subpath/package.json b/cli/tests/testdata/npm/run_existing_npm_package_with_subpath/package.json new file mode 100644 index 0000000000..e9a7e84ceb --- /dev/null +++ b/cli/tests/testdata/npm/run_existing_npm_package_with_subpath/package.json @@ -0,0 +1,6 @@ +{ + "name": "run-existing-npm-package", + "dependencies": { + "@denotest/bin": "1.0.0" + } +} diff --git a/cli/worker.rs b/cli/worker.rs index 45968afe7d..b29f22e6ef 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -40,7 +40,9 @@ use deno_runtime::worker::WorkerOptions; use deno_runtime::BootstrapOptions; use deno_runtime::WorkerLogLevel; use deno_semver::npm::NpmPackageReqReference; +use deno_semver::package::PackageReqReference; +use crate::args::package_json::PackageJsonDeps; use crate::args::StorageKeyResolver; use crate::errors; use crate::npm::CliNpmResolver; @@ -89,6 +91,7 @@ pub struct CliMainWorkerOptions { pub seed: Option, pub unsafely_ignore_certificate_errors: Option>, pub unstable: bool, + pub maybe_package_json_deps: Option, } struct SharedWorkerState { @@ -356,6 +359,29 @@ impl CliMainWorkerFactory { let (main_module, is_main_cjs) = if let Ok(package_ref) = NpmPackageReqReference::from_specifier(&main_module) { + let package_ref = if package_ref.req().version_req.version_text() == "*" { + // When using the wildcard version, select the same version used in the + // package.json deps in order to prevent adding new dependency version + shared + .options + .maybe_package_json_deps + .as_ref() + .and_then(|deps| { + deps + .values() + .filter_map(|v| v.as_ref().ok()) + .find(|dep| dep.name == package_ref.req().name) + .map(|dep| { + NpmPackageReqReference::new(PackageReqReference { + req: dep.clone(), + sub_path: package_ref.sub_path().map(|s| s.to_string()), + }) + }) + }) + .unwrap_or(package_ref) + } else { + package_ref + }; shared .npm_resolver .add_package_reqs(&[package_ref.req().clone()])