Skip to content

Commit

Permalink
Refactor check_yanked to avoid some duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
arlosi committed Jul 8, 2022
1 parent 6867277 commit 8a1f401
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 45 deletions.
45 changes: 6 additions & 39 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::task::Poll;
use std::{env, fs};

use crate::core::compiler::{CompileKind, DefaultExecutor, Executor, Freshness, UnitOutput};
Expand Down Expand Up @@ -531,44 +530,12 @@ impl<'cfg, 'a> InstallablePackage<'cfg, 'a> {
// duplicate "Updating", but since `source` is taken by value, then it
// wouldn't be available for `compile_ws`.
let (pkg_set, resolve) = ops::resolve_ws(&self.ws)?;

// Checking the yanked status involves taking a look at the registry and
// maybe updating files, so be sure to lock it here.
let _lock = self.ws.config().acquire_package_cache_lock()?;

let mut sources = pkg_set.sources_mut();
let mut pending: Vec<PackageId> = resolve.iter().collect();
let mut results = Vec::new();
for (_id, source) in sources.sources_mut() {
source.invalidate_cache();
}
while !pending.is_empty() {
pending.retain(|pkg_id| {
if let Some(source) = sources.get_mut(pkg_id.source_id()) {
match source.is_yanked(*pkg_id) {
Poll::Ready(result) => results.push((*pkg_id, result)),
Poll::Pending => return true,
}
}
false
});
for (_id, source) in sources.sources_mut() {
source.block_until_ready()?;
}
}

for (pkg_id, is_yanked) in results {
if is_yanked? {
self.ws.config().shell().warn(format!(
"package `{}` in Cargo.lock is yanked in registry `{}`, \
consider running without --locked",
pkg_id,
pkg_id.source_id().display_registry_name()
))?;
}
}

Ok(())
ops::check_yanked(
self.ws.config(),
&pkg_set,
&resolve,
"consider running without --locked",
)
}
}

Expand Down
20 changes: 15 additions & 5 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,12 @@ fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
if let Some(orig_resolve) = orig_resolve {
compare_resolve(config, tmp_ws.current()?, &orig_resolve, &new_resolve)?;
}
check_yanked(config, &pkg_set, &new_resolve)?;
check_yanked(
config,
&pkg_set,
&new_resolve,
"consider updating to a version that is not yanked",
)?;

ops::resolve_to_string(&tmp_ws, &mut new_resolve)
}
Expand Down Expand Up @@ -717,7 +722,12 @@ fn compare_resolve(
Ok(())
}

fn check_yanked(config: &Config, pkg_set: &PackageSet<'_>, resolve: &Resolve) -> CargoResult<()> {
pub fn check_yanked(
config: &Config,
pkg_set: &PackageSet<'_>,
resolve: &Resolve,
hint: &str,
) -> CargoResult<()> {
// Checking the yanked status involves taking a look at the registry and
// maybe updating files, so be sure to lock it here.
let _lock = config.acquire_package_cache_lock()?;
Expand Down Expand Up @@ -746,10 +756,10 @@ fn check_yanked(config: &Config, pkg_set: &PackageSet<'_>, resolve: &Resolve) ->
for (pkg_id, is_yanked) in results {
if is_yanked? {
config.shell().warn(format!(
"package `{}` in Cargo.lock is yanked in registry `{}`, \
consider updating to a version that is not yanked",
"package `{}` in Cargo.lock is yanked in registry `{}`, {}",
pkg_id,
pkg_id.source_id().display_registry_name()
pkg_id.source_id().display_registry_name(),
hint
))?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use self::cargo_generate_lockfile::UpdateOptions;
pub use self::cargo_install::{install, install_list};
pub use self::cargo_new::{init, new, NewOptions, VersionControl};
pub use self::cargo_output_metadata::{output_metadata, ExportInfo, OutputMetadataOptions};
pub use self::cargo_package::{package, package_one, PackageOpts};
pub use self::cargo_package::{check_yanked, package, package_one, PackageOpts};
pub use self::cargo_pkgid::pkgid;
pub use self::cargo_read_manifest::{read_package, read_packages};
pub use self::cargo_run::run;
Expand Down

0 comments on commit 8a1f401

Please sign in to comment.