Skip to content
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

Closed
wants to merge 6 commits into from

Conversation

beepster4096
Copy link
Contributor

Closes #80792. Adds the unstable --env VAR=VALUE option, which injects environment variables into the program, allowing them to be read by the env! and option_env! macros.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 26, 2022
@rust-highfive
Copy link
Collaborator

r? @estebank

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 26, 2022
@bjorn3
Copy link
Member

bjorn3 commented Feb 27, 2022

It seems like this doesn't affect proc macros in any way.

@beepster4096
Copy link
Contributor Author

Huh, I completely forgot about proc macros. Looks like I'll need to figure that out too.

@bjorn3
Copy link
Member

bjorn3 commented Feb 27, 2022

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.

@Mark-Simulacrum
Copy link
Member

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

@bjorn3
Copy link
Member

bjorn3 commented Feb 27, 2022

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)

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 20, 2022
@apiraino
Copy link
Contributor

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

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 30, 2022
@bors
Copy link
Contributor

bors commented Apr 9, 2022

☔ The latest upstream changes (presumably #95697) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

Triage:
@drmeepster - can you please post the status of this PR? Thank you

@beepster4096
Copy link
Contributor Author

Got side-tracked by other work. I should have it updated in around a few days.

@rust-log-analyzer

This comment has been minimized.

@beepster4096
Copy link
Contributor Author

Alright, proc_macro::tracked_env::var now works with this option.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 27, 2022
@@ -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],
Copy link
Member

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.

Copy link
Contributor

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.

@jyn514
Copy link
Member

jyn514 commented Jun 23, 2022

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));
Copy link
Member

@bjorn3 bjorn3 Jun 23, 2022

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);
Copy link
Member

@bjorn3 bjorn3 Jun 23, 2022

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.

Copy link
Contributor

@jsgf jsgf Oct 2, 2023

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.

@jyn514
Copy link
Member

jyn514 commented Jun 23, 2022

Having environment variables differ for proc_macros between std::env and tracked_env seems ... pretty bad

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.

@bjorn3
Copy link
Member

bjorn3 commented Jun 23, 2022

Is there any way we can make this consistent?

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 std::env::var entirely for a given thread using an unstable hook in libstd. Rust-analyzer can't depend on this, so having an unstable hook to override env vars for specific threads is not really an option.

@jyn514
Copy link
Member

jyn514 commented Jun 23, 2022

Rust-analyzer can't depend on this, so having an unstable hook to override env vars for specific threads is not really an option.

I don't follow, sorry - the hook idea sounds great to me, why would it break rust-analyzer?

@bjorn3
Copy link
Member

bjorn3 commented Jun 23, 2022

Why do we want to support this?

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 CodegenBackend value that replaces functions in the specified JITModule (and thus requires every rustc invocation to be in the same process). This hot code swapping support is on an old side branch, but I want to rebase it someday.

also though, why do we have code accessing global (currently thread-local) variables? AFAIK this is mostly used for Debug impls ... can we make those implement a custom trait instead?

That is going to be a lot of work to replace pretty much every Debug impl with an impl of this trait. It would mean losing the ability to do for example bug!("some error with {:?}", the_error) or dbg!(ty) as those require Debug to be implemented. And finally the query system itself requires a thread local variable to keep track of the queries the current thread is evaluation.

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 24, 2022
@bors
Copy link
Contributor

bors commented Jul 27, 2022

☔ The latest upstream changes (presumably #99816) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot
Copy link
Collaborator

rustbot commented Aug 1, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

Some changes occurred in library/proc_macro/src/bridge

cc @rust-lang/wg-rls-2

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@jyn514
Copy link
Member

jyn514 commented Aug 1, 2022

That is going to be a lot of work

We'll need to do that work anyway to support parallel_compiler, though, right?

@bjorn3
Copy link
Member

bjorn3 commented Aug 2, 2022

That is going to be a lot of work

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.

@estebank
Copy link
Contributor

estebank commented Aug 5, 2022

r? @jyn514

@rust-highfive rust-highfive assigned jyn514 and unassigned estebank Aug 5, 2022
@jyn514
Copy link
Member

jyn514 commented Aug 6, 2022

I think this needs an MCP. @drmeepster can you open an issue on rust-lang/compiler-team?

@jyn514 jyn514 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 6, 2022
@JohnCSimon JohnCSimon added S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. and removed S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. labels Sep 11, 2022
@JohnCSimon
Copy link
Member

@drmeepster

jyn514 commented on Aug 6
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.

@jyn514
Copy link
Member

jyn514 commented Oct 1, 2022

I'm going to close this for now. Feel free to re-open whenever you have time for an MCP.

@jyn514 jyn514 closed this Oct 1, 2022
@jyn514 jyn514 added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Oct 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to pass environment variables to rustc with a flag