Skip to content

Commit 7dea900

Browse files
William Pagecompenguy
William Page
authored andcommitted
Add '--directory,-C' flag for changing current dir before build
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
1 parent a40a64b commit 7dea900

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/bin/cargo/cli.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::anyhow;
1+
use anyhow::{anyhow, Context as _};
22
use cargo::core::shell::Shell;
33
use cargo::core::{features, CliUnstable};
44
use cargo::{self, drop_print, drop_println, CliResult, Config};
@@ -27,6 +27,12 @@ lazy_static::lazy_static! {
2727
pub fn main(config: &mut LazyConfig) -> CliResult {
2828
let args = cli().try_get_matches()?;
2929

30+
// Update the process-level notion of cwd
31+
if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") {
32+
std::env::set_current_dir(&new_cwd)
33+
.with_context(|| "could not change to requested directory")?;
34+
}
35+
3036
// CAUTION: Be careful with using `config` until it is configured below.
3137
// In general, try to avoid loading config values unless necessary (like
3238
// the [alias] table).
@@ -467,6 +473,13 @@ See 'cargo help <command>' for more information on a specific command.\n",
467473
.value_name("WHEN")
468474
.global(true),
469475
)
476+
.arg(
477+
opt("directory", "Change to DIRECTORY before doing anything")
478+
.short('C')
479+
.value_name("DIRECTORY")
480+
.value_hint(clap::ValueHint::DirPath)
481+
.value_parser(clap::builder::ValueParser::path_buf()),
482+
)
470483
.arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true))
471484
.arg(flag("locked", "Require Cargo.lock is up to date").global(true))
472485
.arg(flag("offline", "Run without accessing the network").global(true))

0 commit comments

Comments
 (0)