From e95d5cc7dcc52bc49703d4e2ba04d3962436fe8b Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 16 Nov 2023 17:29:35 -0500 Subject: [PATCH] fix(npm): support cjs entrypoint in node_modules folder (#21224) Closes #21109 --- cli/tests/integration/npm_tests.rs | 21 +++++++++++++++++++++ cli/tools/run/mod.rs | 4 ++-- cli/worker.rs | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index 4f03e4e809c296..95ffd71cd8bef4 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -2653,3 +2653,24 @@ pub fn different_nested_dep_byonm() { .run(); output.assert_matches_file("npm/different_nested_dep/main.out"); } + +#[test] +pub fn run_cjs_in_node_modules_folder() { + let test_context = TestContextBuilder::for_npm().use_temp_cwd().build(); + let temp_dir = test_context.temp_dir(); + temp_dir.write("package.json", "{}"); + temp_dir.write("deno.json", r#"{ "unstable": ["byonm"] }"#); + let pkg_dir = temp_dir.path().join("node_modules/package"); + pkg_dir.create_dir_all(); + pkg_dir + .join("package.json") + .write(r#"{ "name": "package" }"#); + pkg_dir + .join("main.js") + .write("console.log('hi'); module.exports = 'hi';"); + test_context + .new_command() + .args("run node_modules/package/main.js") + .run() + .assert_matches_text("hi\n"); +} diff --git a/cli/tools/run/mod.rs b/cli/tools/run/mod.rs index 119129b1bab559..21e1f7b5e4c163 100644 --- a/cli/tools/run/mod.rs +++ b/cli/tools/run/mod.rs @@ -45,8 +45,8 @@ To grant permissions, set them before the script argument. For example: let http_client = factory.http_client(); let cli_options = factory.cli_options(); - // Run a background task that checks for available upgrades. If an earlier - // run of this background task found a new version of Deno. + // Run a background task that checks for available upgrades or output + // if an earlier run of this background task found a new version of Deno. super::upgrade::check_for_upgrades( http_client.clone(), deno_dir.upgrade_check_file_path(), diff --git a/cli/worker.rs b/cli/worker.rs index ed3db5321b5d47..8834f59ce54469 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -495,7 +495,9 @@ impl CliMainWorkerFactory { } (node_resolution.into_url(), is_main_cjs) - } else if shared.options.is_npm_main { + } else if shared.options.is_npm_main + || shared.node_resolver.in_npm_package(&main_module) + { let node_resolution = shared.node_resolver.url_to_node_resolution(main_module)?; let is_main_cjs = matches!(node_resolution, NodeResolution::CommonJs(_));