From caeaaa529c39ebad8df18d9a0112dddb2cf57b7d Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sun, 21 Jan 2024 20:21:14 -0500 Subject: [PATCH] fix(cargo-update): once warn once for `--precise ` This also tweaks the error message a bit. --- src/cargo/sources/registry/mod.rs | 21 ++++++++++++++------- tests/testsuite/update.rs | 13 ++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index c363d609970..f2f2bb037fa 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -261,6 +261,12 @@ pub struct RegistrySource<'cfg> { /// Otherwise, the resolver would think that those entries no longer /// exist, and it would trigger updates to unrelated packages. yanked_whitelist: HashSet, + /// Yanked versions that have already been selected during queries. + /// + /// As of this writing, this is for not emitting the `--precise ` + /// warning twice, with the assumption of (`dep.package_name()` + `--precise` + /// version) being sufficient to uniquely identify the same query result. + selected_precise_yanked: HashSet<(InternedString, semver::Version)>, } /// The [`config.json`] file stored in the index. @@ -531,6 +537,7 @@ impl<'cfg> RegistrySource<'cfg> { index: index::RegistryIndex::new(source_id, ops.index_path(), config), yanked_whitelist: yanked_whitelist.clone(), ops, + selected_precise_yanked: HashSet::new(), } } @@ -812,13 +819,13 @@ impl<'cfg> Source for RegistrySource<'cfg> { let version = req .precise_version() .expect("--precise in use"); - let source = self.source_id(); - let mut shell = self.config.shell(); - shell.warn(format_args!( - "yanked package `{name}@{version}` is selected by the `--precise` flag from {source}", - ))?; - shell.note("it is not recommended to depend on a yanked version")?; - shell.note("if possible, try other SemVer-compatbile versions")?; + if self.selected_precise_yanked.insert((name, version.clone())) { + let mut shell = self.config.shell(); + shell.warn(format_args!( + "selected package `{name}@{version}` was yanked by the author" + ))?; + shell.note("if possible, try a compatible non-yanked version")?; + } } if called { return Poll::Ready(Ok(())); diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index fc93e84a0d7..0d283d58079 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -1419,9 +1419,8 @@ Caused by: .with_stderr( "\ [UPDATING] `dummy-registry` index -[WARNING] yanked package `bar@0.1.1` is selected by the `--precise` flag from registry `dummy-registry` -[NOTE] it is not recommended to depend on a yanked version -[NOTE] if possible, try other SemVer-compatbile versions +[WARNING] selected package `bar@0.1.1` was yanked by the author +[NOTE] if possible, try a compatible non-yanked version [UPDATING] bar v0.1.0 -> v0.1.1 ", ) @@ -1463,12 +1462,8 @@ fn precise_yanked_multiple_presence() { .with_stderr( "\ [UPDATING] `dummy-registry` index -[WARNING] yanked package `bar@0.1.1` is selected by the `--precise` flag from registry `dummy-registry` -[NOTE] it is not recommended to depend on a yanked version -[NOTE] if possible, try other SemVer-compatbile versions -[WARNING] yanked package `bar@0.1.1` is selected by the `--precise` flag from registry `dummy-registry` -[NOTE] it is not recommended to depend on a yanked version -[NOTE] if possible, try other SemVer-compatbile versions +[WARNING] selected package `bar@0.1.1` was yanked by the author +[NOTE] if possible, try a compatible non-yanked version [UPDATING] bar v0.1.0 -> v0.1.1 ", )