From 681597f8661b2acfa418575e062f42e4c891c290 Mon Sep 17 00:00:00 2001 From: Pyrode Date: Sat, 3 May 2025 22:19:22 +0530 Subject: [PATCH 1/8] test(install): Added test case for prefixed `v` in version --- tests/testsuite/install.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index ac181b6fc11..47360d0be45 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2899,3 +2899,17 @@ fn dry_run_remove_orphan() { // Ensure server is still installed after the dry run assert_has_installed_exe(paths::cargo_home(), "server"); } + +#[cargo_test] +fn prefixed_v_in_version() { + pkg("foo", "0.0.1"); + cargo_process("install foo@v0.0.1") + .with_status(1) + .with_stderr_data(str![[r#" +[ERROR] invalid value 'foo@v0.0.1' for '[CRATE[@]]...': unexpected character 'v' while parsing major version number + +For more information, try '--help'. + +"#]]) + .run(); +} From 7e0995103971d3ed66aba45a2cfba4a4f6b23573 Mon Sep 17 00:00:00 2001 From: Pyrode Date: Sat, 3 May 2025 22:20:38 +0530 Subject: [PATCH 2/8] install: Added error message for when `v` is prefixed with version --- src/bin/cargo/commands/install.rs | 6 ++++++ tests/testsuite/install.rs | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 5fefb7adfff..0f97071e69a 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -286,6 +286,12 @@ fn parse_semver_flag(v: &str) -> CargoResult { .next() .ok_or_else(|| format_err!("no version provided for the `--version` flag"))?; + if let Some(stripped) = v.strip_prefix("v") { + bail!( + "the version provided, `{v}` is not a valid SemVer requirement\n\n\ + help: try changing the version to `{stripped}`", + ) + } let is_req = "<>=^~".contains(first) || v.contains('*'); if is_req { match v.parse::() { diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 47360d0be45..dfff7a680c8 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2906,7 +2906,9 @@ fn prefixed_v_in_version() { cargo_process("install foo@v0.0.1") .with_status(1) .with_stderr_data(str![[r#" -[ERROR] invalid value 'foo@v0.0.1' for '[CRATE[@]]...': unexpected character 'v' while parsing major version number +[ERROR] invalid value 'foo@v0.0.1' for '[CRATE[@]]...': the version provided, `v0.0.1` is not a valid SemVer requirement + +[HELP] try changing the version to `0.0.1` For more information, try '--help'. From ac86dea366de5c3f06a40fa0ac53a6458c070988 Mon Sep 17 00:00:00 2001 From: Pyrode Date: Sat, 3 May 2025 22:22:06 +0530 Subject: [PATCH 3/8] test(add): Added test case for prefixed `v` in version --- tests/testsuite/cargo_add/mod.rs | 1 + .../cargo_add/prefixed_v_in_version/in | 1 + .../cargo_add/prefixed_v_in_version/mod.rs | 26 +++++++++++++++ .../prefixed_v_in_version/out/Cargo.toml | 6 ++++ .../prefixed_v_in_version/stderr.term.svg | 33 +++++++++++++++++++ 5 files changed, 67 insertions(+) create mode 120000 tests/testsuite/cargo_add/prefixed_v_in_version/in create mode 100644 tests/testsuite/cargo_add/prefixed_v_in_version/mod.rs create mode 100644 tests/testsuite/cargo_add/prefixed_v_in_version/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/prefixed_v_in_version/stderr.term.svg diff --git a/tests/testsuite/cargo_add/mod.rs b/tests/testsuite/cargo_add/mod.rs index 050baa8e16f..1f1d3c0a475 100644 --- a/tests/testsuite/cargo_add/mod.rs +++ b/tests/testsuite/cargo_add/mod.rs @@ -126,6 +126,7 @@ mod path_base_unstable; mod path_dev; mod path_inferred_name; mod path_inferred_name_conflicts_full_feature; +mod prefixed_v_in_version; mod preserve_dep_std_table; mod preserve_features_sorted; mod preserve_features_table; diff --git a/tests/testsuite/cargo_add/prefixed_v_in_version/in b/tests/testsuite/cargo_add/prefixed_v_in_version/in new file mode 120000 index 00000000000..6c6a27fcfb5 --- /dev/null +++ b/tests/testsuite/cargo_add/prefixed_v_in_version/in @@ -0,0 +1 @@ +../add-basic.in \ No newline at end of file diff --git a/tests/testsuite/cargo_add/prefixed_v_in_version/mod.rs b/tests/testsuite/cargo_add/prefixed_v_in_version/mod.rs new file mode 100644 index 00000000000..e36b65242b8 --- /dev/null +++ b/tests/testsuite/cargo_add/prefixed_v_in_version/mod.rs @@ -0,0 +1,26 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::current_dir; +use cargo_test_support::file; +use cargo_test_support::prelude::*; +use cargo_test_support::str; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + + let project = Project::from_template(current_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("add") + .arg_line("foo@v0.0.1") + .current_dir(cwd) + .assert() + .code(101) + .stdout_eq(str![""]) + .stderr_eq(file!["stderr.term.svg"]); + + assert_ui().subset_matches(current_dir!().join("out"), &project_root); +} diff --git a/tests/testsuite/cargo_add/prefixed_v_in_version/out/Cargo.toml b/tests/testsuite/cargo_add/prefixed_v_in_version/out/Cargo.toml new file mode 100644 index 00000000000..946b7c86bf0 --- /dev/null +++ b/tests/testsuite/cargo_add/prefixed_v_in_version/out/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" +edition = "2015" diff --git a/tests/testsuite/cargo_add/prefixed_v_in_version/stderr.term.svg b/tests/testsuite/cargo_add/prefixed_v_in_version/stderr.term.svg new file mode 100644 index 00000000000..2a6050f6ae4 --- /dev/null +++ b/tests/testsuite/cargo_add/prefixed_v_in_version/stderr.term.svg @@ -0,0 +1,33 @@ + + + + + + + error: invalid version requirement `v0.0.1` + + + + Caused by: + + unexpected character 'v' while parsing major version number + + + + + + From 9ae361a8fed9db27c8446b440f2034e304b0f34e Mon Sep 17 00:00:00 2001 From: Pyrode Date: Sat, 3 May 2025 22:25:12 +0530 Subject: [PATCH 4/8] add: Added error message for when `v` is prefixed with version --- src/cargo/ops/cargo_add/crate_spec.rs | 12 ++++++++++-- .../prefixed_v_in_version/stderr.term.svg | 14 +++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/cargo/ops/cargo_add/crate_spec.rs b/src/cargo/ops/cargo_add/crate_spec.rs index 47959b464b0..9fb48fdb443 100644 --- a/src/cargo/ops/cargo_add/crate_spec.rs +++ b/src/cargo/ops/cargo_add/crate_spec.rs @@ -47,8 +47,16 @@ impl CrateSpec { package_name?; if let Some(version) = version { - semver::VersionReq::parse(version) - .with_context(|| format!("invalid version requirement `{version}`"))?; + semver::VersionReq::parse(version).with_context(|| { + if let Some(stripped) = version.strip_prefix("v") { + return format!( + "the version provided, `{version}` is not a \ + valid SemVer requirement\n\n\ + help: changing the package to `{name}@{stripped}`", + ); + } + format!("invalid version requirement `{version}`") + })?; } let id = Self { diff --git a/tests/testsuite/cargo_add/prefixed_v_in_version/stderr.term.svg b/tests/testsuite/cargo_add/prefixed_v_in_version/stderr.term.svg index 2a6050f6ae4..86e034ff602 100644 --- a/tests/testsuite/cargo_add/prefixed_v_in_version/stderr.term.svg +++ b/tests/testsuite/cargo_add/prefixed_v_in_version/stderr.term.svg @@ -1,4 +1,4 @@ - +