-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add option to pass environment variables #94387
Conversation
r? @estebank (rust-highfive has picked a reviewer for you, use r? to override) |
It seems like this doesn't affect proc macros in any way. |
Huh, I completely forgot about proc macros. Looks like I'll need to figure that out too. |
That one may be hard to solve "correctly" as environment variables are process global and it is possible to run multiple rustc instances in the same process when using an unstable api. |
We should be able to solve it correctly if the proc_macro is using https://doc.rust-lang.org/nightly/proc_macro/tracked_env/fn.var.html, right? That API is unstable right now, but it's a start, and stabilizing it might not be that hard (this could be an additional piece of motivation). |
Indeed. It may take a while for proc macros to migrate to it though. (Unless we clear the process env for proc macros when using the rustc binary) |
by reading the comments, it seems there's still a bit of work to do before another round of review, so I'll flip the switch (but feel free to re-request a review if it's the case) @rustbot author |
☔ The latest upstream changes (presumably #95697) made this pull request unmergeable. Please resolve the merge conflicts. |
Triage: |
Got side-tracked by other work. I should have it updated in around a few days. |
090788f
to
7f4eb38
Compare
This comment has been minimized.
This comment has been minimized.
Alright, @rustbot ready |
@@ -230,6 +232,9 @@ top_level_options!( | |||
|
|||
/// The (potentially remapped) working directory | |||
working_dir: RealFileName [TRACKED], | |||
|
|||
/// Overridden env vars used for `env!` and `option_env!` | |||
injected_env_vars: FxHashMap<String, String> [UNTRACKED], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be [TRACKED]
as changing the provided env vars should cause code which uses them to be recompiled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the tracking be based on the actual use of the env vars? If an env var is specified but the crate never references it, changing it shouldn't require a crate recompile.
Having environment variables differ for proc_macros between std::env and tracked_env seems ... pretty bad. Is there any way we can make this consistent? I worry this is going to lead to weird bugs in the ecosystem. |
cx.sess.opts.injected_env_vars.get(var.as_str()).map(|s| Symbol::intern(s)); | ||
let value = | ||
injected_value.or_else(|| env::var(var.as_str()).ok().as_deref().map(Symbol::intern)); | ||
|
||
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be skipped in case --env
mentions this env var. Otherwise changes to the actual env vars will cause a recompilation, even if they don't affect compilation at all.
@@ -1287,7 +1287,10 @@ pub mod tracked_env { | |||
#[unstable(feature = "proc_macro_tracked_env", issue = "74690")] | |||
pub fn var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> { | |||
let key: &str = key.as_ref(); | |||
let value = env::var(key); | |||
let injected_value = crate::bridge::client::FreeFunctions::injected_env_var(key); | |||
let env_value = env::var(key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think env::var
should only be called if injected_env_var
returned None
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my take on this - https://github.com/jsgf/rust/tree/env-sandbox - I just initialized the logical environment (equiv of injected vars) with all the env vars, and never used env::var
after initializing it. I think it's a more consistent approach. In particular, it allows you to logically delete vars from the environment.
Hmm, what I would really love is for all accesses to env variables to be tracked - can we kill two birds with one stone and have std::env use tracked_env internally when it detects that it's running as a proc macro somehow? That would also let us land tracked_env without an FCP for the new API. |
Env vars are global to the process (and not the compilation session) and as recent discussion has shown can't be written after any thread has been spawned (as is the case for rustc). Maybe we could prevent usage of |
I don't follow, sorry - the hook idea sounds great to me, why would it break rust-analyzer? |
In the past RLS did this. I also got a use case for it: To implement hot code swapping in cg_clif by invoking rustc and passing a special
That is going to be a lot of work to replace pretty much every |
☔ The latest upstream changes (presumably #99816) made this pull request unmergeable. Please resolve the merge conflicts. |
114f0f0
to
223942d
Compare
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
Some changes occurred in library/proc_macro/src/bridge cc @rust-lang/wg-rls-2 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
c405b41
to
8a9edfd
Compare
We'll need to do that work anyway to support parallel_compiler, though, right? |
All threads of a compilation session inherit the tls variables thanks to our fork of rayon having support for a hook that runs just after spawning a new worker thread and before running any jobs on it. This means that all Debug impls should already work with parallel_rustc. |
r? @jyn514 |
I think this needs an MCP. @drmeepster can you open an issue on rust-lang/compiler-team? |
If you have created this already, please link it. Thank you. |
I'm going to close this for now. Feel free to re-open whenever you have time for an MCP. |
Closes #80792. Adds the unstable
--env VAR=VALUE
option, which injects environment variables into the program, allowing them to be read by theenv!
andoption_env!
macros.