Skip to content

Add ensure option to cargo install #6592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ pub fn cli() -> App {
))
.arg_jobs()
.arg(opt("force", "Force overwriting existing crates or binaries").short("f"))
.arg(
opt(
"ensure",
"Simply leaves it as-is when you already have a suitable version",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the optional help wording

)
.short("e"),
)
.arg_features()
.arg(opt("debug", "Build in debug mode instead of release mode"))
.arg_targets_bins_examples(
Expand Down Expand Up @@ -128,6 +135,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
version,
&compile_opts,
args.is_present("force"),
args.is_present("ensure"),
)?;
}
Ok(())
Expand Down
15 changes: 13 additions & 2 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub fn install(
vers: Option<&str>,
opts: &ops::CompileOptions<'_>,
force: bool,
ensure: bool,
) -> CargoResult<()> {
let root = resolve_root(root, opts.config)?;
let map = SourceConfigMap::new(opts.config)?;
Expand All @@ -56,6 +57,7 @@ pub fn install(
vers,
opts,
force,
ensure,
true,
)?;
(true, false)
Expand All @@ -75,6 +77,7 @@ pub fn install(
vers,
opts,
force,
ensure,
first,
) {
Ok(()) => succeeded.push(krate),
Expand Down Expand Up @@ -137,6 +140,7 @@ fn install_one(
vers: Option<&str>,
opts: &ops::CompileOptions<'_>,
force: bool,
ensure: bool,
is_first_install: bool,
) -> CargoResult<()> {
let config = opts.config;
Expand Down Expand Up @@ -248,7 +252,7 @@ fn install_one(
let metadata = metadata(config, root)?;
let list = read_crate_list(&metadata)?;
let dst = metadata.parent().join("bin");
check_overwrites(&dst, pkg, &opts.filter, &list, force)?;
check_overwrites(&dst, pkg, &opts.filter, &list, force, ensure)?;
}

let exec: Arc<dyn Executor> = Arc::new(DefaultExecutor);
Expand Down Expand Up @@ -287,7 +291,7 @@ fn install_one(
let metadata = metadata(config, root)?;
let mut list = read_crate_list(&metadata)?;
let dst = metadata.parent().join("bin");
let duplicates = check_overwrites(&dst, pkg, &opts.filter, &list, force)?;
let duplicates = check_overwrites(&dst, pkg, &opts.filter, &list, force, ensure)?;

fs::create_dir_all(&dst)?;

Expand Down Expand Up @@ -411,6 +415,7 @@ fn check_overwrites(
filter: &ops::CompileFilter,
prev: &CrateListingV1,
force: bool,
ensure: bool,
) -> CargoResult<BTreeMap<String, Option<PackageId>>> {
// If explicit --bin or --example flags were passed then those'll
// get checked during cargo_compile, we only care about the "build
Expand All @@ -433,6 +438,12 @@ fn check_overwrites(
}
}
msg.push_str("Add --force to overwrite");

if ensure {
eprintln!("{}", msg);
std::process::exit(0)
};

Err(failure::format_err!("{}", msg))
}

Expand Down
21 changes: 21 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,27 @@ foo v0.2.0 ([..]):
.run();
}

#[test]
fn install_ensure() {
let p = project()
.file("src/bin/foo-bin1.rs", "fn main() {}")
.file("src/bin/foo-bin2.rs", "fn main() {}")
.build();

cargo_process("install --ensure --path").arg(p.root()).run();
cargo_process("install --ensure --path")
.arg(p.root())
.with_stderr(
"\
[INSTALLING] foo v0.0.1 [..]
binary `foo-bin1[..]` already exists in destination as part of `foo v0.0.1 ([..])`
binary `foo-bin2[..]` already exists in destination as part of `foo v0.0.1 ([..])`
Add --force to overwrite
",
)
.run();
}

#[test]
fn install_force_partial_overlap() {
let p = project()
Expand Down