mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(npm): improve error message on directory import in npm package (#19538)
Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
This commit is contained in:
parent
5bbada61ad
commit
0223ad72a9
11 changed files with 74 additions and 6 deletions
|
@ -748,13 +748,34 @@ impl NpmModuleLoader {
|
|||
.read_to_string(&file_path)
|
||||
.map_err(AnyError::from)
|
||||
.with_context(|| {
|
||||
let mut msg = "Unable to load ".to_string();
|
||||
msg.push_str(&file_path.to_string_lossy());
|
||||
if let Some(referrer) = &maybe_referrer {
|
||||
msg.push_str(" imported from ");
|
||||
msg.push_str(referrer.as_str());
|
||||
if file_path.is_dir() {
|
||||
// directory imports are not allowed when importing from an
|
||||
// ES module, so provide the user with a helpful error message
|
||||
let dir_path = file_path;
|
||||
let mut msg = "Directory import ".to_string();
|
||||
msg.push_str(&dir_path.to_string_lossy());
|
||||
if let Some(referrer) = &maybe_referrer {
|
||||
msg.push_str(" is not supported resolving import from ");
|
||||
msg.push_str(referrer.as_str());
|
||||
let entrypoint_name = ["index.mjs", "index.js", "index.cjs"]
|
||||
.iter()
|
||||
.find(|e| dir_path.join(e).is_file());
|
||||
if let Some(entrypoint_name) = entrypoint_name {
|
||||
msg.push_str("\nDid you mean to import ");
|
||||
msg.push_str(entrypoint_name);
|
||||
msg.push_str(" within the directory?");
|
||||
}
|
||||
}
|
||||
msg
|
||||
} else {
|
||||
let mut msg = "Unable to load ".to_string();
|
||||
msg.push_str(&file_path.to_string_lossy());
|
||||
if let Some(referrer) = &maybe_referrer {
|
||||
msg.push_str(" imported from ");
|
||||
msg.push_str(referrer.as_str());
|
||||
}
|
||||
msg
|
||||
}
|
||||
msg
|
||||
})?;
|
||||
|
||||
let code = if self.cjs_resolutions.contains(specifier) {
|
||||
|
|
|
@ -778,6 +778,22 @@ itest!(deno_run_non_existent {
|
|||
exit_code: 1,
|
||||
});
|
||||
|
||||
itest!(directory_import_folder_index_js {
|
||||
args: "run npm/directory_import/folder_index_js.ts",
|
||||
output: "npm/directory_import/folder_index_js.out",
|
||||
envs: env_vars_for_npm_tests(),
|
||||
http_server: true,
|
||||
exit_code: 1,
|
||||
});
|
||||
|
||||
itest!(directory_import_folder_no_index {
|
||||
args: "run npm/directory_import/folder_no_index.ts",
|
||||
output: "npm/directory_import/folder_no_index.out",
|
||||
envs: env_vars_for_npm_tests(),
|
||||
http_server: true,
|
||||
exit_code: 1,
|
||||
});
|
||||
|
||||
itest!(builtin_module_module {
|
||||
args: "run --allow-read --quiet npm/builtin_module_module/main.js",
|
||||
output: "npm/builtin_module_module/main.out",
|
||||
|
|
7
cli/tests/testdata/npm/directory_import/folder_index_js.out
vendored
Normal file
7
cli/tests/testdata/npm/directory_import/folder_index_js.out
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
Download http://localhost:4545/npm/registry/@denotest/sub-folders
|
||||
Download http://localhost:4545/npm/registry/@denotest/sub-folders/1.0.0.tgz
|
||||
error: Directory import [WILDCARD]folder_index_js is not supported resolving import from file:///[WILDCARD]/directory_import/folder_index_js.ts
|
||||
Did you mean to import index.js within the directory?
|
||||
|
||||
Caused by:
|
||||
[WILDCARD]
|
2
cli/tests/testdata/npm/directory_import/folder_index_js.ts
vendored
Normal file
2
cli/tests/testdata/npm/directory_import/folder_index_js.ts
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
import test from "npm:@denotest/sub-folders/folder_index_js";
|
||||
console.log(test);
|
6
cli/tests/testdata/npm/directory_import/folder_no_index.out
vendored
Normal file
6
cli/tests/testdata/npm/directory_import/folder_no_index.out
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
Download http://localhost:4545/npm/registry/@denotest/sub-folders
|
||||
Download http://localhost:4545/npm/registry/@denotest/sub-folders/1.0.0.tgz
|
||||
error: Directory import [WILDCARD]folder_no_index is not supported resolving import from file:///[WILDCARD]/folder_no_index.ts
|
||||
|
||||
Caused by:
|
||||
[WILDCARD]
|
2
cli/tests/testdata/npm/directory_import/folder_no_index.ts
vendored
Normal file
2
cli/tests/testdata/npm/directory_import/folder_no_index.ts
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
import test from "npm:@denotest/sub-folders/folder_no_index";
|
||||
console.log(test);
|
1
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/folder_index_js/index.d.ts
vendored
Normal file
1
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/folder_index_js/index.d.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
export function add(a, b): number;
|
3
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/folder_index_js/index.js
vendored
Normal file
3
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/folder_index_js/index.js
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
export function add(a, b) {
|
||||
return a + b;
|
||||
}
|
1
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/folder_no_index/random_name.js
vendored
Normal file
1
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/folder_no_index/random_name.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
module.exports = 5;
|
3
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/main.mjs
vendored
Normal file
3
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/main.mjs
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
export function getValue() {
|
||||
return 5;
|
||||
}
|
6
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/package.json
vendored
Normal file
6
cli/tests/testdata/npm/registry/@denotest/sub-folders/1.0.0/package.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name": "@denotest/sub-folders",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"main": "main.mjs"
|
||||
}
|
Loading…
Reference in a new issue