Skip to content

Commit f25a3a0

Browse files
committed
feat: add --dry-run to install command
1 parent 146b383 commit f25a3a0

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/bin/cargo/commands/install.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use cargo_util::paths;
1616
pub fn cli() -> Command {
1717
subcommand("install")
1818
.about("Install a Rust binary")
19+
.arg_dry_run("Perform all checks without installing (unstable)")
1920
.arg(
2021
Arg::new("crate")
2122
.value_name("CRATE[@<VER>]")
@@ -200,7 +201,9 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
200201

201202
compile_opts.build_config.requested_profile =
202203
args.get_profile_name("release", ProfileChecking::Custom)?;
203-
204+
if args.dry_run() {
205+
gctx.cli_unstable().fail_if_stable_opt("--dry-run", 11123)?;
206+
}
204207
if args.flag("list") {
205208
ops::install_list(root, gctx)?;
206209
} else {
@@ -213,6 +216,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
213216
&compile_opts,
214217
args.flag("force"),
215218
args.flag("no-track"),
219+
args.dry_run(),
216220
)?;
217221
}
218222
Ok(())

src/cargo/core/compiler/build_config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct BuildConfig {
3131
pub build_plan: bool,
3232
/// Output the unit graph to stdout instead of actually compiling.
3333
pub unit_graph: bool,
34+
/// `true` to avoid really compiling.
35+
pub dry_run: bool,
3436
/// An optional override of the rustc process for primary units
3537
pub primary_unit_rustc: Option<ProcessBuilder>,
3638
/// A thread used by `cargo fix` to receive messages on a socket regarding
@@ -112,6 +114,7 @@ impl BuildConfig {
112114
force_rebuild: false,
113115
build_plan: false,
114116
unit_graph: false,
117+
dry_run: false,
115118
primary_unit_rustc: None,
116119
rustfix_diagnostic_server: Rc::new(RefCell::new(None)),
117120
export_dir: None,

src/cargo/ops/cargo_compile/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ pub fn compile_ws<'a>(
154154
unit_graph::emit_serialized_unit_graph(&bcx.roots, &bcx.unit_graph, ws.gctx())?;
155155
return Compilation::new(&bcx);
156156
}
157+
if options.build_config.dry_run {
158+
return Compilation::new(&bcx);
159+
}
157160
crate::core::gc::auto_gc(bcx.gctx);
158161
let build_runner = BuildRunner::new(&bcx)?;
159162
build_runner.compile(exec)

src/cargo/ops/cargo_install.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl<'gctx> InstallablePackage<'gctx> {
297297
Ok(duplicates)
298298
}
299299

300-
fn install_one(mut self) -> CargoResult<bool> {
300+
fn install_one(mut self, dry_run: bool) -> CargoResult<bool> {
301301
self.gctx.shell().status("Installing", &self.pkg)?;
302302

303303
let dst = self.root.join("bin").into_path_unlocked();
@@ -321,6 +321,7 @@ impl<'gctx> InstallablePackage<'gctx> {
321321
self.check_yanked_install()?;
322322

323323
let exec: Arc<dyn Executor> = Arc::new(DefaultExecutor);
324+
self.opts.build_config.dry_run = dry_run;
324325
let compile = ops::compile_ws(&self.ws, &self.opts, &exec).with_context(|| {
325326
if let Some(td) = td_opt.take() {
326327
// preserve the temporary directory, so the user can inspect it
@@ -385,7 +386,7 @@ impl<'gctx> InstallablePackage<'gctx> {
385386
.iter()
386387
.filter(|t| t.is_executable())
387388
.collect();
388-
if !binaries.is_empty() {
389+
if !binaries.is_empty() && !dry_run {
389390
self.gctx
390391
.shell()
391392
.warn(make_warning_about_missing_features(&binaries))?;
@@ -620,6 +621,7 @@ pub fn install(
620621
opts: &ops::CompileOptions,
621622
force: bool,
622623
no_track: bool,
624+
dry_run: bool,
623625
) -> CargoResult<()> {
624626
let root = resolve_root(root, gctx)?;
625627
let dst = root.join("bin").into_path_unlocked();
@@ -654,7 +656,7 @@ pub fn install(
654656
)?;
655657
let mut installed_anything = true;
656658
if let Some(installable_pkg) = installable_pkg {
657-
installed_anything = installable_pkg.install_one()?;
659+
installed_anything = installable_pkg.install_one(dry_run)?;
658660
}
659661
(installed_anything, false)
660662
} else {
@@ -705,7 +707,7 @@ pub fn install(
705707

706708
let install_results: Vec<_> = pkgs_to_install
707709
.into_iter()
708-
.map(|(krate, installable_pkg)| (krate, installable_pkg.install_one()))
710+
.map(|(krate, installable_pkg)| (krate, installable_pkg.install_one(dry_run)))
709711
.collect();
710712

711713
for (krate, result) in install_results {
@@ -745,7 +747,7 @@ pub fn install(
745747
let path = gctx.get_env_os("PATH").unwrap_or_default();
746748
let dst_in_path = env::split_paths(&path).any(|path| path == dst);
747749

748-
if !dst_in_path {
750+
if !dst_in_path && !dry_run {
749751
gctx.shell().warn(&format!(
750752
"be sure to add `{}` to your PATH to be \
751753
able to run the installed binaries",

0 commit comments

Comments
 (0)