Skip to content

Commit

Permalink
Add '--directory,-C' flag for changing current dir before build
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
William Page authored and compenguy committed Feb 1, 2023
1 parent 5e4f035 commit d1956e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::anyhow;
use anyhow::{anyhow, Context as _};
use cargo::core::shell::Shell;
use cargo::core::{features, CliUnstable};
use cargo::{self, drop_print, drop_println, CliResult, Config};
Expand Down Expand Up @@ -27,6 +27,15 @@ lazy_static::lazy_static! {
pub fn main(config: &mut LazyConfig) -> CliResult {
let args = cli().try_get_matches()?;

if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") {
// Change the configured directory to work in, before any config files are read
assert_eq!(config.is_init(), false);
let config = config.get_mut();
config
.set_cwd(new_cwd.as_path())
.with_context(|| "could not change to requested directory")?;
}

// CAUTION: Be careful with using `config` until it is configured below.
// In general, try to avoid loading config values unless necessary (like
// the [alias] table).
Expand Down Expand Up @@ -467,6 +476,13 @@ See 'cargo help <command>' for more information on a specific command.\n",
.value_name("WHEN")
.global(true),
)
.arg(
opt("directory", "Change to DIRECTORY before doing anything")
.short('C')
.value_name("DIRECTORY")
.value_hint(clap::ValueHint::DirPath)
.value_parser(clap::builder::ValueParser::path_buf()),
)
.arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true))
.arg(flag("locked", "Require Cargo.lock is up to date").global(true))
.arg(flag("offline", "Run without accessing the network").global(true))
Expand Down
17 changes: 17 additions & 0 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,23 @@ impl Config {
Ok(())
}

/// Updates the application's notion of current working directory, both in
/// the process environment as well as the configuration data
pub fn set_cwd(&mut self, new_cwd: &Path) -> CargoResult<()> {
// Update the process-level notion of cwd
std::env::set_current_dir(&new_cwd)?;
// Update struct members derived from the process-wide cwd
// processing occurs
self.cwd = new_cwd.to_path_buf();
self.home_path = Filesystem::new(homedir(new_cwd).ok_or_else(|| {
anyhow!(
"Cargo couldn't find your home directory. \
This probably means that $HOME was not set."
)
})?);
Ok(())
}

/// The current working directory.
pub fn cwd(&self) -> &Path {
&self.cwd
Expand Down

0 comments on commit d1956e4

Please sign in to comment.