Allow setting unstable options in .cargo/config

Obviously this only works with nightlies and all
that, but if you're e.g. testing resolver v2, want
to always have Ztiming on in your workspace, or
something like that, this makes it easier to do.
This commit is contained in:
Alex Berghage 2020-06-21 14:19:37 -06:00
parent 1cea4cbe85
commit 4aede8c784
3 changed files with 90 additions and 3 deletions

View File

@ -63,7 +63,7 @@ use std::str::FromStr;
use std::sync::Once;
use std::time::Instant;
use anyhow::{anyhow, bail};
use anyhow::{anyhow, bail, Context};
use curl::easy::Easy;
use lazycell::LazyCell;
use serde::Deserialize;
@ -742,9 +742,20 @@ impl Config {
.unwrap_or(false);
self.target_dir = cli_target_dir;
// If nightly features are enabled, allow setting Z-flags from config
// using the `unstable` table. Ignore that block otherwise.
if nightly_features_allowed() {
if let Some(val) = self.get::<Option<bool>>("unstable.mtime_on_use")? {
self.unstable_flags.mtime_on_use |= val;
if let Some(unstable_configs) =
self.get::<Option<HashMap<String, String>>>("unstable")?
{
self.unstable_flags
.from_table(&unstable_configs)
.with_context(|| "Invalid [unstable] entry in Cargo config")?;
// NB. It sucks to parse these twice, but doing it again here
// allows the CLI to override config files for both enabling
// and disabling.
self.unstable_flags.parse(unstable_flags)?;
}
}

View File

@ -7,6 +7,16 @@ see a list of flags available.
`-Z unstable-options` is a generic flag for enabling other unstable
command-line flags. Options requiring this will be called out below.
Anything which can be configured with a Z flag can also be set in the cargo
config file (`.cargo/config.toml`) in the `unstable` table. For example:
```
[unstable]
mtime-on-use = 'yes'
multitarget = 'yes'
timings = 'yes'
```
Some unstable features will require you to specify the `cargo-features` key in
`Cargo.toml`.

View File

@ -1094,6 +1094,72 @@ Caused by:
.is_none());
}
#[cargo_test]
/// Assert that unstable options can be configured with the `unstable` table in
/// cargo config files
fn config_unstable_table() {
cargo::core::enable_nightly_features();
write_config(
"\
[unstable]
print-im-a-teapot = 'yes'
",
);
let config = ConfigBuilder::new().build();
assert_eq!(config.cli_unstable().print_im_a_teapot, true);
}
#[cargo_test]
/// Assert that dotted notation works for configuring unstable options
fn config_unstable_dotted() {
cargo::core::enable_nightly_features();
write_config(
"\
unstable.print-im-a-teapot = 'yes'
",
);
let config = ConfigBuilder::new().build();
assert_eq!(config.cli_unstable().print_im_a_teapot, true);
}
#[cargo_test]
/// Assert that Zflags on the CLI take precedence over those from config
fn config_unstable_cli_wins() {
cargo::core::enable_nightly_features();
write_config(
"\
unstable.print-im-a-teapot = 'yes'
",
);
let config = ConfigBuilder::new().build();
assert_eq!(config.cli_unstable().print_im_a_teapot, true);
let config = ConfigBuilder::new()
.unstable_flag("print-im-a-teapot=no")
.build();
assert_eq!(config.cli_unstable().print_im_a_teapot, false);
}
#[cargo_test]
/// Assert that atempting to set an unstable flag that doesn't exist via config
/// errors out the same as it would on the command line
fn config_unstable_invalid_flag() {
cargo::core::enable_nightly_features();
write_config(
"\
unstable.an-invalid-flag = 'yes'
",
);
assert_error(
ConfigBuilder::new().build_err().unwrap_err(),
"\
Invalid [unstable] entry in Cargo config
Caused by:
unknown `-Z` flag specified: an-invalid-flag",
);
}
#[cargo_test]
fn table_merge_failure() {
// Config::merge fails to merge entries in two tables.