-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement --env
compiler flag (without tracked_env
support)
#118368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
486e55e
Implement `--env` compiler flag
GuillaumeGomez 37d6809
Add ui tests for `--env` option
GuillaumeGomez d2b1f94
Add feature gate test for `--env` flag
GuillaumeGomez dc2f77a
Add (unstable) documentation for `--env` command line option
GuillaumeGomez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
GuillaumeGomez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 hidden or 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 hidden or 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` | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.