From 33ba31b390fba30e5e165dc465bc2a10b3f36f64 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 14 Jul 2023 13:47:15 -0400 Subject: [PATCH] fix(npm): improve error message importing non-existent file in a node_modules npm package (#19835) --- cli/npm/resolvers/local.rs | 11 ++++++++--- cli/tests/integration/npm_tests.rs | 10 ++++++++++ cli/tests/testdata/npm/nonexistent_file/main.js | 3 ++- cli/tests/testdata/npm/nonexistent_file/main.out | 2 +- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cli/npm/resolvers/local.rs b/cli/npm/resolvers/local.rs index e94a910860..42cd0cc9ea 100644 --- a/cli/npm/resolvers/local.rs +++ b/cli/npm/resolvers/local.rs @@ -14,6 +14,7 @@ use std::sync::Arc; use crate::cache::CACHE_PERM; use crate::npm::cache::mixed_case_package_name_decode; use crate::util::fs::atomic_write_file; +use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs; use crate::util::fs::symlink_dir; use crate::util::fs::LaxSingleProcessFsFlag; use crate::util::progress_bar::ProgressBar; @@ -112,9 +113,13 @@ impl LocalNpmPackageResolver { match self.maybe_resolve_folder_for_specifier(specifier) { // 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.realpath_sync(&path)?)) - } + Some(path) => canonicalize_path_maybe_not_exists_with_fs(&path, |path| { + self + .fs + .realpath_sync(path) + .map_err(|err| err.into_io_error()) + }) + .map_err(|err| err.into()), None => bail!("could not find npm package for '{}'", specifier), } } diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index 6da402a385..82f73be081 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -286,6 +286,16 @@ itest!(nonexistent_file { exit_code: 1, }); +itest!(nonexistent_file_node_modules_dir { + // there was a bug where the message was different when using a node_modules dir + args: "run -A --quiet --node-modules-dir npm/nonexistent_file/main.js", + output: "npm/nonexistent_file/main.out", + copy_temp_dir: Some("npm/nonexistent_file/"), + envs: env_vars_for_npm_tests(), + http_server: true, + exit_code: 1, +}); + itest!(invalid_package_name { args: "run -A --quiet npm/invalid_package_name/main.js", output: "npm/invalid_package_name/main.out", diff --git a/cli/tests/testdata/npm/nonexistent_file/main.js b/cli/tests/testdata/npm/nonexistent_file/main.js index 4e46570882..c480b0548f 100644 --- a/cli/tests/testdata/npm/nonexistent_file/main.js +++ b/cli/tests/testdata/npm/nonexistent_file/main.js @@ -1 +1,2 @@ -import hmacSHA512 from "npm:crypto-js/hmac-sha512"; +import hmacSHA512 from "npm:crypto-js/non-existent"; +console.log(hmacSHA512); diff --git a/cli/tests/testdata/npm/nonexistent_file/main.out b/cli/tests/testdata/npm/nonexistent_file/main.out index 5f43f7242d..baa79b1cef 100644 --- a/cli/tests/testdata/npm/nonexistent_file/main.out +++ b/cli/tests/testdata/npm/nonexistent_file/main.out @@ -1,4 +1,4 @@ -error: Unable to load [WILDCARD]hmac-sha512 imported from [WILDCARD]/testdata/npm/nonexistent_file/main.js +error: Unable to load [WILDCARD]non-existent imported from [WILDCARD]main.js Caused by: [WILDCARD]