Skip to content

Commit dc9f202

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 6191945 commit dc9f202

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
@@ -280,7 +280,7 @@ fn spec_has_match(
280280
}
281281

282282
let version_matches = match (spec.version(), dep.version()) {
283-
(Some(v), Some(vq)) => semver::VersionReq::parse(vq)?.matches(v),
283+
(Some(v), Some(vq)) => semver::VersionReq::parse(vq)?.matches(&v),
284284
(Some(_), None) => false,
285285
(None, None | Some(_)) => true,
286286
};

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
}
@@ -128,14 +129,14 @@ impl PackageIdSpec {
128129
let name_or_version = parts.next().unwrap();
129130
match parts.next() {
130131
Some(part) => {
131-
let version = part.to_semver()?;
132+
let version = part.parse::<PartialVersion>()?;
132133
(InternedString::new(name_or_version), Some(version))
133134
}
134135
None => {
135136
if name_or_version.chars().next().unwrap().is_alphabetic() {
136137
(InternedString::new(name_or_version), None)
137138
} else {
138-
let version = name_or_version.to_semver()?;
139+
let version = name_or_version.parse::<PartialVersion>()?;
139140
(InternedString::new(path_name), Some(version))
140141
}
141142
}
@@ -155,7 +156,12 @@ impl PackageIdSpec {
155156
self.name
156157
}
157158

158-
pub fn version(&self) -> Option<&Version> {
159+
/// Full `semver::Version`, if present
160+
pub fn version(&self) -> Option<Version> {
161+
self.version.as_ref().and_then(|v| v.version())
162+
}
163+
164+
pub fn partial_version(&self) -> Option<&PartialVersion> {
159165
self.version.as_ref()
160166
}
161167

@@ -174,7 +180,8 @@ impl PackageIdSpec {
174180
}
175181

176182
if let Some(ref v) = self.version {
177-
if v != package_id.version() {
183+
let req = v.exact_req();
184+
if !req.matches(package_id.version()) {
178185
return false;
179186
}
180187
}
@@ -326,7 +333,6 @@ mod tests {
326333
use super::PackageIdSpec;
327334
use crate::core::{PackageId, SourceId};
328335
use crate::util::interning::InternedString;
329-
use crate::util::ToSemver;
330336
use url::Url;
331337

332338
#[test]
@@ -351,16 +357,25 @@ mod tests {
351357
"https://crates.io/foo#1.2.3",
352358
PackageIdSpec {
353359
name: InternedString::new("foo"),
354-
version: Some("1.2.3".to_semver().unwrap()),
360+
version: Some("1.2.3".parse().unwrap()),
355361
url: Some(Url::parse("https://crates.io/foo").unwrap()),
356362
},
357363
"https://crates.io/foo#1.2.3",
358364
);
365+
ok(
366+
"https://crates.io/foo#1.2",
367+
PackageIdSpec {
368+
name: InternedString::new("foo"),
369+
version: Some("1.2".parse().unwrap()),
370+
url: Some(Url::parse("https://crates.io/foo").unwrap()),
371+
},
372+
"https://crates.io/foo#1.2",
373+
);
359374
ok(
360375
"https://crates.io/foo#bar:1.2.3",
361376
PackageIdSpec {
362377
name: InternedString::new("bar"),
363-
version: Some("1.2.3".to_semver().unwrap()),
378+
version: Some("1.2.3".parse().unwrap()),
364379
url: Some(Url::parse("https://crates.io/foo").unwrap()),
365380
},
366381
"https://crates.io/foo#bar@1.2.3",
@@ -369,11 +384,20 @@ mod tests {
369384
"https://crates.io/foo#bar@1.2.3",
370385
PackageIdSpec {
371386
name: InternedString::new("bar"),
372-
version: Some("1.2.3".to_semver().unwrap()),
387+
version: Some("1.2.3".parse().unwrap()),
373388
url: Some(Url::parse("https://crates.io/foo").unwrap()),
374389
},
375390
"https://crates.io/foo#bar@1.2.3",
376391
);
392+
ok(
393+
"https://crates.io/foo#bar@1.2",
394+
PackageIdSpec {
395+
name: InternedString::new("bar"),
396+
version: Some("1.2".parse().unwrap()),
397+
url: Some(Url::parse("https://crates.io/foo").unwrap()),
398+
},
399+
"https://crates.io/foo#bar@1.2",
400+
);
377401
ok(
378402
"foo",
379403
PackageIdSpec {
@@ -387,7 +411,7 @@ mod tests {
387411
"foo:1.2.3",
388412
PackageIdSpec {
389413
name: InternedString::new("foo"),
390-
version: Some("1.2.3".to_semver().unwrap()),
414+
version: Some("1.2.3".parse().unwrap()),
391415
url: None,
392416
},
393417
"foo@1.2.3",
@@ -396,21 +420,29 @@ mod tests {
396420
"foo@1.2.3",
397421
PackageIdSpec {
398422
name: InternedString::new("foo"),
399-
version: Some("1.2.3".to_semver().unwrap()),
423+
version: Some("1.2.3".parse().unwrap()),
400424
url: None,
401425
},
402426
"foo@1.2.3",
403427
);
428+
ok(
429+
"foo@1.2",
430+
PackageIdSpec {
431+
name: InternedString::new("foo"),
432+
version: Some("1.2".parse().unwrap()),
433+
url: None,
434+
},
435+
"foo@1.2",
436+
);
404437
}
405438

406439
#[test]
407440
fn bad_parsing() {
408441
assert!(PackageIdSpec::parse("baz:").is_err());
409442
assert!(PackageIdSpec::parse("baz:*").is_err());
410-
assert!(PackageIdSpec::parse("baz:1.0").is_err());
411443
assert!(PackageIdSpec::parse("baz@").is_err());
412444
assert!(PackageIdSpec::parse("baz@*").is_err());
413-
assert!(PackageIdSpec::parse("baz@1.0").is_err());
445+
assert!(PackageIdSpec::parse("baz@^1.0").is_err());
414446
assert!(PackageIdSpec::parse("https://baz:1.0").is_err());
415447
assert!(PackageIdSpec::parse("https://#baz:1.0").is_err());
416448
}
@@ -428,5 +460,6 @@ mod tests {
428460
assert!(!PackageIdSpec::parse("foo:1.2.2").unwrap().matches(foo));
429461
assert!(PackageIdSpec::parse("foo@1.2.3").unwrap().matches(foo));
430462
assert!(!PackageIdSpec::parse("foo@1.2.2").unwrap().matches(foo));
463+
assert!(PackageIdSpec::parse("foo@1.2").unwrap().matches(foo));
431464
}
432465
}

src/cargo/ops/cargo_clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
9999
for spec_str in opts.spec.iter() {
100100
// Translate the spec to a Package.
101101
let spec = PackageIdSpec::parse(spec_str)?;
102-
if spec.version().is_some() {
102+
if spec.partial_version().is_some() {
103103
config.shell().warn(&format!(
104104
"version qualifier in `-p {}` is ignored, \
105105
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+
| `regex@1.4` | `regex` | `1.4.*` |
4345
| `regex@1.4.3` | `regex` | `1.4.3` |
4446
| `https://github.com/rust-lang/crates.io-index#regex` | `regex` | `*` |
4547
| `https://github.com/rust-lang/crates.io-index#regex@1.4.3` | `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+
two-ver@0.1.0
157+
two-ver@0.2.0
160158
",
161159
)
162160
.run();
163161

164162
// Incomplete version.
165163
p.cargo("pkgid two-ver@0.2")
166-
.with_status(101)
167-
.with_stderr(
164+
.with_status(0)
165+
.with_stdout(
168166
"\
169-
error: invalid package ID specification: `two-ver@0.2`
170-
171-
Caused by:
172-
cannot parse '0.2' as a SemVer version
167+
https://github.com/rust-lang/crates.io-index#two-ver@0.2.0
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)