Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add '-C' flag for changing current dir before build #10952

Merged
merged 4 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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