Add a dry_run argument to resolve_ws().

This commit is contained in:
Tor Hovland 2024-06-05 17:58:08 +02:00
parent b95fb6e2bf
commit b902398b02
14 changed files with 48 additions and 18 deletions

View file

@ -33,6 +33,7 @@ fn do_resolve<'gctx>(gctx: &'gctx GlobalContext, ws_root: &Path) -> ResolveInfo<
let force_all_targets = ForceAllTargets::No; let force_all_targets = ForceAllTargets::No;
// Do an initial run to download anything necessary so that it does // Do an initial run to download anything necessary so that it does
// not confuse criterion's warmup. // not confuse criterion's warmup.
let dry_run = false;
let ws_resolve = cargo::ops::resolve_ws_with_opts( let ws_resolve = cargo::ops::resolve_ws_with_opts(
&ws, &ws,
&mut target_data, &mut target_data,
@ -41,6 +42,7 @@ fn do_resolve<'gctx>(gctx: &'gctx GlobalContext, ws_root: &Path) -> ResolveInfo<
&specs, &specs,
has_dev_units, has_dev_units,
force_all_targets, force_all_targets,
dry_run,
) )
.unwrap(); .unwrap();
ResolveInfo { ResolveInfo {
@ -71,6 +73,7 @@ fn resolve_ws(c: &mut Criterion) {
// iterator once, and we don't want to call `do_resolve` in every // iterator once, and we don't want to call `do_resolve` in every
// "step", since that would just be some useless work. // "step", since that would just be some useless work.
let mut lazy_info = None; let mut lazy_info = None;
let dry_run = false;
group.bench_function(&ws_name, |b| { group.bench_function(&ws_name, |b| {
let ResolveInfo { let ResolveInfo {
ws, ws,
@ -91,6 +94,7 @@ fn resolve_ws(c: &mut Criterion) {
specs, specs,
*has_dev_units, *has_dev_units,
*force_all_targets, *force_all_targets,
dry_run,
) )
.unwrap(); .unwrap();
}) })

View file

@ -214,11 +214,9 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
}; };
add(&ws, &options)?; add(&ws, &options)?;
if !dry_run { // Reload the workspace since we've changed dependencies
// Reload the workspace since we've changed dependencies let ws = args.workspace(gctx)?;
let ws = args.workspace(gctx)?; resolve_ws(&ws, dry_run)?;
resolve_ws(&ws)?;
}
Ok(()) Ok(())
} }

View file

@ -121,7 +121,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
ws.gctx() ws.gctx()
.shell() .shell()
.set_verbosity(cargo::core::Verbosity::Quiet); .set_verbosity(cargo::core::Verbosity::Quiet);
let resolve = resolve_ws(&ws); let resolve = resolve_ws(&ws, dry_run);
ws.gctx().shell().set_verbosity(verbosity); ws.gctx().shell().set_verbosity(verbosity);
resolve?.1 resolve?.1
}; };
@ -129,7 +129,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
// Attempt to gc unused patches and re-resolve if anything is removed // Attempt to gc unused patches and re-resolve if anything is removed
if gc_unused_patches(&workspace, &resolve)? { if gc_unused_patches(&workspace, &resolve)? {
let ws = args.workspace(gctx)?; let ws = args.workspace(gctx)?;
resolve_ws(&ws)?; resolve_ws(&ws, dry_run)?;
} }
} }
Ok(()) Ok(())

View file

@ -149,6 +149,7 @@ pub fn resolve_std<'gctx>(
let cli_features = CliFeatures::from_command_line( let cli_features = CliFeatures::from_command_line(
&features, /*all_features*/ false, /*uses_default_features*/ false, &features, /*all_features*/ false, /*uses_default_features*/ false,
)?; )?;
let dry_run = false;
let resolve = ops::resolve_ws_with_opts( let resolve = ops::resolve_ws_with_opts(
&std_ws, &std_ws,
target_data, target_data,
@ -157,6 +158,7 @@ pub fn resolve_std<'gctx>(
&specs, &specs,
HasDevUnits::No, HasDevUnits::No,
crate::core::resolver::features::ForceAllTargets::No, crate::core::resolver::features::ForceAllTargets::No,
dry_run,
)?; )?;
Ok(( Ok((
resolve.pkg_set, resolve.pkg_set,

View file

@ -77,7 +77,14 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
if opts.spec.is_empty() { if opts.spec.is_empty() {
clean_ctx.remove_paths(&[target_dir.into_path_unlocked()])?; clean_ctx.remove_paths(&[target_dir.into_path_unlocked()])?;
} else { } else {
clean_specs(&mut clean_ctx, &ws, &profiles, &opts.targets, &opts.spec)?; clean_specs(
&mut clean_ctx,
&ws,
&profiles,
&opts.targets,
&opts.spec,
opts.dry_run,
)?;
} }
} }
@ -91,11 +98,12 @@ fn clean_specs(
profiles: &Profiles, profiles: &Profiles,
targets: &[String], targets: &[String],
spec: &[String], spec: &[String],
dry_run: bool,
) -> CargoResult<()> { ) -> CargoResult<()> {
// Clean specific packages. // Clean specific packages.
let requested_kinds = CompileKind::from_requested_targets(clean_ctx.gctx, targets)?; let requested_kinds = CompileKind::from_requested_targets(clean_ctx.gctx, targets)?;
let target_data = RustcTargetData::new(ws, &requested_kinds)?; let target_data = RustcTargetData::new(ws, &requested_kinds)?;
let (pkg_set, resolve) = ops::resolve_ws(ws)?; let (pkg_set, resolve) = ops::resolve_ws(ws, dry_run)?;
let prof_dir_name = profiles.get_dir_name(); let prof_dir_name = profiles.get_dir_name();
let host_layout = Layout::new(ws, None, &prof_dir_name)?; let host_layout = Layout::new(ws, None, &prof_dir_name)?;
// Convert requested kinds to a Vec of layouts. // Convert requested kinds to a Vec of layouts.

View file

@ -264,6 +264,7 @@ pub fn create_bcx<'a, 'gctx>(
HasDevUnits::No HasDevUnits::No
} }
}; };
let dry_run = false;
let resolve = ops::resolve_ws_with_opts( let resolve = ops::resolve_ws_with_opts(
ws, ws,
&mut target_data, &mut target_data,
@ -272,6 +273,7 @@ pub fn create_bcx<'a, 'gctx>(
&specs, &specs,
has_dev_units, has_dev_units,
crate::core::resolver::features::ForceAllTargets::No, crate::core::resolver::features::ForceAllTargets::No,
dry_run,
)?; )?;
let WorkspaceResolve { let WorkspaceResolve {
mut pkg_set, mut pkg_set,

View file

@ -19,7 +19,8 @@ pub fn fetch<'a>(
options: &FetchOptions<'a>, options: &FetchOptions<'a>,
) -> CargoResult<(Resolve, PackageSet<'a>)> { ) -> CargoResult<(Resolve, PackageSet<'a>)> {
ws.emit_warnings()?; ws.emit_warnings()?;
let (mut packages, resolve) = ops::resolve_ws(ws)?; let dry_run = false;
let (mut packages, resolve) = ops::resolve_ws(ws, dry_run)?;
let jobs = Some(JobsConfig::Integer(1)); let jobs = Some(JobsConfig::Integer(1));
let keep_going = false; let keep_going = false;

View file

@ -561,7 +561,8 @@ impl<'gctx> InstallablePackage<'gctx> {
// It would be best if `source` could be passed in here to avoid a // It would be best if `source` could be passed in here to avoid a
// duplicate "Updating", but since `source` is taken by value, then it // duplicate "Updating", but since `source` is taken by value, then it
// wouldn't be available for `compile_ws`. // wouldn't be available for `compile_ws`.
let (pkg_set, resolve) = ops::resolve_ws(&self.ws)?; let dry_run = false;
let (pkg_set, resolve) = ops::resolve_ws(&self.ws, dry_run)?;
ops::check_yanked( ops::check_yanked(
self.ws.gctx(), self.ws.gctx(),
&pkg_set, &pkg_set,

View file

@ -142,6 +142,7 @@ fn build_resolve_graph(
// Note that even with --filter-platform we end up downloading host dependencies as well, // Note that even with --filter-platform we end up downloading host dependencies as well,
// as that is the behavior of download_accessible. // as that is the behavior of download_accessible.
let dry_run = false;
let ws_resolve = ops::resolve_ws_with_opts( let ws_resolve = ops::resolve_ws_with_opts(
ws, ws,
&mut target_data, &mut target_data,
@ -150,6 +151,7 @@ fn build_resolve_graph(
&specs, &specs,
HasDevUnits::Yes, HasDevUnits::Yes,
force_all, force_all,
dry_run,
)?; )?;
let package_map: BTreeMap<PackageId, Package> = ws_resolve let package_map: BTreeMap<PackageId, Package> = ws_resolve

View file

@ -190,7 +190,8 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option
if ws.root().join("Cargo.lock").exists() { if ws.root().join("Cargo.lock").exists() {
// Make sure the Cargo.lock is up-to-date and valid. // Make sure the Cargo.lock is up-to-date and valid.
let _ = ops::resolve_ws(ws)?; let dry_run = false;
let _ = ops::resolve_ws(ws, dry_run)?;
// If Cargo.lock does not exist, it will be generated by `build_lock` // If Cargo.lock does not exist, it will be generated by `build_lock`
// below, and will be validated during the verification step. // below, and will be validated during the verification step.
} }

View file

@ -506,6 +506,7 @@ fn check_resolver_change<'gctx>(
assert_eq!(ws.resolve_behavior(), ResolveBehavior::V1); assert_eq!(ws.resolve_behavior(), ResolveBehavior::V1);
let specs = opts.compile_opts.spec.to_package_id_specs(ws)?; let specs = opts.compile_opts.spec.to_package_id_specs(ws)?;
let mut resolve_differences = |has_dev_units| -> CargoResult<(WorkspaceResolve<'_>, DiffMap)> { let mut resolve_differences = |has_dev_units| -> CargoResult<(WorkspaceResolve<'_>, DiffMap)> {
let dry_run = false;
let ws_resolve = ops::resolve_ws_with_opts( let ws_resolve = ops::resolve_ws_with_opts(
ws, ws,
target_data, target_data,
@ -514,6 +515,7 @@ fn check_resolver_change<'gctx>(
&specs, &specs,
has_dev_units, has_dev_units,
crate::core::resolver::features::ForceAllTargets::No, crate::core::resolver::features::ForceAllTargets::No,
dry_run,
)?; )?;
let feature_opts = FeatureOpts::new_behavior(ResolveBehavior::V2, has_dev_units); let feature_opts = FeatureOpts::new_behavior(ResolveBehavior::V2, has_dev_units);

View file

@ -115,9 +115,9 @@ version. This may also occur with an optional dependency that is not enabled.";
/// ///
/// This is a simple interface used by commands like `clean`, `fetch`, and /// This is a simple interface used by commands like `clean`, `fetch`, and
/// `package`, which don't specify any options or features. /// `package`, which don't specify any options or features.
pub fn resolve_ws<'a>(ws: &Workspace<'a>) -> CargoResult<(PackageSet<'a>, Resolve)> { pub fn resolve_ws<'a>(ws: &Workspace<'a>, dry_run: bool) -> CargoResult<(PackageSet<'a>, Resolve)> {
let mut registry = PackageRegistry::new(ws.gctx())?; let mut registry = PackageRegistry::new(ws.gctx())?;
let resolve = resolve_with_registry(ws, &mut registry)?; let resolve = resolve_with_registry(ws, &mut registry, dry_run)?;
let packages = get_resolved_packages(&resolve, registry)?; let packages = get_resolved_packages(&resolve, registry)?;
Ok((packages, resolve)) Ok((packages, resolve))
} }
@ -140,6 +140,7 @@ pub fn resolve_ws_with_opts<'gctx>(
specs: &[PackageIdSpec], specs: &[PackageIdSpec],
has_dev_units: HasDevUnits, has_dev_units: HasDevUnits,
force_all_targets: ForceAllTargets, force_all_targets: ForceAllTargets,
dry_run: bool,
) -> CargoResult<WorkspaceResolve<'gctx>> { ) -> CargoResult<WorkspaceResolve<'gctx>> {
let mut registry = PackageRegistry::new(ws.gctx())?; let mut registry = PackageRegistry::new(ws.gctx())?;
let (resolve, resolved_with_overrides) = if ws.ignore_lock() { let (resolve, resolved_with_overrides) = if ws.ignore_lock() {
@ -160,7 +161,7 @@ pub fn resolve_ws_with_opts<'gctx>(
} else if ws.require_optional_deps() { } else if ws.require_optional_deps() {
// First, resolve the root_package's *listed* dependencies, as well as // First, resolve the root_package's *listed* dependencies, as well as
// downloading and updating all remotes and such. // downloading and updating all remotes and such.
let resolve = resolve_with_registry(ws, &mut registry)?; let resolve = resolve_with_registry(ws, &mut registry, dry_run)?;
// No need to add patches again, `resolve_with_registry` has done it. // No need to add patches again, `resolve_with_registry` has done it.
let add_patches = false; let add_patches = false;
@ -269,6 +270,7 @@ pub fn resolve_ws_with_opts<'gctx>(
fn resolve_with_registry<'gctx>( fn resolve_with_registry<'gctx>(
ws: &Workspace<'gctx>, ws: &Workspace<'gctx>,
registry: &mut PackageRegistry<'gctx>, registry: &mut PackageRegistry<'gctx>,
dry_run: bool,
) -> CargoResult<Resolve> { ) -> CargoResult<Resolve> {
let prev = ops::load_pkg_lockfile(ws)?; let prev = ops::load_pkg_lockfile(ws)?;
let mut resolve = resolve_with_previous( let mut resolve = resolve_with_previous(
@ -283,7 +285,11 @@ fn resolve_with_registry<'gctx>(
)?; )?;
let print = if !ws.is_ephemeral() && ws.require_optional_deps() { let print = if !ws.is_ephemeral() && ws.require_optional_deps() {
ops::write_pkg_lockfile(ws, &mut resolve)? if !dry_run {
ops::write_pkg_lockfile(ws, &mut resolve)?
} else {
true
}
} else { } else {
// This mostly represents // This mostly represents
// - `cargo install --locked` and the only change is the package is no longer local but // - `cargo install --locked` and the only change is the package is no longer local but

View file

@ -131,6 +131,7 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
} else { } else {
ForceAllTargets::No ForceAllTargets::No
}; };
let dry_run = false;
let ws_resolve = ops::resolve_ws_with_opts( let ws_resolve = ops::resolve_ws_with_opts(
ws, ws,
&mut target_data, &mut target_data,
@ -139,6 +140,7 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
&specs, &specs,
has_dev, has_dev,
force_all, force_all,
dry_run,
)?; )?;
let package_map: HashMap<PackageId, &Package> = ws_resolve let package_map: HashMap<PackageId, &Package> = ws_resolve

View file

@ -80,6 +80,7 @@ fn sync(
workspaces: &[&Workspace<'_>], workspaces: &[&Workspace<'_>],
opts: &VendorOptions<'_>, opts: &VendorOptions<'_>,
) -> CargoResult<VendorConfig> { ) -> CargoResult<VendorConfig> {
let dry_run = false;
let canonical_destination = try_canonicalize(opts.destination); let canonical_destination = try_canonicalize(opts.destination);
let canonical_destination = canonical_destination.as_deref().unwrap_or(opts.destination); let canonical_destination = canonical_destination.as_deref().unwrap_or(opts.destination);
let dest_dir_already_exists = canonical_destination.exists(); let dest_dir_already_exists = canonical_destination.exists();
@ -112,7 +113,7 @@ fn sync(
// crate to work with. // crate to work with.
for ws in workspaces { for ws in workspaces {
let (packages, resolve) = let (packages, resolve) =
ops::resolve_ws(ws).with_context(|| "failed to load pkg lockfile")?; ops::resolve_ws(ws, dry_run).with_context(|| "failed to load pkg lockfile")?;
packages packages
.get_many(resolve.iter()) .get_many(resolve.iter())
@ -144,7 +145,7 @@ fn sync(
// tables about them. // tables about them.
for ws in workspaces { for ws in workspaces {
let (packages, resolve) = let (packages, resolve) =
ops::resolve_ws(ws).with_context(|| "failed to load pkg lockfile")?; ops::resolve_ws(ws, dry_run).with_context(|| "failed to load pkg lockfile")?;
packages packages
.get_many(resolve.iter()) .get_many(resolve.iter())