mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(npm): panic on invalid package name (#16123)
This commit is contained in:
parent
a4a628dc6f
commit
8e1b2fca59
5 changed files with 16 additions and 6 deletions
|
@ -81,12 +81,11 @@ impl GraphData {
|
|||
continue;
|
||||
}
|
||||
if specifier.scheme() == "npm" {
|
||||
// the loader enforces npm specifiers are valid, so it's ok to unwrap here
|
||||
let reference =
|
||||
NpmPackageReference::from_specifier(&specifier).unwrap();
|
||||
if let Ok(reference) = NpmPackageReference::from_specifier(&specifier) {
|
||||
self.npm_packages.insert(reference.req);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if let Some(found) = graph.redirects.get(&specifier) {
|
||||
let module_entry = ModuleEntry::Redirect(found.clone());
|
||||
self.modules.insert(specifier.clone(), module_entry);
|
||||
|
|
|
@ -8,6 +8,7 @@ use std::collections::VecDeque;
|
|||
use deno_ast::ModuleSpecifier;
|
||||
use deno_core::anyhow::bail;
|
||||
use deno_core::anyhow::Context;
|
||||
use deno_core::error::generic_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::futures;
|
||||
use deno_core::parking_lot::RwLock;
|
||||
|
@ -52,7 +53,7 @@ impl NpmPackageReference {
|
|||
let parts = specifier.split('/').collect::<Vec<_>>();
|
||||
let name_part_len = if specifier.starts_with('@') { 2 } else { 1 };
|
||||
if parts.len() < name_part_len {
|
||||
bail!("Not a valid package: {}", specifier);
|
||||
return Err(generic_error(format!("Not a valid package: {}", specifier)));
|
||||
}
|
||||
let name_parts = &parts[0..name_part_len];
|
||||
let last_name_part = &name_parts[name_part_len - 1];
|
||||
|
|
|
@ -181,6 +181,13 @@ itest!(nonexistent_file {
|
|||
exit_code: 1,
|
||||
});
|
||||
|
||||
itest!(invalid_package_name {
|
||||
args: "run --unstable -A --quiet npm/invalid_package_name/main.js",
|
||||
output: "npm/invalid_package_name/main.out",
|
||||
envs: env_vars(),
|
||||
exit_code: 1,
|
||||
});
|
||||
|
||||
itest!(require_json {
|
||||
args: "run --unstable -A --quiet npm/require_json/main.js",
|
||||
output: "npm/require_json/main.out",
|
||||
|
|
1
cli/tests/testdata/npm/invalid_package_name/main.js
vendored
Normal file
1
cli/tests/testdata/npm/invalid_package_name/main.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
import * as foo from "npm:@foo";
|
2
cli/tests/testdata/npm/invalid_package_name/main.out
vendored
Normal file
2
cli/tests/testdata/npm/invalid_package_name/main.out
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
error: Not a valid package: @foo
|
||||
at [WILDCARD]/invalid_package_name/main.js:1:22
|
Loading…
Reference in a new issue