Skip to content

Commit 7404367

Browse files
committed
Auto merge of #10952 - compenguy:cargo-change-dir-10098, r=epage
Add '-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. The behavior of this new flag makes cargo build function exactly the same when run at the root of the project as if run elsewhere outside of the project. This is in contrast to `--manifest-path`, which, for example, results in a different series of directories being searched for `.cargo/config.toml` between the two cases. Fixes #10098 Reduces impact of #2930 for many, possibly all impacted, by switching to this new cli argument. ### How should we test and review this PR? The easiest way to reproduce the issue described in #2930 is to create an invalid `.cargo/config.toml` file in the root of a cargo project, for example `!` as the contents of the file. Running cargo with the current working directory underneath the root of that project will quickly fail with an error, showing that the config file was processed. This is correct and expected behavior. Running the the same build with the current working directory set outside of the project, e.g. /tmp, and passing the `--manifest-path /path/to/project/Cargo.toml`. The build will proceed without erroring as a result of reading the project's `.cargo/config.toml`, because config file searching is done from cwd (e.g. in `/tmp` and `/`) without including the project root, which is a surprising result in the context of the [cargo config documentation](https://doc.rust-lang.org/cargo/reference/config.html), which suggests that a `.cargo/config.toml` file checked into the root of a project's revision control will be processed during the build of that project. Finally to demonstrate that this PR results in the expected behavior, run cargo similar to the previous run, from /tmp or similar, but instead of `--manifest-path /path/to/project/Cargo.toml`, pass `-C/path/to/project` to cargo (note the missing `Cargo.toml` at the end). The build will provide the exact same (expected error) behavior as when running it within the project root directory. ### Additional information ~Passing a path with a trailing '/' will result in failure. It is unclear whether this is a result of improper input sanitization, or whether the config.cwd value is being improperly handled on output. In either case this needs to be resolved before this PR is merge-ready.~ (the above issue appears to have been a side effect of local corruption of my rustup toolchain, and unrelated to this change) Because a `struct Config` gets created before command line arguments are processed, a config will exist with the actual cwd recorded, and it must then be replaced with the new value after command line arguments are processed but before anything tries to use the stored cwd value or any other value derived from it for anything. This change effectively creates a difficult-to-document requirement during cargo initialization regarding the order of events. For example, should a setting stored in a config file discovered via cwd+ancestors search be wanted before argument processing happens, this could result in unpleasant surprises in the exact use case this feature is being added to fix. A long flag was deferred out to not block this on deciding what to name it. A follow up issue will be created.
2 parents a490498 + 6510cc5 commit 7404367

File tree

99 files changed

+675
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+675
-1
lines changed

src/bin/cargo/cli.rs

Lines changed: 23 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,13 @@ 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+
// This must be completed before config is initialized
32+
assert_eq!(config.is_init(), false);
33+
if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") {
34+
std::env::set_current_dir(&new_cwd).context("could not change to requested directory")?;
35+
}
36+
3037
// CAUTION: Be careful with using `config` until it is configured below.
3138
// In general, try to avoid loading config values unless necessary (like
3239
// the [alias] table).
@@ -467,6 +474,14 @@ See 'cargo help <command>' for more information on a specific command.\n",
467474
.value_name("WHEN")
468475
.global(true),
469476
)
477+
.arg(
478+
Arg::new("directory")
479+
.help("Change to DIRECTORY before doing anything")
480+
.short('C')
481+
.value_name("DIRECTORY")
482+
.value_hint(clap::ValueHint::DirPath)
483+
.value_parser(clap::builder::ValueParser::path_buf()),
484+
)
470485
.arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true))
471486
.arg(flag("locked", "Require Cargo.lock is up to date").global(true))
472487
.arg(flag("offline", "Run without accessing the network").global(true))
@@ -497,6 +512,13 @@ impl LazyConfig {
497512
Self { config: None }
498513
}
499514

515+
/// Check whether the config is loaded
516+
///
517+
/// This is useful for asserts in case the environment needs to be setup before loading
518+
pub fn is_init(&self) -> bool {
519+
self.config.is_some()
520+
}
521+
500522
/// Get the config, loading it if needed
501523
///
502524
/// On error, the process is terminated

src/doc/man/generated_txt/cargo-add.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ OPTIONS
185185
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
186186
for more information.
187187

188+
-C PATH
189+
Changes the current working directory before executing any specified
190+
operations. This affects things like where cargo looks by default
191+
for the project manifest (Cargo.toml), as well as the directories
192+
searched for discovering .cargo/config.toml, for example.
193+
188194
-h, --help
189195
Prints help information.
190196

src/doc/man/generated_txt/cargo-bench.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ OPTIONS
363363
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
364364
for more information.
365365

366+
-C PATH
367+
Changes the current working directory before executing any specified
368+
operations. This affects things like where cargo looks by default
369+
for the project manifest (Cargo.toml), as well as the directories
370+
searched for discovering .cargo/config.toml, for example.
371+
366372
-h, --help
367373
Prints help information.
368374

src/doc/man/generated_txt/cargo-build.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ OPTIONS
312312
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
313313
for more information.
314314

315+
-C PATH
316+
Changes the current working directory before executing any specified
317+
operations. This affects things like where cargo looks by default
318+
for the project manifest (Cargo.toml), as well as the directories
319+
searched for discovering .cargo/config.toml, for example.
320+
315321
-h, --help
316322
Prints help information.
317323

src/doc/man/generated_txt/cargo-check.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ OPTIONS
297297
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
298298
for more information.
299299

300+
-C PATH
301+
Changes the current working directory before executing any specified
302+
operations. This affects things like where cargo looks by default
303+
for the project manifest (Cargo.toml), as well as the directories
304+
searched for discovering .cargo/config.toml, for example.
305+
300306
-h, --help
301307
Prints help information.
302308

src/doc/man/generated_txt/cargo-clean.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ OPTIONS
127127
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
128128
for more information.
129129

130+
-C PATH
131+
Changes the current working directory before executing any specified
132+
operations. This affects things like where cargo looks by default
133+
for the project manifest (Cargo.toml), as well as the directories
134+
searched for discovering .cargo/config.toml, for example.
135+
130136
-h, --help
131137
Prints help information.
132138

src/doc/man/generated_txt/cargo-doc.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ OPTIONS
268268
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
269269
for more information.
270270

271+
-C PATH
272+
Changes the current working directory before executing any specified
273+
operations. This affects things like where cargo looks by default
274+
for the project manifest (Cargo.toml), as well as the directories
275+
searched for discovering .cargo/config.toml, for example.
276+
271277
-h, --help
272278
Prints help information.
273279

src/doc/man/generated_txt/cargo-fetch.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ OPTIONS
112112
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
113113
for more information.
114114

115+
-C PATH
116+
Changes the current working directory before executing any specified
117+
operations. This affects things like where cargo looks by default
118+
for the project manifest (Cargo.toml), as well as the directories
119+
searched for discovering .cargo/config.toml, for example.
120+
115121
-h, --help
116122
Prints help information.
117123

src/doc/man/generated_txt/cargo-fix.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ OPTIONS
370370
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
371371
for more information.
372372

373+
-C PATH
374+
Changes the current working directory before executing any specified
375+
operations. This affects things like where cargo looks by default
376+
for the project manifest (Cargo.toml), as well as the directories
377+
searched for discovering .cargo/config.toml, for example.
378+
373379
-h, --help
374380
Prints help information.
375381

src/doc/man/generated_txt/cargo-generate-lockfile.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ OPTIONS
8787
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
8888
for more information.
8989

90+
-C PATH
91+
Changes the current working directory before executing any specified
92+
operations. This affects things like where cargo looks by default
93+
for the project manifest (Cargo.toml), as well as the directories
94+
searched for discovering .cargo/config.toml, for example.
95+
9096
-h, --help
9197
Prints help information.
9298

0 commit comments

Comments
 (0)