Auto merge of #118368 - GuillaumeGomez:env-flag, r=Nilstrieb

Implement `--env` compiler flag (without `tracked_env` support)

Part of https://github.com/rust-lang/rust/issues/80792.
Implementation of https://github.com/rust-lang/compiler-team/issues/653.
Not an implementation of https://github.com/rust-lang/rfcs/pull/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
This commit is contained in:
bors 2023-12-10 21:48:53 +00:00
commit d86d65bbc1
9 changed files with 107 additions and 3 deletions

View file

@ -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).