Skip to content

Commit 8b87e30

Browse files
committed
Add a unstable --ignore-local-config flag
1 parent 888ae72 commit 8b87e30

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

src/bin/cargo/cli.rs

+9
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ fn execute_subcommand(
218218
cmd: &str,
219219
subcommand_args: &ArgMatches<'_>,
220220
) -> CliResult {
221+
if subcommand_args.is_present("ignore-local-config") {
222+
config.reload_rooted_at(config.home().clone().into_path_unlocked())?;
223+
}
224+
221225
if let Some(exec) = commands::builtin_exec(cmd) {
222226
return exec(config, subcommand_args);
223227
}
@@ -329,6 +333,11 @@ See 'cargo help <command>' for more information on a specific command.\n",
329333
.global(true)
330334
.hidden(true),
331335
)
336+
.arg(
337+
opt("ignore-local-config", "Ignore local `.cargo/config` files")
338+
.global(true)
339+
.hidden(true),
340+
)
332341
.arg(
333342
Arg::with_name("unstable-features")
334343
.help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details")

src/cargo/core/compiler/build_config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct BuildConfig {
3737
// Note that, although the cmd-line flag name is `out-dir`, in code we use
3838
// `export_dir`, to avoid confusion with out dir at `target/debug/deps`.
3939
pub export_dir: Option<PathBuf>,
40+
pub ignore_local_config: bool,
4041
}
4142

4243
impl BuildConfig {
@@ -80,6 +81,7 @@ impl BuildConfig {
8081
primary_unit_rustc: None,
8182
rustfix_diagnostic_server: RefCell::new(None),
8283
export_dir: None,
84+
ignore_local_config: false,
8385
})
8486
}
8587

src/cargo/util/command_prelude.rs

+13
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ pub trait AppExt: Sized {
163163
))
164164
}
165165

166+
fn arg_ignore_local_config(self) -> Self {
167+
self._arg(opt(
168+
"ignore-local-config",
169+
"Ignore local `.cargo/config.toml` files (unstable)",
170+
))
171+
}
172+
166173
fn arg_unit_graph(self) -> Self {
167174
self._arg(opt("unit-graph", "Output build graph in JSON (unstable)").hidden(true))
168175
}
@@ -459,6 +466,7 @@ pub trait ArgMatchesExt {
459466
build_config.requested_profile = self.get_profile_name(config, "dev", profile_checking)?;
460467
build_config.build_plan = self._is_present("build-plan");
461468
build_config.unit_graph = self._is_present("unit-graph");
469+
build_config.ignore_local_config = self._is_present("ignore-local-config");
462470
if build_config.build_plan {
463471
config
464472
.cli_unstable()
@@ -469,6 +477,11 @@ pub trait ArgMatchesExt {
469477
.cli_unstable()
470478
.fail_if_stable_opt("--unit-graph", 8002)?;
471479
}
480+
if build_config.ignore_local_config {
481+
config
482+
.cli_unstable()
483+
.fail_if_stable_opt("--ignore-local-config", 0000)?;
484+
}
472485

473486
let opts = CompileOptions {
474487
build_config,

tests/testsuite/config.rs

+46
Original file line numberDiff line numberDiff line change
@@ -1483,3 +1483,49 @@ Caused by:
14831483
unknown variant `invalid`, expected one of `debuginfo`, `none`, `symbols`",
14841484
);
14851485
}
1486+
1487+
#[cargo_test]
1488+
fn ignore_local_config_gated() {
1489+
// Requires -Zunstable-options
1490+
let p = project().file("src/lib.rs", "").build();
1491+
1492+
p.cargo("build --ignore-local-config")
1493+
.with_status(101)
1494+
.with_stderr(
1495+
"\
1496+
[ERROR] the `--ignore-local-config` flag is unstable, and only available \
1497+
on the nightly channel of Cargo, [..]
1498+
See [..]
1499+
See [..]
1500+
",
1501+
)
1502+
.run();
1503+
1504+
p.cargo("build --ignore-local-config")
1505+
.masquerade_as_nightly_cargo()
1506+
.with_status(101)
1507+
.with_stderr(
1508+
"\
1509+
[ERROR] the `--ignore-local-config` flag is unstable, pass `-Z unstable-options` to enable it
1510+
See [..]
1511+
",
1512+
)
1513+
.run();
1514+
}
1515+
1516+
#[cargo_test]
1517+
fn ignore_local_config_basic() {
1518+
let p = project()
1519+
.file("src/lib.rs", "")
1520+
.file(
1521+
".cargo/config",
1522+
r#"
1523+
[build]
1524+
target = "non-existent"
1525+
"#,
1526+
)
1527+
.build();
1528+
p.cargo("build -v --ignore-local-config -Z unstable-options")
1529+
.masquerade_as_nightly_cargo()
1530+
.run();
1531+
}

0 commit comments

Comments
 (0)