Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dx: better error messages for 'relative import path not prefixed with / or ./ or ../.' #3405

Merged
merged 2 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/deno_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl GetErrorKind for ModuleResolutionError {
match self {
InvalidUrl(ref err) | InvalidBaseUrl(ref err) => err.kind(),
InvalidPath(_) => ErrorKind::InvalidPath,
ImportPrefixMissing(_) => ErrorKind::ImportPrefixMissing,
ImportPrefixMissing(_, _) => ErrorKind::ImportPrefixMissing,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/error_011_bad_module_specifier.ts.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "bad-module.ts" not prefixed with / or ./ or ../
[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_011_bad_module_specifier.ts"
[WILDCARD]dispatch_json.ts:[WILDCARD]
at DenoError ([WILDCARD]errors.ts:[WILDCARD])
at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD])
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/error_012_bad_dynamic_import_specifier.ts.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "bad-module.ts" not prefixed with / or ./ or ../
[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_012_bad_dynamic_import_specifier.ts"
[WILDCARD]dispatch_json.ts:[WILDCARD]
at DenoError ([WILDCARD]errors.ts:[WILDCARD])
at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD])
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/error_014_catch_dynamic_import_error.js.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Caught direct dynamic import error.
TypeError: relative import path "does not exist" not prefixed with / or ./ or ../
TypeError: relative import path "does not exist" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_014_catch_dynamic_import_error.js"

Caught indirect direct dynamic import error.
TypeError: relative import path "does not exist either" not prefixed with / or ./ or ../
TypeError: relative import path "does not exist either" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/indirect_import_error.js"

Caught error thrown by dynamically imported module.
Error: An error
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/error_type_definitions.ts.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "baz" not prefixed with / or ./ or ../
[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "baz" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/type_definitions/bar.d.ts"
[WILDCARD]dispatch_json.ts:[WILDCARD]
at DenoError ([WILDCARD]errors.ts:[WILDCARD])
at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD])
Expand Down
52 changes: 40 additions & 12 deletions core/module_specifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum ModuleResolutionError {
InvalidUrl(ParseError),
InvalidBaseUrl(ParseError),
InvalidPath(PathBuf),
ImportPrefixMissing(String),
ImportPrefixMissing(String, Option<String>),
}
use ModuleResolutionError::*;

Expand All @@ -32,11 +32,19 @@ impl fmt::Display for ModuleResolutionError {
write!(f, "invalid base URL for relative import: {}", err)
}
InvalidPath(ref path) => write!(f, "invalid module path: {:?}", path),
ImportPrefixMissing(ref specifier) => write!(
f,
"relative import path \"{}\" not prefixed with / or ./ or ../",
specifier
),
ImportPrefixMissing(ref specifier, ref maybe_referrer) => {
let msg = format!(
"relative import path \"{}\" not prefixed with / or ./ or ../",
specifier
);
let msg = if let Some(referrer) = maybe_referrer {
format!("{} Imported from \"{}\"", msg, referrer)
} else {
msg
};

write!(f, "{}", msg)
}
}
}
}
Expand Down Expand Up @@ -78,7 +86,12 @@ impl ModuleSpecifier {
|| specifier.starts_with("./")
|| specifier.starts_with("../")) =>
{
return Err(ImportPrefixMissing(specifier.to_string()))
let maybe_referrer = if base.is_empty() {
None
} else {
Some(base.to_string())
};
return Err(ImportPrefixMissing(specifier.to_string(), maybe_referrer));
}

// 3. Return the result of applying the URL parser to specifier with base
Expand Down Expand Up @@ -282,27 +295,42 @@ mod tests {
(
"005_more_imports.ts",
"https://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing("005_more_imports.ts".to_string()),
ImportPrefixMissing(
"005_more_imports.ts".to_string(),
Some("https://deno.land/core/tests/006_url_imports.ts".to_string()),
),
),
(
".tomato",
"https://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing(".tomato".to_string()),
ImportPrefixMissing(
".tomato".to_string(),
Some("https://deno.land/core/tests/006_url_imports.ts".to_string()),
),
),
(
"..zucchini.mjs",
"https://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing("..zucchini.mjs".to_string()),
ImportPrefixMissing(
"..zucchini.mjs".to_string(),
Some("https://deno.land/core/tests/006_url_imports.ts".to_string()),
),
),
(
r".\yam.es",
"https://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing(r".\yam.es".to_string()),
ImportPrefixMissing(
r".\yam.es".to_string(),
Some("https://deno.land/core/tests/006_url_imports.ts".to_string()),
),
),
(
r"..\yam.es",
"https://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing(r"..\yam.es".to_string()),
ImportPrefixMissing(
r"..\yam.es".to_string(),
Some("https://deno.land/core/tests/006_url_imports.ts".to_string()),
),
),
(
"https://eggplant:b/c",
Expand Down