summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2023-11-07 09:56:06 -0500
committerGitHub <noreply@github.com>2023-11-07 09:56:06 -0500
commit9201198efd6fb116585d4c26111669f4c1006e5d (patch)
tree746b2283da7edb457cea5eced2bc6cd7b42b8067 /cli
parent50e4806a2db66be289aa123a0bfd8dd8688712ba (diff)
fix(node): inspect ancestor directories when resolving cjs re-exports during analysis (#21104)
If a CJS re-export can't be resolved, it will check the ancestor directories, which is more similar to what `require` does at runtime.
Diffstat (limited to 'cli')
-rw-r--r--cli/args/mod.rs2
-rw-r--r--cli/tests/integration/npm_tests.rs67
2 files changed, 68 insertions, 1 deletions
diff --git a/cli/args/mod.rs b/cli/args/mod.rs
index 4acbb1763..9c113acd2 100644
--- a/cli/args/mod.rs
+++ b/cli/args/mod.rs
@@ -924,7 +924,7 @@ impl CliOptions {
}
pub fn has_node_modules_dir(&self) -> bool {
- self.maybe_node_modules_folder.is_some()
+ self.maybe_node_modules_folder.is_some() || self.unstable_byonm()
}
pub fn node_modules_dir_path(&self) -> Option<PathBuf> {
diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs
index 92af166e0..006180843 100644
--- a/cli/tests/integration/npm_tests.rs
+++ b/cli/tests/integration/npm_tests.rs
@@ -2483,6 +2483,73 @@ console.log(add(1, 2));
output.assert_matches_text("Check file:///[WILDCARD]/project-b/main.ts\n");
}
+#[test]
+pub fn byonm_cjs_export_analysis_require_re_export() {
+ let test_context = TestContextBuilder::for_npm().use_temp_cwd().build();
+ let dir = test_context.temp_dir();
+ dir.write(
+ "deno.json",
+ r#"{
+ "unstable": [
+ "byonm"
+ ]
+ }"#,
+ );
+
+ dir.write(
+ "package.json",
+ r#"{
+ "name": "test",
+ "packages": {
+ "my-package": "1.0.0"
+ }
+}
+"#,
+ );
+ dir.write(
+ "main.js",
+ "import { value1, value2 } from 'my-package';\nconsole.log(value1);\nconsole.log(value2)\n",
+ );
+
+ let node_modules_dir = dir.path().join("node_modules");
+
+ // create a package at node_modules/.multipart/name/nested without a package.json
+ {
+ let pkg_dir = node_modules_dir
+ .join(".multipart")
+ .join("name")
+ .join("nested");
+ pkg_dir.create_dir_all();
+ pkg_dir.join("index.js").write("module.exports.value1 = 5;");
+ }
+ // create a package at node_modules/.multipart/other with a package.json
+ {
+ let pkg_dir = node_modules_dir.join(".multipart").join("other");
+ pkg_dir.create_dir_all();
+ pkg_dir.join("index.js").write("module.exports.value2 = 6;");
+ }
+ // create a package at node_modules/my-package that requires them both
+ {
+ let pkg_dir = node_modules_dir.join("my-package");
+ pkg_dir.create_dir_all();
+ pkg_dir.join("package.json").write_json(&json!({
+ "name": "my-package",
+ "version": "1.0.0",
+ }));
+ pkg_dir
+ .join("index.js")
+ .write("module.exports = { ...require('.multipart/name/nested/index'), ...require('.multipart/other/index.js') }");
+ }
+
+ // the cjs export analysis was preivously failing, but it should
+ // resolve these exports similar to require
+ let output = test_context
+ .new_command()
+ .args("run --allow-read main.js")
+ .run();
+ output.assert_matches_text("5\n6\n");
+}
+
itest!(imports_package_json {
args: "run --node-modules-dir=false npm/imports_package_json/main.js",
output: "npm/imports_package_json/main.out",