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

+23-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,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

+6
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

+6
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

+6
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

+6
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

+6
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

+6
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

+6
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

+6
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

+6
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

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

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ OPTIONS
9595
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
9696
for more information.
9797

98+
-C PATH
99+
Changes the current working directory before executing any specified
100+
operations. This affects things like where cargo looks by default
101+
for the project manifest (Cargo.toml), as well as the directories
102+
searched for discovering .cargo/config.toml, for example.
103+
98104
-h, --help
99105
Prints help information.
100106

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

+6
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ OPTIONS
334334
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
335335
for more information.
336336

337+
-C PATH
338+
Changes the current working directory before executing any specified
339+
operations. This affects things like where cargo looks by default
340+
for the project manifest (Cargo.toml), as well as the directories
341+
searched for discovering .cargo/config.toml, for example.
342+
337343
-h, --help
338344
Prints help information.
339345

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

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ OPTIONS
7777
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
7878
for more information.
7979

80+
-C PATH
81+
Changes the current working directory before executing any specified
82+
operations. This affects things like where cargo looks by default
83+
for the project manifest (Cargo.toml), as well as the directories
84+
searched for discovering .cargo/config.toml, for example.
85+
8086
-h, --help
8187
Prints help information.
8288

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

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ OPTIONS
7070
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
7171
for more information.
7272

73+
-C PATH
74+
Changes the current working directory before executing any specified
75+
operations. This affects things like where cargo looks by default
76+
for the project manifest (Cargo.toml), as well as the directories
77+
searched for discovering .cargo/config.toml, for example.
78+
7379
-h, --help
7480
Prints help information.
7581

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

+6
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ OPTIONS
400400
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
401401
for more information.
402402

403+
-C PATH
404+
Changes the current working directory before executing any specified
405+
operations. This affects things like where cargo looks by default
406+
for the project manifest (Cargo.toml), as well as the directories
407+
searched for discovering .cargo/config.toml, for example.
408+
403409
-h, --help
404410
Prints help information.
405411

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

+6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ OPTIONS
9090
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
9191
for more information.
9292

93+
-C PATH
94+
Changes the current working directory before executing any specified
95+
operations. This affects things like where cargo looks by default
96+
for the project manifest (Cargo.toml), as well as the directories
97+
searched for discovering .cargo/config.toml, for example.
98+
9399
-h, --help
94100
Prints help information.
95101

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

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ OPTIONS
9797
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
9898
for more information.
9999

100+
-C PATH
101+
Changes the current working directory before executing any specified
102+
operations. This affects things like where cargo looks by default
103+
for the project manifest (Cargo.toml), as well as the directories
104+
searched for discovering .cargo/config.toml, for example.
105+
100106
-h, --help
101107
Prints help information.
102108

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

+6
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ OPTIONS
238238
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
239239
for more information.
240240

241+
-C PATH
242+
Changes the current working directory before executing any specified
243+
operations. This affects things like where cargo looks by default
244+
for the project manifest (Cargo.toml), as well as the directories
245+
searched for discovering .cargo/config.toml, for example.
246+
241247
-h, --help
242248
Prints help information.
243249

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

+6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ OPTIONS
117117
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
118118
for more information.
119119

120+
-C PATH
121+
Changes the current working directory before executing any specified
122+
operations. This affects things like where cargo looks by default
123+
for the project manifest (Cargo.toml), as well as the directories
124+
searched for discovering .cargo/config.toml, for example.
125+
120126
-h, --help
121127
Prints help information.
122128

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

+6
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ OPTIONS
205205
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
206206
for more information.
207207

208+
-C PATH
209+
Changes the current working directory before executing any specified
210+
operations. This affects things like where cargo looks by default
211+
for the project manifest (Cargo.toml), as well as the directories
212+
searched for discovering .cargo/config.toml, for example.
213+
208214
-h, --help
209215
Prints help information.
210216

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

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ OPTIONS
103103
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
104104
for more information.
105105

106+
-C PATH
107+
Changes the current working directory before executing any specified
108+
operations. This affects things like where cargo looks by default
109+
for the project manifest (Cargo.toml), as well as the directories
110+
searched for discovering .cargo/config.toml, for example.
111+
106112
-h, --help
107113
Prints help information.
108114

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

+6
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ OPTIONS
212212
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
213213
for more information.
214214

215+
-C PATH
216+
Changes the current working directory before executing any specified
217+
operations. This affects things like where cargo looks by default
218+
for the project manifest (Cargo.toml), as well as the directories
219+
searched for discovering .cargo/config.toml, for example.
220+
215221
-h, --help
216222
Prints help information.
217223

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

+6
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ OPTIONS
314314
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
315315
for more information.
316316

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

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

+6
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ OPTIONS
284284
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
285285
for more information.
286286

287+
-C PATH
288+
Changes the current working directory before executing any specified
289+
operations. This affects things like where cargo looks by default
290+
for the project manifest (Cargo.toml), as well as the directories
291+
searched for discovering .cargo/config.toml, for example.
292+
287293
-h, --help
288294
Prints help information.
289295

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

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ OPTIONS
6767
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
6868
for more information.
6969

70+
-C PATH
71+
Changes the current working directory before executing any specified
72+
operations. This affects things like where cargo looks by default
73+
for the project manifest (Cargo.toml), as well as the directories
74+
searched for discovering .cargo/config.toml, for example.
75+
7076
-h, --help
7177
Prints help information.
7278

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

+6
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ OPTIONS
381381
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
382382
for more information.
383383

384+
-C PATH
385+
Changes the current working directory before executing any specified
386+
operations. This affects things like where cargo looks by default
387+
for the project manifest (Cargo.toml), as well as the directories
388+
searched for discovering .cargo/config.toml, for example.
389+
384390
-h, --help
385391
Prints help information.
386392

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

+6
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ OPTIONS
298298
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
299299
for more information.
300300

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

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

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ OPTIONS
7979
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
8080
for more information.
8181

82+
-C PATH
83+
Changes the current working directory before executing any specified
84+
operations. This affects things like where cargo looks by default
85+
for the project manifest (Cargo.toml), as well as the directories
86+
searched for discovering .cargo/config.toml, for example.
87+
8288
-h, --help
8389
Prints help information.
8490

0 commit comments

Comments
 (0)