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

fix(npm): better error is version is specified after subpath #16131

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 14 additions & 1 deletion cli/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,20 @@ pub fn node_resolve_npm_reference(
NodeModuleKind::Esm,
)
.with_context(|| {
format!("Error resolving package config for '{}'.", reference)
let mut msg =
format!("Error resolving package config for '{}'.", reference);

if let Some(sub_path) = &reference.sub_path {
if let Some(at_index) = sub_path.rfind('@') {
let (sub_path, version) = sub_path.split_at(at_index);
msg = format!(
"{} Did you mean to write '{}{}/{}'?",
msg, reference.req, version, sub_path
);
}
}
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved

msg
})?;

let url = ModuleSpecifier::from_file_path(resolved_path).unwrap();
Expand Down
8 changes: 8 additions & 0 deletions cli/tests/integration/npm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ itest!(require_json {
http_server: true,
});

itest!(error_version_after_subpath {
args: "run --unstable -A --quiet npm/error_version_after_subpath/main.js",
output: "npm/error_version_after_subpath/main.out",
envs: env_vars(),
http_server: true,
exit_code: 1,
});

#[test]
fn parallel_downloading() {
let (out, _err) = util::run_and_collect_output_with_args(
Expand Down
1 change: 1 addition & 0 deletions cli/tests/testdata/npm/error_version_after_subpath/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "npm:react-dom/server@18.2.0";
5 changes: 5 additions & 0 deletions cli/tests/testdata/npm/error_version_after_subpath/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Could not resolve 'react-dom/server@18.2.0'.

Caused by:
0: Error resolving package config for 'react-dom/server@18.2.0'. Did you mean to write 'react-dom@18.2.0/server'?
1: [ERR_PACKAGE_PATH_NOT_EXPORTED] Package subpath './server@18.2.0' is not defined by "exports" in [WILDCARD]
21 changes: 17 additions & 4 deletions ext/node/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,36 @@ pub fn err_invalid_package_target(
}

pub fn err_package_path_not_exported(
pkg_path: String,
mut pkg_path: String,
subpath: String,
maybe_referrer: Option<String>,
) -> AnyError {
let mut msg = "[ERR_PACKAGE_PATH_NOT_EXPORTED]".to_string();

#[cfg(windows)]
{
if !pkg_path.ends_with('\\') {
pkg_path.push('\\');
}
}
#[cfg(not(windows))]
{
if !pkg_path.ends_with('/') {
pkg_path.push('/');
}
}

if subpath == "." {
msg = format!(
"{} No \"exports\" main defined in {}package.json",
"{} No \"exports\" main defined in '{}package.json'",
msg, pkg_path
);
} else {
msg = format!("{} Package subpath \'{}\' is not defined by \"exports\" in {}package.json", msg, subpath, pkg_path);
msg = format!("{} Package subpath '{}' is not defined by \"exports\" in '{}package.json'", msg, subpath, pkg_path);
};

if let Some(referrer) = maybe_referrer {
msg = format!("{} imported from {}", msg, referrer);
msg = format!("{} imported from '{}'", msg, referrer);
}

generic_error(msg)
Expand Down