From 87c4695d06fb69f010972a3ed9fe43d4c2d84d8f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 13 Jan 2024 14:33:00 +0100 Subject: [PATCH 1/2] Stabilize `--env-set` option --- compiler/rustc_session/src/config.rs | 2 +- src/doc/rustc/src/command-line-arguments.md | 44 ++++++++++++++++++ .../src/compiler-flags/env-set.md | 45 ------------------- tests/ui/extenv/extenv-env-overload.rs | 2 +- tests/ui/extenv/extenv-env.rs | 2 +- tests/ui/feature-gates/env-flag.rs | 3 -- tests/ui/feature-gates/env-flag.stderr | 2 - tests/ui/proc-macro/env.rs | 2 +- 8 files changed, 48 insertions(+), 54 deletions(-) delete mode 100644 src/doc/unstable-book/src/compiler-flags/env-set.md delete mode 100644 tests/ui/feature-gates/env-flag.rs delete mode 100644 tests/ui/feature-gates/env-flag.stderr diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index fe1166457baea..dd78beecebc8b 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1827,7 +1827,7 @@ pub fn rustc_optgroups() -> Vec { "Remap source names in all output (compiler messages and output files)", "FROM=TO", ), - opt::multi("", "env-set", "Inject an environment variable", "VAR=VALUE"), + opt::multi_s("", "env-set", "Inject an environment variable", "VAR=VALUE"), ]); opts } diff --git a/src/doc/rustc/src/command-line-arguments.md b/src/doc/rustc/src/command-line-arguments.md index 5e02453e23667..4c69efc49bbc4 100644 --- a/src/doc/rustc/src/command-line-arguments.md +++ b/src/doc/rustc/src/command-line-arguments.md @@ -504,3 +504,47 @@ an empty option. The file can use Unix or Windows style line endings, and must b encoded as UTF-8. [the JSON chapter]: json.md + + +## `env-set`: inject an environment variable + +This option flag allows to specify environment variables value at compile time to be +used by `env!` and `option_env!` macros. It also impacts `tracked_env::var` function +from the `proc_macro` crate. + +This information will be stored in the dep-info files. For more information about +dep-info files, take a look [here](https://doc.rust-lang.org/cargo/guide/build-cache.html#dep-info-file\ +s). + +When retrieving an environment variable value, the one specified by `--env-set` will take +precedence. For example, if you want have `PATH=a` in your environment and pass: + +```bash +rustc --env-set PATH=env +``` + +Then you will have: + +```rust,no_run +assert_eq!(env!("PATH"), "env"); +``` + +It will trigger a new compilation if any of the `--env-set` argument value is different. +So if you first passed: + +```bash +--env-set A=B --env X=12 +``` + +and then on next compilation: + +```bash +--env-set A=B +``` + +`X` value is different (not set) so the code will be re-compiled. + +Please note that on Windows, environment variables are case insensitive but case +preserving whereas `rustc`'s environment variables are case sensitive. For example, +having `Path` in your environment (case insensitive) is different than using +`rustc --env-set Path=...` (case sensitive). diff --git a/src/doc/unstable-book/src/compiler-flags/env-set.md b/src/doc/unstable-book/src/compiler-flags/env-set.md deleted file mode 100644 index e5d7206c3a340..0000000000000 --- a/src/doc/unstable-book/src/compiler-flags/env-set.md +++ /dev/null @@ -1,45 +0,0 @@ -# `env-set` - -The tracking issue for this feature is: [#118372](https://github.com/rust-lang/rust/issues/118372). - ------------------------- - -This option flag allows to specify environment variables value at compile time to be -used by `env!` and `option_env!` macros. It also impacts `tracked_env::var` function -from the `proc_macro` crate. - -This information will be stored in the dep-info files. For more information about -dep-info files, take a look [here](https://doc.rust-lang.org/cargo/guide/build-cache.html#dep-info-files). - -When retrieving an environment variable value, the one specified by `--env-set` will take -precedence. For example, if you want have `PATH=a` in your environment and pass: - -```bash -rustc --env-set PATH=env -``` - -Then you will have: - -```rust,no_run -assert_eq!(env!("PATH"), "env"); -``` - -It will trigger a new compilation if any of the `--env-set` argument value is different. -So if you first passed: - -```bash ---env-set A=B --env X=12 -``` - -and then on next compilation: - -```bash ---env-set A=B -``` - -`X` value is different (not set) so the code will be re-compiled. - -Please note that on Windows, environment variables are case insensitive but case -preserving whereas `rustc`'s environment variables are case sensitive. For example, -having `Path` in your environment (case insensitive) is different than using -`rustc --env-set Path=...` (case sensitive). diff --git a/tests/ui/extenv/extenv-env-overload.rs b/tests/ui/extenv/extenv-env-overload.rs index 8b3b565fe83f0..520c9640c4793 100644 --- a/tests/ui/extenv/extenv-env-overload.rs +++ b/tests/ui/extenv/extenv-env-overload.rs @@ -1,6 +1,6 @@ // run-pass // rustc-env:MY_VAR=tadam -// compile-flags: --env-set MY_VAR=123abc -Zunstable-options +// compile-flags: --env-set MY_VAR=123abc // This test ensures that variables provided with `--env` take precedence over // variables from environment. diff --git a/tests/ui/extenv/extenv-env.rs b/tests/ui/extenv/extenv-env.rs index 051ea214c1bd0..0157a130b3f5c 100644 --- a/tests/ui/extenv/extenv-env.rs +++ b/tests/ui/extenv/extenv-env.rs @@ -1,4 +1,4 @@ -// compile-flags: --env-set FOO=123abc -Zunstable-options +// compile-flags: --env-set FOO=123abc // run-pass fn main() { assert_eq!(env!("FOO"), "123abc"); diff --git a/tests/ui/feature-gates/env-flag.rs b/tests/ui/feature-gates/env-flag.rs deleted file mode 100644 index 598773cf3e4cc..0000000000000 --- a/tests/ui/feature-gates/env-flag.rs +++ /dev/null @@ -1,3 +0,0 @@ -// compile-flags: --env-set A=B - -fn main() {} diff --git a/tests/ui/feature-gates/env-flag.stderr b/tests/ui/feature-gates/env-flag.stderr deleted file mode 100644 index a9fa1b65ea18c..0000000000000 --- a/tests/ui/feature-gates/env-flag.stderr +++ /dev/null @@ -1,2 +0,0 @@ -error: the `-Z unstable-options` flag must also be passed to enable the flag `env-set` - diff --git a/tests/ui/proc-macro/env.rs b/tests/ui/proc-macro/env.rs index c0edda4f7df23..21c4307528006 100644 --- a/tests/ui/proc-macro/env.rs +++ b/tests/ui/proc-macro/env.rs @@ -1,7 +1,7 @@ // aux-build:env.rs // run-pass // rustc-env: THE_CONST=1 -// compile-flags: -Zunstable-options --env-set THE_CONST=12 --env-set ANOTHER=4 +// compile-flags: --env-set THE_CONST=12 --env-set ANOTHER=4 #![crate_name = "foo"] From b90ccdefa3705e60d9ca69e48963bd4a8b2f013a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 18 Jan 2024 12:32:56 +0100 Subject: [PATCH 2/2] Improve documentation for `--env-set` option flag --- src/doc/rustc/src/command-line-arguments.md | 25 +++++---------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/doc/rustc/src/command-line-arguments.md b/src/doc/rustc/src/command-line-arguments.md index 4c69efc49bbc4..db1e99afa56c5 100644 --- a/src/doc/rustc/src/command-line-arguments.md +++ b/src/doc/rustc/src/command-line-arguments.md @@ -508,16 +508,16 @@ encoded as UTF-8. ## `env-set`: inject an environment variable -This option flag allows to specify environment variables value at compile time to be -used by `env!` and `option_env!` macros. It also impacts `tracked_env::var` function -from the `proc_macro` crate. +This option flag allows specification of environment variables at compile time for the +`env!` and `option_env!` macros and `tracked_env::var` function from the `proc_macro` +crate. This information will be stored in the dep-info files. For more information about dep-info files, take a look [here](https://doc.rust-lang.org/cargo/guide/build-cache.html#dep-info-file\ s). -When retrieving an environment variable value, the one specified by `--env-set` will take -precedence. For example, if you want have `PATH=a` in your environment and pass: +When retrieving an environment variable, the value specified by `--env-set` will take +precedence. For example, if you want have `PATH=env` in your environment and pass: ```bash rustc --env-set PATH=env @@ -529,20 +529,7 @@ Then you will have: assert_eq!(env!("PATH"), "env"); ``` -It will trigger a new compilation if any of the `--env-set` argument value is different. -So if you first passed: - -```bash ---env-set A=B --env X=12 -``` - -and then on next compilation: - -```bash ---env-set A=B -``` - -`X` value is different (not set) so the code will be re-compiled. +Crates will be fully re-compiled if `--env-set` arguments are changed. Please note that on Windows, environment variables are case insensitive but case preserving whereas `rustc`'s environment variables are case sensitive. For example,