Skip to content

Commit d1956e4

Browse files
William Pagecompenguy
William Page
authored andcommitted
Add '--directory,-C' flag for changing current dir before build
This implements the suggestion in #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 #2930.
1 parent 5e4f035 commit d1956e4

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/bin/cargo/cli.rs

Lines changed: 17 additions & 1 deletion
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,15 @@ lazy_static::lazy_static! {
2727
pub fn main(config: &mut LazyConfig) -> CliResult {
2828
let args = cli().try_get_matches()?;
2929

30+
if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") {
31+
// Change the configured directory to work in, before any config files are read
32+
assert_eq!(config.is_init(), false);
33+
let config = config.get_mut();
34+
config
35+
.set_cwd(new_cwd.as_path())
36+
.with_context(|| "could not change to requested directory")?;
37+
}
38+
3039
// CAUTION: Be careful with using `config` until it is configured below.
3140
// In general, try to avoid loading config values unless necessary (like
3241
// the [alias] table).
@@ -467,6 +476,13 @@ See 'cargo help <command>' for more information on a specific command.\n",
467476
.value_name("WHEN")
468477
.global(true),
469478
)
479+
.arg(
480+
opt("directory", "Change to DIRECTORY before doing anything")
481+
.short('C')
482+
.value_name("DIRECTORY")
483+
.value_hint(clap::ValueHint::DirPath)
484+
.value_parser(clap::builder::ValueParser::path_buf()),
485+
)
470486
.arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true))
471487
.arg(flag("locked", "Require Cargo.lock is up to date").global(true))
472488
.arg(flag("offline", "Run without accessing the network").global(true))

src/cargo/util/config/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,23 @@ impl Config {
551551
Ok(())
552552
}
553553

554+
/// Updates the application's notion of current working directory, both in
555+
/// the process environment as well as the configuration data
556+
pub fn set_cwd(&mut self, new_cwd: &Path) -> CargoResult<()> {
557+
// Update the process-level notion of cwd
558+
std::env::set_current_dir(&new_cwd)?;
559+
// Update struct members derived from the process-wide cwd
560+
// processing occurs
561+
self.cwd = new_cwd.to_path_buf();
562+
self.home_path = Filesystem::new(homedir(new_cwd).ok_or_else(|| {
563+
anyhow!(
564+
"Cargo couldn't find your home directory. \
565+
This probably means that $HOME was not set."
566+
)
567+
})?);
568+
Ok(())
569+
}
570+
554571
/// The current working directory.
555572
pub fn cwd(&self) -> &Path {
556573
&self.cwd

0 commit comments

Comments
 (0)