File tree Expand file tree Collapse file tree 12 files changed +234
-4
lines changed Expand file tree Collapse file tree 12 files changed +234
-4
lines changed Original file line number Diff line number Diff line change @@ -286,6 +286,12 @@ fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
286
286
. next ( )
287
287
. ok_or_else ( || format_err ! ( "no version provided for the `--version` flag" ) ) ?;
288
288
289
+ if let Some ( stripped) = v. strip_prefix ( "v" ) {
290
+ bail ! (
291
+ "the version provided, `{v}` is not a valid SemVer requirement\n \n \
292
+ help: try changing the version to `{stripped}`",
293
+ )
294
+ }
289
295
let is_req = "<>=^~" . contains ( first) || v. contains ( '*' ) ;
290
296
if is_req {
291
297
match v. parse :: < VersionReq > ( ) {
Original file line number Diff line number Diff line change 1
1
use crate :: command_prelude:: * ;
2
2
3
+ use anyhow:: Context ;
3
4
use cargo:: ops;
4
5
use cargo_credential:: Secret ;
5
6
@@ -60,5 +61,19 @@ fn resolve_crate<'k>(
60
61
krate = Some ( k) ;
61
62
version = Some ( v) ;
62
63
}
64
+
65
+ if let Some ( version) = version {
66
+ semver:: Version :: parse ( version) . with_context ( || {
67
+ if let Some ( stripped) = version. strip_prefix ( "v" ) {
68
+ return format ! (
69
+ "the version provided, `{version}` is not a \
70
+ valid SemVer version\n \n \
71
+ help: try changing the version to `{stripped}`",
72
+ ) ;
73
+ }
74
+ format ! ( "invalid version `{version}`" )
75
+ } ) ?;
76
+ }
77
+
63
78
Ok ( ( krate, version) )
64
79
}
Original file line number Diff line number Diff line change @@ -523,8 +523,16 @@ impl SourceId {
523
523
version : semver:: Version ,
524
524
precise : & str ,
525
525
) -> CargoResult < SourceId > {
526
- let precise = semver:: Version :: parse ( precise)
527
- . with_context ( || format ! ( "invalid version format for precise version `{precise}`" ) ) ?;
526
+ let precise = semver:: Version :: parse ( precise) . with_context ( || {
527
+ if let Some ( stripped) = precise. strip_prefix ( "v" ) {
528
+ return format ! (
529
+ "the version provided, `{precise}` is not a \
530
+ valid SemVer version\n \n \
531
+ help: try changing the version to `{stripped}`",
532
+ ) ;
533
+ }
534
+ format ! ( "invalid version format for precise version `{precise}`" )
535
+ } ) ?;
528
536
529
537
Ok ( SourceId :: wrap ( SourceIdInner {
530
538
precise : Some ( Precise :: Updated {
Original file line number Diff line number Diff line change @@ -47,8 +47,16 @@ impl CrateSpec {
47
47
package_name?;
48
48
49
49
if let Some ( version) = version {
50
- semver:: VersionReq :: parse ( version)
51
- . with_context ( || format ! ( "invalid version requirement `{version}`" ) ) ?;
50
+ semver:: VersionReq :: parse ( version) . with_context ( || {
51
+ if let Some ( stripped) = version. strip_prefix ( "v" ) {
52
+ return format ! (
53
+ "the version provided, `{version}` is not a \
54
+ valid SemVer requirement\n \n \
55
+ help: changing the package to `{name}@{stripped}`",
56
+ ) ;
57
+ }
58
+ format ! ( "invalid version requirement `{version}`" )
59
+ } ) ?;
52
60
}
53
61
54
62
let id = Self {
Original file line number Diff line number Diff line change @@ -126,6 +126,7 @@ mod path_base_unstable;
126
126
mod path_dev;
127
127
mod path_inferred_name;
128
128
mod path_inferred_name_conflicts_full_feature;
129
+ mod prefixed_v_in_version;
129
130
mod preserve_dep_std_table;
130
131
mod preserve_features_sorted;
131
132
mod preserve_features_table;
Original file line number Diff line number Diff line change
1
+ ../add-basic.in
Original file line number Diff line number Diff line change
1
+ use cargo_test_support:: compare:: assert_ui;
2
+ use cargo_test_support:: current_dir;
3
+ use cargo_test_support:: file;
4
+ use cargo_test_support:: prelude:: * ;
5
+ use cargo_test_support:: str;
6
+ use cargo_test_support:: Project ;
7
+
8
+ #[ cargo_test]
9
+ fn case ( ) {
10
+ cargo_test_support:: registry:: init ( ) ;
11
+
12
+ let project = Project :: from_template ( current_dir ! ( ) . join ( "in" ) ) ;
13
+ let project_root = project. root ( ) ;
14
+ let cwd = & project_root;
15
+
16
+ snapbox:: cmd:: Command :: cargo_ui ( )
17
+ . arg ( "add" )
18
+ . arg_line ( "foo@v0.0.1" )
19
+ . current_dir ( cwd)
20
+ . assert ( )
21
+ . code ( 101 )
22
+ . stdout_eq ( str![ "" ] )
23
+ . stderr_eq ( file ! [ "stderr.term.svg" ] ) ;
24
+
25
+ assert_ui ( ) . subset_matches ( current_dir ! ( ) . join ( "out" ) , & project_root) ;
26
+ }
Original file line number Diff line number Diff line change
1
+ [workspace ]
2
+
3
+ [package ]
4
+ name = " cargo-list-test-fixture"
5
+ version = " 0.0.0"
6
+ edition = " 2015"
Original file line number Diff line number Diff line change @@ -2899,3 +2899,19 @@ fn dry_run_remove_orphan() {
2899
2899
// Ensure server is still installed after the dry run
2900
2900
assert_has_installed_exe ( paths:: cargo_home ( ) , "server" ) ;
2901
2901
}
2902
+
2903
+ #[ cargo_test]
2904
+ fn prefixed_v_in_version ( ) {
2905
+ pkg ( "foo" , "0.0.1" ) ;
2906
+ cargo_process ( "install foo@v0.0.1" )
2907
+ . with_status ( 1 )
2908
+ . with_stderr_data ( str![ [ r#"
2909
+ [ERROR] invalid value 'foo@v0.0.1' for '[CRATE[@<VER>]]...': the version provided, `v0.0.1` is not a valid SemVer requirement
2910
+
2911
+ [HELP] try changing the version to `0.0.1`
2912
+
2913
+ For more information, try '--help'.
2914
+
2915
+ "# ] ] )
2916
+ . run ( ) ;
2917
+ }
You can’t perform that action at this time.
0 commit comments