Skip to content

Add environment variable query - #130883

Merged
bors merged 3 commits into
rust-lang:masterfrom
madsmtm:env-var-query
Mar 27, 2025
Merged

Add environment variable query#130883
bors merged 3 commits into
rust-lang:masterfrom
madsmtm:env-var-query

Conversation

@madsmtm

Copy link
Copy Markdown
Member

Generally, rustc prefers command-line arguments, but in some cases, an environment variable really is the most sensible option. We should make sure that this works properly with the compiler's change-tracking mechanisms, such that changing the relevant environment variable causes a rebuild.

This PR is a first step forwards in doing that.

Part of the work needed to do #118204, see #129342 for some discussion.

r? @petrochenkov

@rustbotrustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 26, 2024
Comment threadcompiler/rustc_interface/src/passes.rs Outdated
}

fn env_var(tcx: TyCtxt<'_>, key: Symbol) -> Option<Symbol> {
let var = match std::env::var(key.as_str()) {

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something similar to Clippy's disallowed-methods that I could use to lint against usage of std::env::var in the compiler, and direct users towards the query instead?

See also rust-lang/cargo#11588.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@madsmtm
madsmtmforce-pushed the env-var-query branch 2 times, most recently from dee9b61 to 0ef9570CompareSeptember 27, 2024 14:06
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment threadcompiler/rustc_interface/src/passes.rs Outdated
Comment threadcompiler/rustc_borrowck/src/nll.rs Outdated
Comment threadcompiler/rustc_middle/src/query/mod.rs Outdated
Comment threadcompiler/rustc_middle/src/query/mod.rs
@petrochenkovpetrochenkov 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 Oct 22, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 1, 2024
…g, r=petrochenkov
Move versioned Apple LLVM targets from `rustc_target` to `rustc_codegen_ssa`
Fully specified LLVM targets contain the OS version on macOS/iOS/tvOS/watchOS/visionOS, and this version depends on the deployment target environment variables like `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET` etc.
We would like to move this to later in the compilation pipeline, both because it feels impure to access environment variables when fetching target information, but mostly because we need access to more information from rust-lang#130883 to do rust-lang#118204. See also rust-lang#129342 (comment) for some discussion.
The first and second commit does the actual refactor, it should be a non-functional change, the third commit adds diagnostics for invalid deployment targets, which are now possible to do because we have access to the session.
Tested with the same commands as in rust-lang#130435.
r? `@petrochenkov`
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Nov 2, 2024
…g, r=petrochenkov
Move versioned Apple LLVM targets from `rustc_target` to `rustc_codegen_ssa`
Fully specified LLVM targets contain the OS version on macOS/iOS/tvOS/watchOS/visionOS, and this version depends on the deployment target environment variables like `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET` etc.
We would like to move this to later in the compilation pipeline, both because it feels impure to access environment variables when fetching target information, but mostly because we need access to more information from rust-lang#130883 to do rust-lang#118204. See also rust-lang#129342 (comment) for some discussion.
The first and second commit does the actual refactor, it should be a non-functional change, the third commit adds diagnostics for invalid deployment targets, which are now possible to do because we have access to the session.
Tested with the same commands as in rust-lang#130435.
r? ```@petrochenkov```
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 2, 2024
…g, r=petrochenkov
Move versioned Apple LLVM targets from `rustc_target` to `rustc_codegen_ssa`
Fully specified LLVM targets contain the OS version on macOS/iOS/tvOS/watchOS/visionOS, and this version depends on the deployment target environment variables like `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET` etc.
We would like to move this to later in the compilation pipeline, both because it feels impure to access environment variables when fetching target information, but mostly because we need access to more information from rust-lang#130883 to do rust-lang#118204. See also rust-lang#129342 (comment) for some discussion.
The first and second commit does the actual refactor, it should be a non-functional change, the third commit adds diagnostics for invalid deployment targets, which are now possible to do because we have access to the session.
Tested with the same commands as in rust-lang#130435.
r? ````@petrochenkov````
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Nov 2, 2024
…g, r=petrochenkov
Move versioned Apple LLVM targets from `rustc_target` to `rustc_codegen_ssa`
Fully specified LLVM targets contain the OS version on macOS/iOS/tvOS/watchOS/visionOS, and this version depends on the deployment target environment variables like `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET` etc.
We would like to move this to later in the compilation pipeline, both because it feels impure to access environment variables when fetching target information, but mostly because we need access to more information from rust-lang#130883 to do rust-lang#118204. See also rust-lang#129342 (comment) for some discussion.
The first and second commit does the actual refactor, it should be a non-functional change, the third commit adds diagnostics for invalid deployment targets, which are now possible to do because we have access to the session.
Tested with the same commands as in rust-lang#130435.
r? `````@petrochenkov`````
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 2, 2024
…g, r=petrochenkov
Move versioned Apple LLVM targets from `rustc_target` to `rustc_codegen_ssa`
Fully specified LLVM targets contain the OS version on macOS/iOS/tvOS/watchOS/visionOS, and this version depends on the deployment target environment variables like `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET` etc.
We would like to move this to later in the compilation pipeline, both because it feels impure to access environment variables when fetching target information, but mostly because we need access to more information from rust-lang#130883 to do rust-lang#118204. See also rust-lang#129342 (comment) for some discussion.
The first and second commit does the actual refactor, it should be a non-functional change, the third commit adds diagnostics for invalid deployment targets, which are now possible to do because we have access to the session.
Tested with the same commands as in rust-lang#130435.
r? ``````@petrochenkov``````
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 2, 2024
Rollup merge of rust-lang#131037 - madsmtm:move-llvm-target-versioning, r=petrochenkov
Move versioned Apple LLVM targets from `rustc_target` to `rustc_codegen_ssa`
Fully specified LLVM targets contain the OS version on macOS/iOS/tvOS/watchOS/visionOS, and this version depends on the deployment target environment variables like `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET` etc.
We would like to move this to later in the compilation pipeline, both because it feels impure to access environment variables when fetching target information, but mostly because we need access to more information from rust-lang#130883 to do rust-lang#118204. See also rust-lang#129342 (comment) for some discussion.
The first and second commit does the actual refactor, it should be a non-functional change, the third commit adds diagnostics for invalid deployment targets, which are now possible to do because we have access to the session.
Tested with the same commands as in rust-lang#130435.
r? ``````@petrochenkov``````
@bors

This comment was marked as resolved.

@madsmtm

madsmtm commented Nov 16, 2024

Copy link
Copy Markdown
MemberAuthor

@rustbot ready

Note that I've also added a test that env!/option_env! works with incremental compilation, I felt it was relevant to this PR, but feel free to tell me to do it in a different one.

@rustbotrustbot 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 Nov 16, 2024
@madsmtm

madsmtm commented Mar 26, 2025

Copy link
Copy Markdown
MemberAuthor

Finally got around to rebasing and addressing your review comments, sorry for the long wait.

@rustbot ready

@rustbotrustbot 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 Mar 26, 2025
Comment threadcompiler/rustc_borrowck/src/nll.rs Outdated
Comment threadcompiler/rustc_interface/src/passes.rs Outdated
@petrochenkov

Copy link
Copy Markdown
Contributor

r=me with the remaining nits addressed.
@rustbot author

@rustbotrustbot 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 26, 2025
Along with `TyCtx::env_var` helper. These can be used to track
environment variable accesses in the query system.
Since `TyCtx::env_var_os` uses `OsStr`, this commit also adds the
necessary trait implementations for that to work.
This won't work with Cargo's change tracking, but it should work with incremental.
@madsmtm

Copy link
Copy Markdown
MemberAuthor

Done!

@rustbot ready

@rustbotrustbot 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 Mar 26, 2025
@petrochenkov

Copy link
Copy Markdown
Contributor

Thanks!
@bors r+

@bors

bors commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit 632ce38 has been approved by petrochenkov

It is now in the queue for this repository.

@borsbors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 26, 2025
TaKO8Ki added a commit to TaKO8Ki/rust that referenced this pull request Mar 26, 2025
Add environment variable query
Generally, `rustc` prefers command-line arguments, but in some cases, an environment variable really is the most sensible option. We should make sure that this works properly with the compiler's change-tracking mechanisms, such that changing the relevant environment variable causes a rebuild.
This PR is a first step forwards in doing that.
Part of the work needed to do rust-lang#118204, see rust-lang#129342 for some discussion.
r? `@petrochenkov`
@TaKO8KiTaKO8Ki mentioned this pull request Mar 26, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 26, 2025
Rollup of 9 pull requests
Successful merges:
- rust-lang#130883 (Add environment variable query)
- rust-lang#138672 (Avoiding calling queries when collecting active queries)
- rust-lang#138702 (Allow spawning threads after TLS destruction)
- rust-lang#138935 (Update wg-prio triagebot config)
- rust-lang#138946 (Un-bury chapters from the chapter list in rustc book)
- rust-lang#138964 (Implement lint against using Interner and InferCtxtLike in random compiler crates)
- rust-lang#138977 (Don't deaggregate InvocationParent just to reaggregate it again)
- rust-lang#138980 (Collect items referenced from var_debug_info)
- rust-lang#138985 (Use the correct binder scope for elided lifetimes in assoc consts)
r? `@ghost`
`@rustbot` modify labels: rollup
@ZalatharZalathar mentioned this pull request Mar 27, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 27, 2025
Rollup of 10 pull requests
Successful merges:
- rust-lang#130883 (Add environment variable query)
- rust-lang#138624 (Add mipsel maintainer)
- rust-lang#138672 (Avoiding calling queries when collecting active queries)
- rust-lang#138935 (Update wg-prio triagebot config)
- rust-lang#138946 (Un-bury chapters from the chapter list in rustc book)
- rust-lang#138964 (Implement lint against using Interner and InferCtxtLike in random compiler crates)
- rust-lang#138977 (Don't deaggregate InvocationParent just to reaggregate it again)
- rust-lang#138980 (Collect items referenced from var_debug_info)
- rust-lang#138985 (Use the correct binder scope for elided lifetimes in assoc consts)
- rust-lang#138987 (Always emit `native-static-libs` note, even if it is empty)
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit c45986a into rust-lang:masterMar 27, 2025
@rustbotrustbot added this to the 1.87.0 milestone Mar 27, 2025
@bors

bors commented Mar 27, 2025

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 632ce38 with merge ecb170a...

rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 27, 2025
Rollup merge of rust-lang#130883 - madsmtm:env-var-query, r=petrochenkov
Add environment variable query
Generally, `rustc` prefers command-line arguments, but in some cases, an environment variable really is the most sensible option. We should make sure that this works properly with the compiler's change-tracking mechanisms, such that changing the relevant environment variable causes a rebuild.
This PR is a first step forwards in doing that.
Part of the work needed to do rust-lang#118204, see rust-lang#129342 for some discussion.
r? ``@petrochenkov``
@madsmtm
madsmtm deleted the env-var-query branch March 27, 2025 13:05
christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull request Jan 27, 2026
…ochenkov
Move versioned Apple LLVM targets from `rustc_target` to `rustc_codegen_ssa`
Fully specified LLVM targets contain the OS version on macOS/iOS/tvOS/watchOS/visionOS, and this version depends on the deployment target environment variables like `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET` etc.
We would like to move this to later in the compilation pipeline, both because it feels impure to access environment variables when fetching target information, but mostly because we need access to more information from rust-lang/rust#130883 to do rust-lang/rust#118204. See also rust-lang/rust#129342 (comment) for some discussion.
The first and second commit does the actual refactor, it should be a non-functional change, the third commit adds diagnostics for invalid deployment targets, which are now possible to do because we have access to the session.
Tested with the same commands as in rust-lang/rust#130435.
r? ``````@petrochenkov``````
christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull request Jan 27, 2026
…ochenkov
Move versioned Apple LLVM targets from `rustc_target` to `rustc_codegen_ssa`
Fully specified LLVM targets contain the OS version on macOS/iOS/tvOS/watchOS/visionOS, and this version depends on the deployment target environment variables like `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET` etc.
We would like to move this to later in the compilation pipeline, both because it feels impure to access environment variables when fetching target information, but mostly because we need access to more information from rust-lang/rust#130883 to do rust-lang/rust#118204. See also rust-lang/rust#129342 (comment) for some discussion.
The first and second commit does the actual refactor, it should be a non-functional change, the third commit adds diagnostics for invalid deployment targets, which are now possible to do because we have access to the session.
Tested with the same commands as in rust-lang/rust#130435.
r? ``````@petrochenkov``````
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant 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.

7 participants

@madsmtm@rust-log-analyzer@bors@petrochenkov@clubby789@bjorn3@rustbot