-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 support for --env
on tracked_env::var
#118830
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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,28 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
#![feature(proc_macro_tracked_env)] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use proc_macro::tracked_env::var; | ||
|
||
#[proc_macro] | ||
pub fn generate_const(input: TokenStream) -> TokenStream { | ||
let the_const = match var("THE_CONST") { | ||
Ok(x) if x == "12" => { | ||
"const THE_CONST: u32 = 12;" | ||
} | ||
_ => { | ||
"const THE_CONST: u32 = 0;" | ||
} | ||
}; | ||
let another = if var("ANOTHER").is_ok() { | ||
"const ANOTHER: u32 = 1;" | ||
} else { | ||
"const ANOTHER: u32 = 2;" | ||
}; | ||
format!("{the_const}{another}").parse().unwrap() | ||
} |
This file contains 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,17 @@ | ||
// aux-build:env.rs | ||
// run-pass | ||
// rustc-env: THE_CONST=1 | ||
// compile-flags: -Zunstable-options --env THE_CONST=12 --env ANOTHER=4 | ||
|
||
#![crate_name = "foo"] | ||
|
||
extern crate env; | ||
|
||
use env::generate_const; | ||
|
||
generate_const!(); | ||
|
||
fn main() { | ||
assert_eq!(THE_CONST, 12); | ||
assert_eq!(ANOTHER, 1); | ||
} |
Oops, something went wrong.
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.
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.
Is it correct to track
--env
here? I don't think it is. Tracked env vars end up in depinfo, implying that the compiler invocation depended on this environment variable, but it didn't, there is no environment variable and even if there is, the compiler didn't care, it just depended on a flag. I think it should only track whenenv::var
is used.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.
Well, if this function is called, it means the environment variable was used no? Or did I misunderstand what you meant?
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.
From an outside perspective, no "environment variable" was used. It just took a value for the flag. A build system doesn't have to check whether an environment variables changes to figure out whether a rebuild is needed, it needs to look at arguments instead (which it of course already does).
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.
Well, I'm fine with removing this call to
track_env_var
. Please just confirm it's what you want I do do it. :)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.
Yes
I also just checked your previous PR and I missed that there:
https://github.com/rust-lang/rust/pull/118368/files#diff-0eaa1095d59f2f3d9c015005d15fd8f7cda13121a8404d77b2955fc5fcd40449R37
The name here also makes it more clear what tracking is for: it's for depinfo which is used by builds systems or other consumers to figure out what went into the compilation (for example when things need to be rebuilt). It's essential that rustc records env variables it reads with
env::var
. But--env
are not actually env variables in the build environment, so they don't need to be tracked. I'd also be happy to review a PR changing the behavior for the macros to not track when the var is read from--env
>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.
That's a flag though and not an environment variable. A build system already needs to handle flags changing. Depinfo tells the build system "hey, I read this environment variable, check it again next time" but in reality it didn't read any environment variable, it got it from the flag.
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.
If it is not tracked in the dep info, any change to a
--env
argument would need to trigger recompilation of all crates even if they don't use thoseenv!()
at all.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 see, for build systems that pass env vars explicitly like that it would be nice if they could detect whether it was actually used. But I don't think the normal env depinfo is the right place for that. The case I was thinking about was for example having
A=x
in the environment and using--env A=x
. Then, changingA=y
should not cause any rebuilds as it wasn't used, only the--env A=x
value was.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.
And in this case it won't trigger a rebuild because the value is same. The
--env
option uses the same mechanism as theenv!
macro: variables are tracked if used and will trigger a rebuild if their value changed. So unless I missed something, I think it's doing exactly what we want?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.
So we're relying on build systems correctly doing the "dep info env -> when I passed --env" translation themselves, since they know they passed the flag.
That makes sense, I didn't think of that, I was too focused on the env depinfo meaning actual environment variable, but I guess the build system is supposed to know what it's doing.
In that case, sounds good. Maybe it should be documented explicitly in the documentation for
--env
that it ends up in depinfo as env vars.