From bd6203911249150fad1eb7472c71053b2e585f18 Mon Sep 17 00:00:00 2001 From: Flowrey Date: Mon, 22 Jul 2024 20:39:59 +0200 Subject: [PATCH] feat: add --dry-run to install command --- src/bin/cargo/commands/install.rs | 6 +- src/cargo/core/compiler/build_config.rs | 3 + src/cargo/ops/cargo_compile/mod.rs | 3 + src/cargo/ops/cargo_install.rs | 12 +-- .../cargo_install/help/stdout.term.svg | 82 ++++++++++--------- tests/testsuite/install.rs | 10 +-- 6 files changed, 63 insertions(+), 53 deletions(-) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 8aaaeb87b0e4..dabdc79c3539 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -69,6 +69,7 @@ pub fn cli() -> Command { ) .arg(opt("root", "Directory to install packages into").value_name("DIR")) .arg(flag("force", "Force overwriting existing crates or binaries").short('f')) + .arg_dry_run("Perform all checks without installing (unstable)") .arg(flag("no-track", "Do not save tracking information")) .arg(flag( "list", @@ -200,7 +201,9 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { compile_opts.build_config.requested_profile = args.get_profile_name("release", ProfileChecking::Custom)?; - + if args.dry_run() { + gctx.cli_unstable().fail_if_stable_opt("--dry-run", 11123)?; + } if args.flag("list") { ops::install_list(root, gctx)?; } else { @@ -213,6 +216,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { &compile_opts, args.flag("force"), args.flag("no-track"), + args.dry_run(), )?; } Ok(()) diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index 0f66d6dbbc66..4c804f27b681 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -31,6 +31,8 @@ pub struct BuildConfig { pub build_plan: bool, /// Output the unit graph to stdout instead of actually compiling. pub unit_graph: bool, + /// `true` to avoid really compiling. + pub dry_run: bool, /// An optional override of the rustc process for primary units pub primary_unit_rustc: Option, /// A thread used by `cargo fix` to receive messages on a socket regarding @@ -112,6 +114,7 @@ impl BuildConfig { force_rebuild: false, build_plan: false, unit_graph: false, + dry_run: false, primary_unit_rustc: None, rustfix_diagnostic_server: Rc::new(RefCell::new(None)), export_dir: None, diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index 77f6266355ff..ba43e4c5fb96 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -154,6 +154,9 @@ pub fn compile_ws<'a>( unit_graph::emit_serialized_unit_graph(&bcx.roots, &bcx.unit_graph, ws.gctx())?; return Compilation::new(&bcx); } + if options.build_config.dry_run { + return Compilation::new(&bcx); + } crate::core::gc::auto_gc(bcx.gctx); let build_runner = BuildRunner::new(&bcx)?; build_runner.compile(exec) diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index 7abc1b393757..c8b27c929ce6 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -297,7 +297,7 @@ impl<'gctx> InstallablePackage<'gctx> { Ok(duplicates) } - fn install_one(mut self) -> CargoResult { + fn install_one(mut self, dry_run: bool) -> CargoResult { self.gctx.shell().status("Installing", &self.pkg)?; let dst = self.root.join("bin").into_path_unlocked(); @@ -321,6 +321,7 @@ impl<'gctx> InstallablePackage<'gctx> { self.check_yanked_install()?; let exec: Arc = Arc::new(DefaultExecutor); + self.opts.build_config.dry_run = dry_run; let compile = ops::compile_ws(&self.ws, &self.opts, &exec).with_context(|| { if let Some(td) = td_opt.take() { // preserve the temporary directory, so the user can inspect it @@ -385,7 +386,7 @@ impl<'gctx> InstallablePackage<'gctx> { .iter() .filter(|t| t.is_executable()) .collect(); - if !binaries.is_empty() { + if !binaries.is_empty() && !dry_run { self.gctx .shell() .warn(make_warning_about_missing_features(&binaries))?; @@ -620,6 +621,7 @@ pub fn install( opts: &ops::CompileOptions, force: bool, no_track: bool, + dry_run: bool, ) -> CargoResult<()> { let root = resolve_root(root, gctx)?; let dst = root.join("bin").into_path_unlocked(); @@ -654,7 +656,7 @@ pub fn install( )?; let mut installed_anything = true; if let Some(installable_pkg) = installable_pkg { - installed_anything = installable_pkg.install_one()?; + installed_anything = installable_pkg.install_one(dry_run)?; } (installed_anything, false) } else { @@ -705,7 +707,7 @@ pub fn install( let install_results: Vec<_> = pkgs_to_install .into_iter() - .map(|(krate, installable_pkg)| (krate, installable_pkg.install_one())) + .map(|(krate, installable_pkg)| (krate, installable_pkg.install_one(dry_run))) .collect(); for (krate, result) in install_results { @@ -745,7 +747,7 @@ pub fn install( let path = gctx.get_env_os("PATH").unwrap_or_default(); let dst_in_path = env::split_paths(&path).any(|path| path == dst); - if !dst_in_path { + if !dst_in_path && !dry_run { gctx.shell().warn(&format!( "be sure to add `{}` to your PATH to be \ able to run the installed binaries", diff --git a/tests/testsuite/cargo_install/help/stdout.term.svg b/tests/testsuite/cargo_install/help/stdout.term.svg index e021922563f3..38ef23809e74 100644 --- a/tests/testsuite/cargo_install/help/stdout.term.svg +++ b/tests/testsuite/cargo_install/help/stdout.term.svg @@ -1,4 +1,4 @@ - +