Skip to content

Commit 0f06cd8

Browse files
committed
feat(spec): Allow partial versions when unambigious
This was proposed in #12425 to help improve usability of the existing `cargo update` when dealing with the added workflows.
1 parent 90aa439 commit 0f06cd8

File tree

10 files changed

+111
-55
lines changed

10 files changed

+111
-55
lines changed

src/bin/cargo/commands/remove.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ fn spec_has_match(
285285
}
286286

287287
let version_matches = match (spec.version(), dep.version()) {
288-
(Some(v), Some(vq)) => semver::VersionReq::parse(vq)?.matches(v),
288+
(Some(v), Some(vq)) => semver::VersionReq::parse(vq)?.matches(&v),
289289
(Some(_), None) => false,
290290
(None, None | Some(_)) => true,
291291
};

src/cargo/core/package_id_spec.rs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::core::PackageId;
1010
use crate::util::edit_distance;
1111
use crate::util::errors::CargoResult;
1212
use crate::util::interning::InternedString;
13-
use crate::util::{validate_package_name, IntoUrl, ToSemver};
13+
use crate::util::PartialVersion;
14+
use crate::util::{validate_package_name, IntoUrl};
1415

1516
/// Some or all of the data required to identify a package:
1617
///
@@ -24,7 +25,7 @@ use crate::util::{validate_package_name, IntoUrl, ToSemver};
2425
#[derive(Clone, PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
2526
pub struct PackageIdSpec {
2627
name: InternedString,
27-
version: Option<Version>,
28+
version: Option<PartialVersion>,
2829
url: Option<Url>,
2930
}
3031

@@ -70,7 +71,7 @@ impl PackageIdSpec {
7071
let mut parts = spec.splitn(2, [':', '@']);
7172
let name = parts.next().unwrap();
7273
let version = match parts.next() {
73-
Some(version) => Some(version.to_semver()?),
74+
Some(version) => Some(version.parse::<PartialVersion>()?),
7475
None => None,
7576
};
7677
validate_package_name(name, "pkgid", "")?;
@@ -94,12 +95,12 @@ impl PackageIdSpec {
9495
spec.query(i)
9596
}
9697

97-
/// Convert a `PackageId` to a `PackageIdSpec`, which will have both the `Version` and `Url`
98+
/// Convert a `PackageId` to a `PackageIdSpec`, which will have both the `PartialVersion` and `Url`
9899
/// fields filled in.
99100
pub fn from_package_id(package_id: PackageId) -> PackageIdSpec {
100101
PackageIdSpec {
101102
name: package_id.name(),
102-
version: Some(package_id.version().clone()),
103+
version: Some(package_id.version().clone().into()),
103104
url: Some(package_id.source_id().url().clone()),
104105
}
105106
}
@@ -125,14 +126,14 @@ impl PackageIdSpec {
125126
match frag {
126127
Some(fragment) => match fragment.split_once([':', '@']) {
127128
Some((name, part)) => {
128-
let version = part.to_semver()?;
129+
let version = part.parse::<PartialVersion>()?;
129130
(InternedString::new(name), Some(version))
130131
}
131132
None => {
132133
if fragment.chars().next().unwrap().is_alphabetic() {
133134
(InternedString::new(&fragment), None)
134135
} else {
135-
let version = fragment.to_semver()?;
136+
let version = fragment.parse::<PartialVersion>()?;
136137
(InternedString::new(path_name), Some(version))
137138
}
138139
}
@@ -151,7 +152,12 @@ impl PackageIdSpec {
151152
self.name
152153
}
153154

154-
pub fn version(&self) -> Option<&Version> {
155+
/// Full `semver::Version`, if present
156+
pub fn version(&self) -> Option<Version> {
157+
self.version.as_ref().and_then(|v| v.version())
158+
}
159+
160+
pub fn partial_version(&self) -> Option<&PartialVersion> {
155161
self.version.as_ref()
156162
}
157163

@@ -170,7 +176,8 @@ impl PackageIdSpec {
170176
}
171177

172178
if let Some(ref v) = self.version {
173-
if v != package_id.version() {
179+
let req = v.exact_req();
180+
if !req.matches(package_id.version()) {
174181
return false;
175182
}
176183
}
@@ -322,7 +329,6 @@ mod tests {
322329
use super::PackageIdSpec;
323330
use crate::core::{PackageId, SourceId};
324331
use crate::util::interning::InternedString;
325-
use crate::util::ToSemver;
326332
use url::Url;
327333

328334
#[test]
@@ -347,16 +353,25 @@ mod tests {
347353
"https://crates.io/foo#1.2.3",
348354
PackageIdSpec {
349355
name: InternedString::new("foo"),
350-
version: Some("1.2.3".to_semver().unwrap()),
356+
version: Some("1.2.3".parse().unwrap()),
351357
url: Some(Url::parse("https://crates.io/foo").unwrap()),
352358
},
353359
"https://crates.io/foo#1.2.3",
354360
);
361+
ok(
362+
"https://crates.io/foo#1.2",
363+
PackageIdSpec {
364+
name: InternedString::new("foo"),
365+
version: Some("1.2".parse().unwrap()),
366+
url: Some(Url::parse("https://crates.io/foo").unwrap()),
367+
},
368+
"https://crates.io/foo#1.2",
369+
);
355370
ok(
356371
"https://crates.io/foo#bar:1.2.3",
357372
PackageIdSpec {
358373
name: InternedString::new("bar"),
359-
version: Some("1.2.3".to_semver().unwrap()),
374+
version: Some("1.2.3".parse().unwrap()),
360375
url: Some(Url::parse("https://crates.io/foo").unwrap()),
361376
},
362377
"https://crates.io/foo#[email protected]",
@@ -365,11 +380,20 @@ mod tests {
365380
"https://crates.io/foo#[email protected]",
366381
PackageIdSpec {
367382
name: InternedString::new("bar"),
368-
version: Some("1.2.3".to_semver().unwrap()),
383+
version: Some("1.2.3".parse().unwrap()),
369384
url: Some(Url::parse("https://crates.io/foo").unwrap()),
370385
},
371386
"https://crates.io/foo#[email protected]",
372387
);
388+
ok(
389+
"https://crates.io/foo#[email protected]",
390+
PackageIdSpec {
391+
name: InternedString::new("bar"),
392+
version: Some("1.2".parse().unwrap()),
393+
url: Some(Url::parse("https://crates.io/foo").unwrap()),
394+
},
395+
"https://crates.io/foo#[email protected]",
396+
);
373397
ok(
374398
"foo",
375399
PackageIdSpec {
@@ -383,7 +407,7 @@ mod tests {
383407
"foo:1.2.3",
384408
PackageIdSpec {
385409
name: InternedString::new("foo"),
386-
version: Some("1.2.3".to_semver().unwrap()),
410+
version: Some("1.2.3".parse().unwrap()),
387411
url: None,
388412
},
389413
@@ -392,21 +416,29 @@ mod tests {
392416
393417
PackageIdSpec {
394418
name: InternedString::new("foo"),
395-
version: Some("1.2.3".to_semver().unwrap()),
419+
version: Some("1.2.3".parse().unwrap()),
396420
url: None,
397421
},
398422
399423
);
424+
ok(
425+
426+
PackageIdSpec {
427+
name: InternedString::new("foo"),
428+
version: Some("1.2".parse().unwrap()),
429+
url: None,
430+
},
431+
432+
);
400433
}
401434

402435
#[test]
403436
fn bad_parsing() {
404437
assert!(PackageIdSpec::parse("baz:").is_err());
405438
assert!(PackageIdSpec::parse("baz:*").is_err());
406-
assert!(PackageIdSpec::parse("baz:1.0").is_err());
407439
assert!(PackageIdSpec::parse("baz@").is_err());
408440
assert!(PackageIdSpec::parse("baz@*").is_err());
409-
assert!(PackageIdSpec::parse("[email protected]").is_err());
441+
assert!(PackageIdSpec::parse("baz@^1.0").is_err());
410442
assert!(PackageIdSpec::parse("https://baz:1.0").is_err());
411443
assert!(PackageIdSpec::parse("https://#baz:1.0").is_err());
412444
}
@@ -424,5 +456,6 @@ mod tests {
424456
assert!(!PackageIdSpec::parse("foo:1.2.2").unwrap().matches(foo));
425457
assert!(PackageIdSpec::parse("[email protected]").unwrap().matches(foo));
426458
assert!(!PackageIdSpec::parse("[email protected]").unwrap().matches(foo));
459+
assert!(PackageIdSpec::parse("[email protected]").unwrap().matches(foo));
427460
}
428461
}

src/cargo/ops/cargo_clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
108108
for spec_str in opts.spec.iter() {
109109
// Translate the spec to a Package.
110110
let spec = PackageIdSpec::parse(spec_str)?;
111-
if spec.version().is_some() {
111+
if spec.partial_version().is_some() {
112112
config.shell().warn(&format!(
113113
"version qualifier in `-p {}` is ignored, \
114114
cleaning all versions of `{}` found",

src/cargo/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use self::progress::{Progress, ProgressStyle};
2222
pub use self::queue::Queue;
2323
pub use self::restricted_names::validate_package_name;
2424
pub use self::rustc::Rustc;
25-
pub use self::semver_ext::{OptVersionReq, RustVersion, VersionExt, VersionReqExt};
25+
pub use self::semver_ext::{OptVersionReq, PartialVersion, RustVersion, VersionExt, VersionReqExt};
2626
pub use self::to_semver::ToSemver;
2727
pub use self::vcs::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
2828
pub use self::workspace::{

src/cargo/util/semver_ext.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ pub struct PartialVersion {
164164
}
165165

166166
impl PartialVersion {
167+
pub fn version(&self) -> Option<Version> {
168+
Some(Version {
169+
major: self.major,
170+
minor: self.minor?,
171+
patch: self.patch?,
172+
pre: self.pre.clone().unwrap_or_default(),
173+
build: self.build.clone().unwrap_or_default(),
174+
})
175+
}
176+
167177
pub fn caret_req(&self) -> VersionReq {
168178
VersionReq {
169179
comparators: vec![Comparator {
@@ -175,6 +185,18 @@ impl PartialVersion {
175185
}],
176186
}
177187
}
188+
189+
pub fn exact_req(&self) -> VersionReq {
190+
VersionReq {
191+
comparators: vec![Comparator {
192+
op: semver::Op::Exact,
193+
major: self.major,
194+
minor: self.minor,
195+
patch: self.patch,
196+
pre: self.pre.as_ref().cloned().unwrap_or_default(),
197+
}],
198+
}
199+
}
178200
}
179201

180202
impl From<semver::Version> for PartialVersion {

src/cargo/util/toml/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,8 +2653,8 @@ impl TomlManifest {
26532653
replacement.unused_keys(),
26542654
&mut cx.warnings,
26552655
);
2656-
dep.set_version_req(VersionReq::exact(version))
2657-
.lock_version(version);
2656+
dep.set_version_req(VersionReq::exact(&version))
2657+
.lock_version(&version);
26582658
replace.push((spec, dep));
26592659
}
26602660
Ok(replace)

src/doc/src/reference/pkgid-spec.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The formal grammar for a Package Id Specification is:
2424
spec := pkgname
2525
| proto "://" hostname-and-path [ "#" ( pkgname | semver ) ]
2626
pkgname := name [ ("@" | ":" ) semver ]
27+
semver := digits [ "." digits [ "." digits [ "-" prerelease ] [ "+" build ]]]
2728
2829
proto := "http" | "git" | ...
2930
```
@@ -40,6 +41,7 @@ The following are references to the `regex` package on `crates.io`:
4041
| Spec | Name | Version |
4142
|:------------------------------------------------------------|:-------:|:-------:|
4243
| `regex` | `regex` | `*` |
44+
| `[email protected]` | `regex` | `1.4.*` |
4345
| `[email protected]` | `regex` | `1.4.3` |
4446
| `https://github.com/rust-lang/crates.io-index#regex` | `regex` | `*` |
4547
| `https://github.com/rust-lang/crates.io-index#[email protected]` | `regex` | `1.4.3` |

tests/testsuite/clean.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -659,13 +659,21 @@ error: package ID specification `baz` did not match any packages
659659
.run();
660660

661661
p.cargo("clean -p bar:0.1")
662-
.with_status(101)
663662
.with_stderr(
664-
"\
665-
error: cannot parse '0.1' as a SemVer version
666-
",
663+
"warning: version qualifier in `-p bar:0.1` is ignored, \
664+
cleaning all versions of `bar` found",
667665
)
668666
.run();
667+
let mut walker = walkdir::WalkDir::new(p.build_dir())
668+
.into_iter()
669+
.filter_map(|e| e.ok())
670+
.filter(|e| {
671+
let n = e.file_name().to_str().unwrap();
672+
n.starts_with("bar") || n.starts_with("libbar")
673+
});
674+
if let Some(e) = walker.next() {
675+
panic!("{:?} was not cleaned", e.path());
676+
}
669677
}
670678

671679
#[cargo_test]
@@ -705,13 +713,21 @@ error: package ID specification `baz` did not match any packages
705713
.run();
706714

707715
p.cargo("clean -p bar:0")
708-
.with_status(101)
709716
.with_stderr(
710-
"\
711-
error: cannot parse '0' as a SemVer version
712-
",
717+
"warning: version qualifier in `-p bar:0` is ignored, \
718+
cleaning all versions of `bar` found",
713719
)
714720
.run();
721+
let mut walker = walkdir::WalkDir::new(p.build_dir())
722+
.into_iter()
723+
.filter_map(|e| e.ok())
724+
.filter(|e| {
725+
let n = e.file_name().to_str().unwrap();
726+
n.starts_with("bar") || n.starts_with("libbar")
727+
});
728+
if let Some(e) = walker.next() {
729+
panic!("{:?} was not cleaned", e.path());
730+
}
715731
}
716732

717733
#[cargo_test]

tests/testsuite/pkgid.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,20 @@ fn multiple_versions() {
151151
.with_status(101)
152152
.with_stderr(
153153
"\
154-
error: invalid package ID specification: `two-ver@0`
155-
156-
<tab>Did you mean `two-ver`?
157-
158-
Caused by:
159-
cannot parse '0' as a SemVer version
154+
error: There are multiple `two-ver` packages in your project, and the specification `two-ver@0` is ambiguous.
155+
Please re-run this command with `-p <spec>` where `<spec>` is one of the following:
156+
157+
160158
",
161159
)
162160
.run();
163161

164162
// Incomplete version.
165163
p.cargo("pkgid [email protected]")
166-
.with_status(101)
167-
.with_stderr(
164+
.with_status(0)
165+
.with_stdout(
168166
"\
169-
error: invalid package ID specification: `[email protected]`
170-
171-
Caused by:
172-
cannot parse '0.2' as a SemVer version
167+
https://github.com/rust-lang/crates.io-index#[email protected]
173168
",
174169
)
175170
.run();

tests/testsuite/profile_overrides.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,19 +317,7 @@ fn profile_override_spec_with_partial_version() {
317317
.build();
318318

319319
p.cargo("check -v")
320-
.with_status(101)
321-
.with_stderr_contains(
322-
"\
323-
error: failed to parse manifest at `[CWD]/Cargo.toml`
324-
325-
Caused by:
326-
TOML parse error at line 9, column 34
327-
|
328-
9 | [profile.dev.package.\"bar:0.5\"]
329-
| ^^^^^^^^^
330-
cannot parse '0.5' as a SemVer version
331-
",
332-
)
320+
.with_stderr_contains("[RUNNING] `rustc [..]bar/src/lib.rs [..] -C codegen-units=2 [..]")
333321
.run();
334322
}
335323

0 commit comments

Comments
 (0)