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>
This commit is contained in:
Bartek Iwańczuk 2023-05-02 02:35:33 +02:00 committed by GitHub
parent 000315e75a
commit 2f651b2d64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 135 additions and 112 deletions

View file

@ -91,7 +91,11 @@ impl LocalNpmPackageResolver {
specifier: &ModuleSpecifier,
) -> Result<PathBuf, AnyError> {
match self.maybe_resolve_folder_for_specifier(specifier) {
Some(path) => Ok(path),
// Canonicalize the path so it's not pointing to the symlinked directory
// in `node_modules` directory of the referrer.
Some(path) => {
Ok(deno_core::strip_unc_prefix(self.fs.canonicalize(&path)?))
}
None => bail!("could not find npm package for '{}'", specifier),
}
}