Skip to content

Commit

Permalink
Auto merge of #10952 - compenguy:cargo-change-dir-10098, r=epage
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Feb 10, 2023
2 parents a490498 + 6510cc5 commit 7404367
Show file tree
Hide file tree
Showing 99 changed files with 675 additions and 1 deletion.
24 changes: 23 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,13 @@ lazy_static::lazy_static! {
pub fn main(config: &mut LazyConfig) -> CliResult {
let args = cli().try_get_matches()?;

// Update the process-level notion of cwd
// This must be completed before config is initialized
assert_eq!(config.is_init(), false);
if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") {
std::env::set_current_dir(&new_cwd).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 +474,14 @@ See 'cargo help <command>' for more information on a specific command.\n",
.value_name("WHEN")
.global(true),
)
.arg(
Arg::new("directory")
.help("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 Expand Up @@ -497,6 +512,13 @@ impl LazyConfig {
Self { config: None }
}

/// Check whether the config is loaded
///
/// This is useful for asserts in case the environment needs to be setup before loading
pub fn is_init(&self) -> bool {
self.config.is_some()
}

/// Get the config, loading it if needed
///
/// On error, the process is terminated
Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-add.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-bench.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-check.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-clean.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-fetch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-fix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-generate-lockfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-init.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-locate-project.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-login.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-owner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-package.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-pkgid.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-publish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-remove.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-rustc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-rustdoc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-search.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/generated_txt/cargo-uninstall.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ OPTIONS
<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
for more information.

-C PATH
Changes the current working directory before executing any specified
operations. This affects things like where cargo looks by default
for the project manifest (Cargo.toml), as well as the directories
searched for discovering .cargo/config.toml, for example.

-h, --help
Prints help information.

Expand Down
Loading

0 comments on commit 7404367

Please sign in to comment.