1
0
Fork 0
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:
Elian Cordoba 2023-07-14 13:47:18 -03:00 committed by GitHub
parent 5bbada61ad
commit 0223ad72a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 74 additions and 6 deletions

View file

@ -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) {

View file

@ -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",

View 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]

View file

@ -0,0 +1,2 @@
import test from "npm:@denotest/sub-folders/folder_index_js";
console.log(test);

View 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]

View file

@ -0,0 +1,2 @@
import test from "npm:@denotest/sub-folders/folder_no_index";
console.log(test);

View file

@ -0,0 +1 @@
export function add(a, b): number;

View file

@ -0,0 +1,3 @@
export function add(a, b) {
return a + b;
}

View file

@ -0,0 +1 @@
module.exports = 5;

View file

@ -0,0 +1,3 @@
export function getValue() {
return 5;
}

View file

@ -0,0 +1,6 @@
{
"name": "@denotest/sub-folders",
"version": "1.0.0",
"type": "module",
"main": "main.mjs"
}