summaryrefslogtreecommitdiff
path: root/ext/node
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-05-02 02:35:33 +0200
committerGitHub <noreply@github.com>2023-05-02 02:35:33 +0200
commit2f651b2d64523bdd377d22b8b7213a04ad82f459 (patch)
treef5776da1ee7e25b70bd736bf2428c3fbb06b35a0 /ext/node
parent000315e75a20e82616a227702c98346f2b5e8b59 (diff)
fix(npm): canonicalize filename before returning (#18948)
This commit changes how paths for npm packages are handled, by canonicalizing them when resolving. This is done so that instead of returning "node_modules/<package_name>@<version>/node_modules/<dep>/index.js" (which is a symlink) we "node_modules/<dep>@<dep_version>/index.js. Fixes https://github.com/denoland/deno/issues/18924 Fixes https://github.com/bluwy/create-vite-extra/issues/31 --------- Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'ext/node')
-rw-r--r--ext/node/ops/require.rs10
-rw-r--r--ext/node/polyfills/01_require.js6
2 files changed, 5 insertions, 11 deletions
diff --git a/ext/node/ops/require.rs b/ext/node/ops/require.rs
index 34eac8475..1c8647bab 100644
--- a/ext/node/ops/require.rs
+++ b/ext/node/ops/require.rs
@@ -287,15 +287,7 @@ where
let path = PathBuf::from(request);
ensure_read_permission::<Env::P>(state, &path)?;
let fs = state.borrow::<Arc<dyn NodeFs>>();
- let mut canonicalized_path = fs.canonicalize(&path)?;
- if cfg!(windows) {
- canonicalized_path = PathBuf::from(
- canonicalized_path
- .display()
- .to_string()
- .trim_start_matches("\\\\?\\"),
- );
- }
+ let canonicalized_path = deno_core::strip_unc_prefix(fs.canonicalize(&path)?);
Ok(canonicalized_path.to_string_lossy().to_string())
}
diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js
index 8fbe5078c..ce7312ee8 100644
--- a/ext/node/polyfills/01_require.js
+++ b/ext/node/polyfills/01_require.js
@@ -861,9 +861,11 @@ Module.prototype.load = function (filename) {
throw Error("Module already loaded");
}
- this.filename = filename;
+ // Canonicalize the path so it's not pointing to the symlinked directory
+ // in `node_modules` directory of the referrer.
+ this.filename = ops.op_require_real_path(filename);
this.paths = Module._nodeModulePaths(
- pathDirname(filename),
+ pathDirname(this.filename),
);
const extension = findLongestRegisteredExtension(filename);
// allow .mjs to be overriden