-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #118368 - GuillaumeGomez:env-flag, r=Nilstrieb
Implement `--env` compiler flag (without `tracked_env` support) Part of #80792. Implementation of rust-lang/compiler-team#653. Not an implementation of rust-lang/rfcs#2794. It adds the `--env` compiler flag option which allows to set environment values used by `env!` and `option_env!`. Important to note: When trying to retrieve an environment variable value, it will first look into the ones defined with `--env`, and if there isn't one, then only it will look into the environment variables. So if you use `--env PATH=a`, then `env!("PATH")` will return `"a"` and not the actual `PATH` value. As mentioned in the title, `tracked_env` support is not added here. I'll do it in a follow-up PR. r? rust-lang/compiler
- Loading branch information
Showing
9 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# `env` | ||
|
||
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. | ||
|
||
When retrieving an environment variable value, the one specified by `--env` will take | ||
precedence. For example, if you want have `PATH=a` in your environment and pass: | ||
|
||
```bash | ||
rustc --env PATH=env | ||
``` | ||
|
||
Then you will have: | ||
|
||
```rust,no_run | ||
assert_eq!(env!("PATH"), "env"); | ||
``` | ||
|
||
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 Path=...` (case sensitive). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-pass | ||
// rustc-env:MY_VAR=tadam | ||
// compile-flags: --env MY_VAR=123abc -Zunstable-options | ||
|
||
// This test ensures that variables provided with `--env` take precedence over | ||
// variables from environment. | ||
fn main() { | ||
assert_eq!(env!("MY_VAR"), "123abc"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// compile-flags: --env FOO=123abc -Zunstable-options | ||
// run-pass | ||
fn main() { | ||
assert_eq!(env!("FOO"), "123abc"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// run-pass | ||
// rustc-env:MY_ENV=/ | ||
// Ensures that variables not defined through `--env` are still available. | ||
|
||
fn main() { | ||
assert!(!env!("MY_ENV").is_empty()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// compile-flags: --env A=B | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
error: the `-Z unstable-options` flag must also be passed to enable the flag `env` | ||
|