From c58a628e2ff449f8cbbdcd6bb8baeaba1ea95a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 4 Sep 2024 13:55:30 +0100 Subject: feat(add): strip package subpath when adding a package (#25419) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These now works: ``` $ deno add @std/dotenv/load $ deno add npm:preact/hooks ``` Previously we were erroring out, because this is a "package reference" including a subpath. Closes https://github.com/denoland/deno/issues/25385 --------- Signed-off-by: Bartek IwaƄczuk Co-authored-by: David Sherret --- cli/tools/registry/pm.rs | 10 ++++++++-- tests/specs/add/add_with_subpath/__test__.jsonc | 19 +++++++++++++++++++ tests/specs/add/add_with_subpath/add.out | 8 ++++++++ tests/specs/add/add_with_subpath/deno.json | 0 .../add/add_with_subpath/wrong_constraint_jsr.out | 4 ++++ .../add/add_with_subpath/wrong_constraint_npm.out | 4 ++++ 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/specs/add/add_with_subpath/__test__.jsonc create mode 100644 tests/specs/add/add_with_subpath/add.out create mode 100644 tests/specs/add/add_with_subpath/deno.json create mode 100644 tests/specs/add/add_with_subpath/wrong_constraint_jsr.out create mode 100644 tests/specs/add/add_with_subpath/wrong_constraint_npm.out diff --git a/cli/tools/registry/pm.rs b/cli/tools/registry/pm.rs index 4a36b459a..e61da31d5 100644 --- a/cli/tools/registry/pm.rs +++ b/cli/tools/registry/pm.rs @@ -3,6 +3,8 @@ mod cache_deps; pub use cache_deps::cache_top_level_deps; +use deno_semver::jsr::JsrPackageReqReference; +use deno_semver::npm::NpmPackageReqReference; use std::borrow::Cow; use std::path::Path; @@ -501,14 +503,18 @@ impl AddPackageReq { match prefix { Prefix::Jsr => { - let package_req = PackageReq::from_str(entry_text)?; + let req_ref = + JsrPackageReqReference::from_str(&format!("jsr:{}", entry_text))?; + let package_req = req_ref.into_inner().req; Ok(AddPackageReq { alias: maybe_alias.unwrap_or_else(|| package_req.name.to_string()), value: AddPackageReqValue::Jsr(package_req), }) } Prefix::Npm => { - let package_req = PackageReq::from_str(entry_text)?; + let req_ref = + NpmPackageReqReference::from_str(&format!("npm:{}", entry_text))?; + let package_req = req_ref.into_inner().req; Ok(AddPackageReq { alias: maybe_alias.unwrap_or_else(|| package_req.name.to_string()), value: AddPackageReqValue::Npm(package_req), diff --git a/tests/specs/add/add_with_subpath/__test__.jsonc b/tests/specs/add/add_with_subpath/__test__.jsonc new file mode 100644 index 000000000..b051bd265 --- /dev/null +++ b/tests/specs/add/add_with_subpath/__test__.jsonc @@ -0,0 +1,19 @@ +{ + "tempDir": true, + "steps": [ + { + "args": "add @std/testing/bdd npm:preact/hooks", + "output": "add.out" + }, + { + "args": "add @std/testing/bdd@1 npm:preact/hooks@10", + "output": "wrong_constraint_jsr.out", + "exitCode": 1 + }, + { + "args": "add npm:preact/hooks@10", + "output": "wrong_constraint_npm.out", + "exitCode": 1 + } + ] +} diff --git a/tests/specs/add/add_with_subpath/add.out b/tests/specs/add/add_with_subpath/add.out new file mode 100644 index 000000000..02b286ba3 --- /dev/null +++ b/tests/specs/add/add_with_subpath/add.out @@ -0,0 +1,8 @@ +[UNORDERED_START] +Add jsr:@std/testing@1.0.0 +Add npm:preact@10.19.6 +Download http://127.0.0.1:4250/@std/testing/1.0.0/bdd.ts +Download http://127.0.0.1:4250/@std/testing/1.0.0/types.ts +Download http://localhost:4260/preact +Download http://localhost:4260/preact/preact-10.19.6.tgz +[UNORDERED_END] diff --git a/tests/specs/add/add_with_subpath/deno.json b/tests/specs/add/add_with_subpath/deno.json new file mode 100644 index 000000000..e69de29bb diff --git a/tests/specs/add/add_with_subpath/wrong_constraint_jsr.out b/tests/specs/add/add_with_subpath/wrong_constraint_jsr.out new file mode 100644 index 000000000..2b218407d --- /dev/null +++ b/tests/specs/add/add_with_subpath/wrong_constraint_jsr.out @@ -0,0 +1,4 @@ +error: Failed to parse package required: @std/testing/bdd@1 + +Caused by: + Invalid package specifier 'jsr:@std/testing/bdd@1'. Did you mean to write 'jsr:@std/testing@1/bdd'? diff --git a/tests/specs/add/add_with_subpath/wrong_constraint_npm.out b/tests/specs/add/add_with_subpath/wrong_constraint_npm.out new file mode 100644 index 000000000..4adcf9ef6 --- /dev/null +++ b/tests/specs/add/add_with_subpath/wrong_constraint_npm.out @@ -0,0 +1,4 @@ +error: Failed to parse package required: npm:preact/hooks@10 + +Caused by: + Invalid package specifier 'npm:preact/hooks@10'. Did you mean to write 'npm:preact@10/hooks'? -- cgit v1.2.3