Skip to content

Commit 9225ac7

Browse files
committed
Auto merge of #9411 - joshtriplett:stabilize-configurable-env, r=alexcrichton
Stabilize `[env]` sections `[env]` sections seem to work as advertised.
2 parents 0a24beb + 323265d commit 9225ac7

File tree

5 files changed

+54
-48
lines changed

5 files changed

+54
-48
lines changed

src/cargo/core/compiler/compilation.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,15 @@ impl<'cfg> Compilation<'cfg> {
345345
.env("CARGO_PKG_AUTHORS", &pkg.authors().join(":"))
346346
.cwd(pkg.root());
347347

348-
if self.config.cli_unstable().configurable_env {
349-
// Apply any environment variables from the config
350-
for (key, value) in self.config.env_config()?.iter() {
351-
// never override a value that has already been set by cargo
352-
if cmd.get_envs().contains_key(key) {
353-
continue;
354-
}
348+
// Apply any environment variables from the config
349+
for (key, value) in self.config.env_config()?.iter() {
350+
// never override a value that has already been set by cargo
351+
if cmd.get_envs().contains_key(key) {
352+
continue;
353+
}
355354

356-
if value.is_force() || env::var_os(key).is_none() {
357-
cmd.env(key, value.resolve(self.config));
358-
}
355+
if value.is_force() || env::var_os(key).is_none() {
356+
cmd.env(key, value.resolve(self.config));
359357
}
360358
}
361359

src/cargo/core/features.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,6 @@ unstable_cli_options!(
627627
build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
628628
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
629629
config_include: bool = ("Enable the `include` key in config files"),
630-
configurable_env: bool = ("Enable the [env] section in the .cargo/config.toml file"),
631630
credential_process: bool = ("Add a config setting to fetch registry authentication tokens by calling an external process"),
632631
doctest_in_workspace: bool = ("Compile doctests with paths relative to the workspace root"),
633632
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
@@ -691,6 +690,8 @@ const STABILIZED_FEATURES: &str = "The new feature resolver is now available \
691690
const STABILIZED_EXTRA_LINK_ARG: &str = "Additional linker arguments are now \
692691
supported without passing this flag.";
693692

693+
const STABILIZED_CONFIGURABLE_ENV: &str = "The [env] section is now always enabled.";
694+
694695
fn deserialize_build_std<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
695696
where
696697
D: serde::Deserializer<'de>,
@@ -833,7 +834,6 @@ impl CliUnstable {
833834
"doctest-in-workspace" => self.doctest_in_workspace = parse_empty(k, v)?,
834835
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
835836
"jobserver-per-rustc" => self.jobserver_per_rustc = parse_empty(k, v)?,
836-
"configurable-env" => self.configurable_env = parse_empty(k, v)?,
837837
"host-config" => self.host_config = parse_empty(k, v)?,
838838
"target-applies-to-host" => self.target_applies_to_host = parse_empty(k, v)?,
839839
"patch-in-config" => self.patch_in_config = parse_empty(k, v)?,
@@ -871,6 +871,7 @@ impl CliUnstable {
871871
"crate-versions" => stabilized_warn(k, "1.47", STABILIZED_CRATE_VERSIONS),
872872
"package-features" => stabilized_warn(k, "1.51", STABILIZED_PACKAGE_FEATURES),
873873
"extra-link-arg" => stabilized_warn(k, "1.56", STABILIZED_EXTRA_LINK_ARG),
874+
"configurable-env" => stabilized_warn(k, "1.56", STABILIZED_CONFIGURABLE_ENV),
874875
"future-incompat-report" => self.future_incompat_report = parse_empty(k, v)?,
875876
_ => bail!("unknown `-Z` flag specified: {}", k),
876877
}

src/doc/src/reference/config.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ pipelining = true # rustc pipelining
7979
browser = "chromium" # browser to use with `cargo doc --open`,
8080
# overrides the `BROWSER` environment variable
8181

82+
[env]
83+
# Set ENV_VAR_NAME=value for any process run by Cargo
84+
ENV_VAR_NAME = "value"
85+
# Set even if already present in environment
86+
ENV_VAR_NAME_2 = { value = "value", force = true }
87+
# Value is relative to .cargo directory containing `config.toml`, make absolute
88+
ENV_VAR_NAME_3 = { value = "relative/path", relative = true }
89+
8290
[cargo-new]
8391
vcs = "none" # VCS to use ('git', 'hg', 'pijul', 'fossil', 'none')
8492

@@ -469,6 +477,30 @@ Valid values are `git`, `hg` (for Mercurial), `pijul`, `fossil` or `none` to
469477
disable this behavior. Defaults to `git`, or `none` if already inside a VCS
470478
repository. Can be overridden with the `--vcs` CLI option.
471479

480+
### `[env]`
481+
482+
The `[env]` section allows you to set additional environment variables for
483+
build scripts, rustc invocations, `cargo run` and `cargo build`.
484+
485+
```toml
486+
[env]
487+
OPENSSL_DIR = "/opt/openssl"
488+
```
489+
490+
By default, the variables specified will not override values that already exist
491+
in the environment. This behavior can be changed by setting the `force` flag.
492+
493+
Setting the `relative` flag evaluates the value as a config-relative path that
494+
is relative to the parent directory of the `.cargo` directory that contains the
495+
`config.toml` file. The value of the environment variable will be the full
496+
absolute path.
497+
498+
```toml
499+
[env]
500+
TMPDIR = { value = "/home/tmp", force = true }
501+
OPENSSL_DIR = { value = "vendor/openssl", relative = true }
502+
```
503+
472504
#### `[http]`
473505

474506
The `[http]` table defines settings for HTTP behavior. This includes fetching

src/doc/src/reference/unstable.md

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,33 +1234,6 @@ the `--future-incompat-report` flag. The developer should then update their
12341234
dependencies to a version where the issue is fixed, or work with the
12351235
developers of the dependencies to help resolve the issue.
12361236

1237-
### configurable-env
1238-
* Original Pull Request: [#9175](https://github.com/rust-lang/cargo/pull/9175)
1239-
* Tracking Issue: [#9539](https://github.com/rust-lang/cargo/issues/9539)
1240-
1241-
The `-Z configurable-env` flag enables the `[env]` section in the
1242-
`.cargo/config.toml` file. This section allows you to set additional environment
1243-
variables for build scripts, rustc invocations, `cargo run` and `cargo build`.
1244-
1245-
```toml
1246-
[env]
1247-
OPENSSL_DIR = "/opt/openssl"
1248-
```
1249-
1250-
By default, the variables specified will not override values that already exist
1251-
in the environment. This behavior can be changed by setting the `force` flag.
1252-
1253-
Setting the `relative` flag evaluates the value as a config-relative path that
1254-
is relative to the parent directory of the `.cargo` directory that contains the
1255-
`config.toml` file. The value of the environment variable will be the full
1256-
absolute path.
1257-
1258-
```toml
1259-
[env]
1260-
TMPDIR = { value = "/home/tmp", force = true }
1261-
OPENSSL_DIR = { value = "vendor/openssl", relative = true }
1262-
```
1263-
12641237
### patch-in-config
12651238
* Original Pull Request: [#9204](https://github.com/rust-lang/cargo/pull/9204)
12661239
* Tracking Issue: [#9269](https://github.com/rust-lang/cargo/issues/9269)
@@ -1440,3 +1413,10 @@ The `extra-link-arg` feature to specify additional linker arguments in build
14401413
scripts has been stabilized in the 1.56 release. See the [build script
14411414
documentation](build-scripts.md#outputs-of-the-build-script) for more
14421415
information on specifying extra linker arguments.
1416+
1417+
### configurable-env
1418+
1419+
The `configurable-env` feature to specify environment variables in Cargo
1420+
configuration has been stabilized in the 1.56 release. See the [config
1421+
documentation](config.html#env) for more information about configuring
1422+
environment variables.

tests/testsuite/cargo_env_config.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ fn env_basic() {
2525
)
2626
.build();
2727

28-
p.cargo("run -Zconfigurable-env")
29-
.masquerade_as_nightly_cargo()
28+
p.cargo("run")
3029
.with_stdout_contains("compile-time:Hello")
3130
.with_stdout_contains("run-time:Hello")
3231
.run();
@@ -52,8 +51,7 @@ fn env_invalid() {
5251
)
5352
.build();
5453

55-
p.cargo("build -Zconfigurable-env")
56-
.masquerade_as_nightly_cargo()
54+
p.cargo("build")
5755
.with_status(101)
5856
.with_stderr_contains("[..]could not load config key `env.ENV_TEST_BOOL`")
5957
.run();
@@ -85,8 +83,7 @@ fn env_force() {
8583
)
8684
.build();
8785

88-
p.cargo("run -Zconfigurable-env")
89-
.masquerade_as_nightly_cargo()
86+
p.cargo("run")
9087
.env("ENV_TEST_FORCED", "from-env")
9188
.env("ENV_TEST_UNFORCED", "from-env")
9289
.env("ENV_TEST_UNFORCED_DEFAULT", "from-env")
@@ -127,9 +124,7 @@ fn env_relative() {
127124
)
128125
.build();
129126

130-
p.cargo("run -Zconfigurable-env")
131-
.masquerade_as_nightly_cargo()
132-
.run();
127+
p.cargo("run").run();
133128
}
134129

135130
#[cargo_test]

0 commit comments

Comments
 (0)