Auto merge of #9945 - ehuss:update-precise-metadata, r=alexcrichton

Allow `cargo update --precise` with metadata.

`cargo update --precise` would require that the version matches *exactly*, including build metadata.  Usually the build metadata is ignored (like in dependency declarations), but in this circumstance it isn't.  This can be awkward in some cases where it can be more convenient to type just the version number without the build metadata.

This changes it so that if the metadata isn't provided, then it will be ignored when matching.  Otherwise, it will be honored. This is slightly different from a version requirement like `=1.2.3+foo` which ignores the metadata completely.

This also adds a slightly better error message if you don't type in valid syntax for a version number (previously it would just emit the `no matching package` error).
This commit is contained in:
bors 2021-09-27 13:44:18 +00:00
commit d56b42c549
3 changed files with 113 additions and 12 deletions

View File

@ -1,8 +1,3 @@
use std::collections::{BTreeMap, HashSet};
use log::debug;
use termcolor::Color::{self, Cyan, Green, Red};
use crate::core::registry::PackageRegistry;
use crate::core::resolver::features::{CliFeatures, HasDevUnits};
use crate::core::{PackageId, PackageIdSpec};
@ -10,6 +5,10 @@ use crate::core::{Resolve, SourceId, Workspace};
use crate::ops;
use crate::util::config::Config;
use crate::util::CargoResult;
use anyhow::Context;
use log::debug;
use std::collections::{BTreeMap, HashSet};
use termcolor::Color::{self, Cyan, Green, Red};
pub struct UpdateOptions<'a> {
pub config: &'a Config,
@ -95,6 +94,9 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
// seems like a pretty hokey reason to single out
// the registry as well.
let precise = if dep.source_id().is_registry() {
semver::Version::parse(precise).with_context(|| {
format!("invalid version format for precise version `{}`", precise)
})?;
format!("{}={}->{}", dep.name(), dep.version(), precise)
} else {
precise.to_string()

View File

@ -460,19 +460,40 @@ impl<'cfg> RegistryIndex<'cfg> {
// this source, `<p_req>` is the version installed and `<f_req> is the
// version requested (argument to `--precise`).
let name = dep.package_name().as_str();
let summaries = summaries.filter(|s| match source_id.precise() {
let precise = match source_id.precise() {
Some(p) if p.starts_with(name) && p[name.len()..].starts_with('=') => {
let mut vers = p[name.len() + 1..].splitn(2, "->");
if dep
.version_req()
.matches(&vers.next().unwrap().to_semver().unwrap())
{
vers.next().unwrap() == s.version().to_string()
let current_vers = vers.next().unwrap().to_semver().unwrap();
let requested_vers = vers.next().unwrap().to_semver().unwrap();
Some((current_vers, requested_vers))
}
_ => None,
};
let summaries = summaries.filter(|s| match &precise {
Some((current, requested)) => {
if dep.version_req().matches(current) {
// Unfortunately crates.io allows versions to differ only
// by build metadata. This shouldn't be allowed, but since
// it is, this will honor it if requested. However, if not
// specified, then ignore it.
let s_vers = s.version();
match (s_vers.build.is_empty(), requested.build.is_empty()) {
(true, true) => s_vers == requested,
(true, false) => false,
(false, true) => {
// Strip out the metadata.
s_vers.major == requested.major
&& s_vers.minor == requested.minor
&& s_vers.patch == requested.patch
&& s_vers.pre == requested.pre
}
(false, false) => s_vers == requested,
}
} else {
true
}
}
_ => true,
None => true,
});
let mut count = 0;

View File

@ -678,3 +678,81 @@ fn workspace_only() {
assert!(!lock1.contains("0.0.2"));
assert!(!lock2.contains("0.0.1"));
}
#[cargo_test]
fn precise_with_build_metadata() {
// +foo syntax shouldn't be necessary with --precise
Package::new("bar", "0.1.0+extra-stuff.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = "0.1"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("generate-lockfile").run();
Package::new("bar", "0.1.1+extra-stuff.1").publish();
Package::new("bar", "0.1.2+extra-stuff.2").publish();
p.cargo("update -p bar --precise 0.1")
.with_status(101)
.with_stderr(
"\
error: invalid version format for precise version `0.1`
Caused by:
unexpected end of input while parsing minor version number
",
)
.run();
p.cargo("update -p bar --precise 0.1.1+does-not-match")
.with_status(101)
.with_stderr(
"\
[UPDATING] [..] index
error: no matching package named `bar` found
location searched: registry `crates-io`
required by package `foo v0.1.0 ([ROOT]/foo)`
",
)
.run();
p.cargo("update -p bar --precise 0.1.1")
.with_stderr(
"\
[UPDATING] [..] index
[UPDATING] bar v0.1.0+extra-stuff.0 -> v0.1.1+extra-stuff.1
",
)
.run();
Package::new("bar", "0.1.3").publish();
p.cargo("update -p bar --precise 0.1.3+foo")
.with_status(101)
.with_stderr(
"\
[UPDATING] [..] index
error: no matching package named `bar` found
location searched: registry `crates-io`
required by package `foo v0.1.0 ([ROOT]/foo)`
",
)
.run();
p.cargo("update -p bar --precise 0.1.3")
.with_stderr(
"\
[UPDATING] [..] index
[UPDATING] bar v0.1.1+extra-stuff.1 -> v0.1.3
",
)
.run();
}