Skip to content

Commit

Permalink
feat: add --dry-run to install command
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowrey committed Jul 21, 2024
1 parent 146b383 commit f25a3a0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use cargo_util::paths;
pub fn cli() -> Command {
subcommand("install")
.about("Install a Rust binary")
.arg_dry_run("Perform all checks without installing (unstable)")
.arg(
Arg::new("crate")
.value_name("CRATE[@<VER>]")
Expand Down Expand Up @@ -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 {
Expand All @@ -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(())
Expand Down
3 changes: 3 additions & 0 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessBuilder>,
/// A thread used by `cargo fix` to receive messages on a socket regarding
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl<'gctx> InstallablePackage<'gctx> {
Ok(duplicates)
}

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

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

let exec: Arc<dyn Executor> = 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
Expand Down Expand Up @@ -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))?;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit f25a3a0

Please sign in to comment.