Skip to content

Commit c9e387d

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 c9e387d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/bin/cargo/cli.rs

Lines changed: 15 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};
@@ -32,6 +32,13 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
3232
// the [alias] table).
3333
let config = config.get_mut();
3434

35+
if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") {
36+
// Change the configured directory to work in, before any config files are read
37+
config
38+
.set_cwd(new_cwd.as_path())
39+
.with_context(|| "could not change to requested directory")?;
40+
}
41+
3542
let (expanded_args, global_args) = expand_aliases(config, args, vec![])?;
3643

3744
if expanded_args
@@ -467,6 +474,13 @@ See 'cargo help <command>' for more information on a specific command.\n",
467474
.value_name("WHEN")
468475
.global(true),
469476
)
477+
.arg(
478+
opt("directory", "Change to DIRECTORY before doing anything")
479+
.short('C')
480+
.value_name("DIRECTORY")
481+
.value_hint(clap::ValueHint::DirPath)
482+
.value_parser(clap::builder::ValueParser::path_buf()),
483+
)
470484
.arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true))
471485
.arg(flag("locked", "Require Cargo.lock is up to date").global(true))
472486
.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)