Skip to content

Commit f994139

Browse files
Jon GjengsetHezuikn
Jon Gjengset
authored andcommitted
Stabilize --config
FCP rust-lang#7722 (comment) Closes rust-lang#7722.
1 parent 1f11a51 commit f994139

File tree

6 files changed

+75
-91
lines changed

6 files changed

+75
-91
lines changed

src/cargo/util/config/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,6 @@ impl Config {
892892
self.unstable_flags_cli = Some(unstable_flags.to_vec());
893893
}
894894
if !cli_config.is_empty() {
895-
self.unstable_flags.fail_if_stable_opt("--config", 6699)?;
896895
self.cli_config = Some(cli_config.iter().map(|s| s.to_string()).collect());
897896
self.merge_cli_args()?;
898897
}

src/doc/src/reference/config.md

+40
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,46 @@ support environment variables.
195195
In addition to the system above, Cargo recognizes a few other specific
196196
[environment variables][env].
197197

198+
### Command-line overrides
199+
200+
Cargo also accepts arbitrary configuration overrides through the
201+
`--config` command-line option. The argument should be in TOML syntax of
202+
`KEY=VALUE`:
203+
204+
```console
205+
cargo --config net.git-fetch-with-cli=true fetch
206+
```
207+
208+
The `--config` option may be specified multiple times, in which case the
209+
values are merged in left-to-right order, using the same merging logic
210+
that is used when multiple configuration files apply. Configuration
211+
values specified this way take precedence over environment variables,
212+
which take precedence over configuration files.
213+
214+
Some examples of what it looks like using Bourne shell syntax:
215+
216+
```console
217+
# Most shells will require escaping.
218+
cargo --config http.proxy=\"http://example.com\" …
219+
220+
# Spaces may be used.
221+
cargo --config "net.git-fetch-with-cli = true" …
222+
223+
# TOML array example. Single quotes make it easier to read and write.
224+
cargo --config 'build.rustdocflags = ["--html-in-header", "header.html"]' …
225+
226+
# Example of a complex TOML key.
227+
cargo --config "target.'cfg(all(target_arch = \"arm\", target_os = \"none\"))'.runner = 'my-runner'" …
228+
229+
# Example of overriding a profile setting.
230+
cargo --config profile.dev.package.image.opt-level=3 …
231+
```
232+
233+
The `--config` option can also be used to pass paths to extra
234+
configuration files that Cargo should use for a specific invocation.
235+
Options from configuration files loaded this way follow the same
236+
precedence rules as other options specified directly with `--config`.
237+
198238
### Config-relative paths
199239

200240
Paths in config files may be absolute, relative, or a bare name without any

src/doc/src/reference/unstable.md

+6-35
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ Each new feature described below should explain how to use it.
9696
* [unit-graph](#unit-graph) — Emits JSON for Cargo's internal graph structure.
9797
* [`cargo rustc --print`](#rustc---print) — Calls rustc with `--print` to display information from rustc.
9898
* Configuration
99-
* [config-cli](#config-cli) — Adds the ability to pass configuration options on the command-line.
10099
* [config-include](#config-include) — Adds the ability for config files to include other files.
101100
* [`cargo config`](#cargo-config) — Adds a new subcommand for viewing config files.
102101
* Registries
@@ -475,40 +474,6 @@ The `-Z unstable-options` command-line option must be used in order to use
475474
cargo check --keep-going -Z unstable-options
476475
```
477476

478-
### config-cli
479-
* Tracking Issue: [#7722](https://github.com/rust-lang/cargo/issues/7722)
480-
481-
The `--config` CLI option allows arbitrary config values to be passed
482-
in via the command-line. The argument should be in TOML syntax of KEY=VALUE:
483-
484-
```console
485-
cargo +nightly -Zunstable-options --config net.git-fetch-with-cli=true fetch
486-
```
487-
488-
The `--config` option may be specified multiple times, in which case the
489-
values are merged in left-to-right order, using the same merging logic that
490-
multiple config files use. CLI values take precedence over environment
491-
variables, which take precedence over config files.
492-
493-
Some examples of what it looks like using Bourne shell syntax:
494-
495-
```console
496-
# Most shells will require escaping.
497-
cargo --config http.proxy=\"http://example.com\" …
498-
499-
# Spaces may be used.
500-
cargo --config "net.git-fetch-with-cli = true" …
501-
502-
# TOML array example. Single quotes make it easier to read and write.
503-
cargo --config 'build.rustdocflags = ["--html-in-header", "header.html"]' …
504-
505-
# Example of a complex TOML key.
506-
cargo --config "target.'cfg(all(target_arch = \"arm\", target_os = \"none\"))'.runner = 'my-runner'" …
507-
508-
# Example of overriding a profile setting.
509-
cargo --config profile.dev.package.image.opt-level=3 …
510-
```
511-
512477
### config-include
513478
* Tracking Issue: [#7723](https://github.com/rust-lang/cargo/issues/7723)
514479

@@ -1598,3 +1563,9 @@ See the [Features chapter](features.md#dependency-features) for more information
15981563
The `-Ztimings` option has been stabilized as `--timings` in the 1.60 release.
15991564
(`--timings=html` and the machine-readable `--timings=json` output remain
16001565
unstable and require `-Zunstable-options`.)
1566+
1567+
### config-cli
1568+
1569+
The `--config` CLI option has been stabilized in the 1.63 release. See
1570+
the [config documentation](config.html#command-line-overrides) for more
1571+
information.

tests/testsuite/config.rs

-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ impl ConfigBuilder {
5555

5656
/// Passes a `--config` flag.
5757
pub fn config_arg(&mut self, arg: impl Into<String>) -> &mut Self {
58-
if !self.unstable.iter().any(|s| s == "unstable-options") {
59-
// --config is current unstable
60-
self.unstable_flag("unstable-options");
61-
}
6258
self.config_args.push(arg.into());
6359
self
6460
}

tests/testsuite/config_cli.rs

+28-18
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,9 @@
22
33
use super::config::{assert_error, assert_match, read_output, write_config, ConfigBuilder};
44
use cargo::util::config::Definition;
5-
use cargo_test_support::{paths, project};
5+
use cargo_test_support::paths;
66
use std::{collections::HashMap, fs};
77

8-
#[cargo_test]
9-
fn config_gated() {
10-
// Requires -Zunstable-options
11-
let p = project().file("src/lib.rs", "").build();
12-
13-
p.cargo("build --config --config build.jobs=1")
14-
.with_status(101)
15-
.with_stderr(
16-
"\
17-
[ERROR] the `--config` flag is unstable, [..]
18-
See [..]
19-
See [..]
20-
",
21-
)
22-
.run();
23-
}
24-
258
#[cargo_test]
269
fn basic() {
2710
// Simple example.
@@ -472,3 +455,30 @@ Caused by:
472455
expected string, but found array",
473456
);
474457
}
458+
459+
#[cargo_test]
460+
fn cli_path() {
461+
// --config path_to_file
462+
fs::write(paths::root().join("myconfig.toml"), "key = 123").unwrap();
463+
let config = ConfigBuilder::new()
464+
.cwd(paths::root())
465+
.config_arg("myconfig.toml")
466+
.build();
467+
assert_eq!(config.get::<u32>("key").unwrap(), 123);
468+
469+
let config = ConfigBuilder::new().config_arg("missing.toml").build_err();
470+
assert_error(
471+
config.unwrap_err(),
472+
"\
473+
failed to parse value from --config argument `missing.toml` as a dotted key expression
474+
475+
Caused by:
476+
TOML parse error at line 1, column 13
477+
|
478+
1 | missing.toml
479+
| ^
480+
Unexpected end of input
481+
Expected `.` or `=`
482+
",
483+
);
484+
}

tests/testsuite/config_include.rs

+1-33
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Tests for `include` config field.
22
33
use super::config::{assert_error, write_config, write_config_at, ConfigBuilder};
4-
use cargo_test_support::{no_such_file_err_msg, paths, project};
5-
use std::fs;
4+
use cargo_test_support::{no_such_file_err_msg, project};
65

76
#[cargo_test]
87
fn gated() {
@@ -255,34 +254,3 @@ Caused by:
255254
expected array, but found string",
256255
);
257256
}
258-
259-
#[cargo_test]
260-
fn cli_path() {
261-
// --config path_to_file
262-
fs::write(paths::root().join("myconfig.toml"), "key = 123").unwrap();
263-
let config = ConfigBuilder::new()
264-
.cwd(paths::root())
265-
.unstable_flag("config-include")
266-
.config_arg("myconfig.toml")
267-
.build();
268-
assert_eq!(config.get::<u32>("key").unwrap(), 123);
269-
270-
let config = ConfigBuilder::new()
271-
.unstable_flag("config-include")
272-
.config_arg("missing.toml")
273-
.build_err();
274-
assert_error(
275-
config.unwrap_err(),
276-
"\
277-
failed to parse value from --config argument `missing.toml` as a dotted key expression
278-
279-
Caused by:
280-
TOML parse error at line 1, column 13
281-
|
282-
1 | missing.toml
283-
| ^
284-
Unexpected end of input
285-
Expected `.` or `=`
286-
",
287-
);
288-
}

0 commit comments

Comments
 (0)