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
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add --env to env-dep-info test
  • Loading branch information
beepster4096 committed Aug 1, 2022
commit 34d71d9a5fac3f51501668d80d13d49582ce8180
2 changes: 1 addition & 1 deletion library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1508,7 +1508,7 @@ pub mod tracked_env {
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.


let value = injected_value.map_or_else(env_value, Ok);
let value = injected_value.map_or(env_value, Ok);
crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok());
value
}
8 changes: 6 additions & 2 deletions src/test/run-make/env-dep-info/Makefile
Original file line number Diff line number Diff line change
@@ -7,13 +7,17 @@ ADDITIONAL_ARGS := $(RUSTFLAGS)
endif

all:
EXISTING_ENV=1 EXISTING_OPT_ENV=1 $(RUSTC) --emit dep-info main.rs
EXISTING_ENV=1 EXISTING_OPT_ENV=1 OVERRIDEN_INJECTED_ENV=0 $(RUSTC) --emit dep-info --env INJECTED_ENV=1 --env OVERRIDEN_ENV=1 main.rs
$(CGREP) "# env-dep:EXISTING_ENV=1" < $(TMPDIR)/main.d
$(CGREP) "# env-dep:EXISTING_OPT_ENV=1" < $(TMPDIR)/main.d
$(CGREP) "# env-dep:NONEXISTENT_OPT_ENV" < $(TMPDIR)/main.d
$(CGREP) "# env-dep:ESCAPE\nESCAPE\\" < $(TMPDIR)/main.d
$(CGREP) "# env-dep:INJECTED_ENV=1" < $(TMPDIR)/main.d
$(CGREP) "# env-dep:OVERRIDEN_ENV=1" < $(TMPDIR)/main.d
# Proc macro
$(BARE_RUSTC) $(ADDITIONAL_ARGS) --out-dir $(TMPDIR) macro_def.rs
EXISTING_PROC_MACRO_ENV=1 $(RUSTC) --emit dep-info macro_use.rs
EXISTING_PROC_MACRO_ENV=1 OVERRIDEN_PROC_MACRO_ENV=0 $(RUSTC) --emit dep-info --env INJECTED_PROC_MACRO_ENV=1 --env OVERRIDEN_PROC_MACRO_ENV=1 macro_use.rs
$(CGREP) "# env-dep:EXISTING_PROC_MACRO_ENV=1" < $(TMPDIR)/macro_use.d
$(CGREP) "# env-dep:NONEXISTENT_PROC_MACEO_ENV" < $(TMPDIR)/macro_use.d
$(CGREP) "# env-dep:INJECTED_PROC_MACRO_ENV=1" < $(TMPDIR)/macro_use.d
$(CGREP) "# env-dep:OVERRIDEN_PROC_MACRO_ENV=1" < $(TMPDIR)/macro_use.d
2 changes: 2 additions & 0 deletions src/test/run-make/env-dep-info/macro_def.rs
Original file line number Diff line number Diff line change
@@ -8,5 +8,7 @@ use proc_macro::*;
pub fn access_env_vars(_: TokenStream) -> TokenStream {
let _ = tracked_env::var("EXISTING_PROC_MACRO_ENV");
let _ = tracked_env::var("NONEXISTENT_PROC_MACEO_ENV");
let _ = tracked_env::var("INJECTED_PROC_MACRO_ENV");
let _ = tracked_env::var("OVERRIDEN_PROC_MACRO_ENV");
TokenStream::new()
}
2 changes: 2 additions & 0 deletions src/test/run-make/env-dep-info/main.rs
Original file line number Diff line number Diff line change
@@ -3,4 +3,6 @@ fn main() {
option_env!("EXISTING_OPT_ENV");
option_env!("NONEXISTENT_OPT_ENV");
option_env!("ESCAPE\nESCAPE\\");
env!("INJECTED_ENV");
env!("OVERRIDEN_ENV");
}