-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
fix(build-rs): Implicitly report rerun-if-env-changed for input #14911
Conversation
I made a dedicated `Env::is_present` function in case we want to handle `rerun-if-env-changed` differently in that case.
r? @weihanglo rustbot has assigned @weihanglo. Use |
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.
👍 on direction from me.
cargo::rerun-on-env-changed
is purely additive and doesn't disable a default watch set like cargo::rerun-on-changed
does, so emitting it automatically seems like a good idea.
Although, I'm not sure it's actually needed for the CARGO_
environment? It depends on how aggressive Cargo is at caching the buildscript and if any of this environment can change without necessarily requiring a recompile.
fn is_present(key: &str) -> bool { | ||
var_os(key).is_some() | ||
} | ||
|
||
#[track_caller] | ||
fn var_or_panic(key: &str) -> std::ffi::OsString { |
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.
Changing this to e.g. ENV.require(key)
may be a good idea to keep env access more uniform, and paves the way towards maybe supporting a different protocol in the future.
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 intentionally kept it off.
I see two ways of going
- Minimum interface for what is needed
- Every input being an inherent function on the trait
I went with the former. I only included is_present
on the trait because I was concerned about what the right policy is for re-run if changed in that case. The direction I went with doesn't specialize it but I figured I'd still leave that door open. I'm not seeing a reason to specialize this atm and would rather wait for use cases rather than speculatively adding functionality to the trait.
Since we can add this as an inherent function, we can likely do so without a breaking change (yes, there are caveats). Even then, its not a big deal to make a breaking change.
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.
Especially since the trait/type aren't pub
(yet?) and are just internal helpers.
The only cases I saw us specially handle are cargo/src/cargo/core/compiler/fingerprint/mod.rs Lines 852 to 865 in 2f74b54
and cargo/src/cargo/core/compiler/fingerprint/dep_info.rs Lines 306 to 338 in 2f74b54
Which makes sense. We can set a lot and we likely don't want to re-run if all of it has changed. Some we'll rebuild for anyways because some of the fingerprint has changed but I figure its better to be over aggressive than under aggressive or having to maintain a perfectly curated set of ENVs |
As we abstract aware the env variables, users can't do a good job of reporting these, so we'll do it ourselves. We could make the traits public and take and explicit `env` parameter. I figured we can wait until there is a motivating use case. `build_env` does have a fancier `Env` impl where you pass it HOST and TARGET and it automatically looks up values for those with a fallback scheme.
2a6a33d
to
0227c51
Compare
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.
Thanks! This seems like a great incremental step overall.
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.
Which makes sense. We can set a lot and we likely don't want to re-run if all of it has changed. Some we'll rebuild for anyways because some of the fingerprint has changed but I figure its better to be over aggressive than under aggressive or having to maintain a perfectly curated set of ENVs
Agree with this. While we don't recommend user to hand-write rerun-if-env-changed
with cargo internal envs, as a build script helper crate it should be fine as an implementation detail.
Update cargo 18 commits in 20a443231846b81c7b909691ec3f15eb173f2b18..7847c03965260b5dcc8d93218d6af295a717abb6 2024-12-06 21:56:56 +0000 to 2024-12-13 18:06:39 +0000 - fix(base): Support bases in patches in virtual manifests (rust-lang/cargo#14931) - fix(resolver): Report invalid index entries (rust-lang/cargo#14927) - feat: Implement `--depth workspace` for `cargo tree` command (rust-lang/cargo#14928) - fix(resolver): In errors, show rejected versions over alt versions (rust-lang/cargo#14923) - fix: emit_serialized_unit_graph uses the configured shell (rust-lang/cargo#14926) - fix(script): Don't override the release profile (rust-lang/cargo#14925) - feature(SourceId): use stable hash from rustc-stable-hash (rust-lang/cargo#14917) - fix(resolver): Don't report all versions as rejected (rust-lang/cargo#14921) - fix(resolver): Report unmatched versions, rather than saying no package (rust-lang/cargo#14897) - fix(build-rs): Implicitly report rerun-if-env-changed for input (rust-lang/cargo#14911) - a faster hash for ActivationsKey (rust-lang/cargo#14915) - feat(build-script): Pass CARGO_CFG_FEATURE (rust-lang/cargo#14902) - fix(build-rs): Correctly refer to the item in assert (rust-lang/cargo#14913) - chore: update auto-label to include build-rs crate (rust-lang/cargo#14912) - refactor: use Path::push to construct remap-path-prefix (rust-lang/cargo#14908) - feat(build-rs): Add the 'error' directive (rust-lang/cargo#14910) - fix(build-std): determine root crates by target spec `std:bool` (rust-lang/cargo#14899) - SemVer: Add section on RPIT capturing (rust-lang/cargo#14849)
Update cargo 19 commits in 20a443231846b81c7b909691ec3f15eb173f2b18..769f622e12db0001431d8ae36d1093fb8727c5d9 2024-12-06 21:56:56 +0000 to 2024-12-14 04:27:35 +0000 - test(build-std): dont require rustup (rust-lang/cargo#14933) - fix(base): Support bases in patches in virtual manifests (rust-lang/cargo#14931) - fix(resolver): Report invalid index entries (rust-lang/cargo#14927) - feat: Implement `--depth workspace` for `cargo tree` command (rust-lang/cargo#14928) - fix(resolver): In errors, show rejected versions over alt versions (rust-lang/cargo#14923) - fix: emit_serialized_unit_graph uses the configured shell (rust-lang/cargo#14926) - fix(script): Don't override the release profile (rust-lang/cargo#14925) - feature(SourceId): use stable hash from rustc-stable-hash (rust-lang/cargo#14917) - fix(resolver): Don't report all versions as rejected (rust-lang/cargo#14921) - fix(resolver): Report unmatched versions, rather than saying no package (rust-lang/cargo#14897) - fix(build-rs): Implicitly report rerun-if-env-changed for input (rust-lang/cargo#14911) - a faster hash for ActivationsKey (rust-lang/cargo#14915) - feat(build-script): Pass CARGO_CFG_FEATURE (rust-lang/cargo#14902) - fix(build-rs): Correctly refer to the item in assert (rust-lang/cargo#14913) - chore: update auto-label to include build-rs crate (rust-lang/cargo#14912) - refactor: use Path::push to construct remap-path-prefix (rust-lang/cargo#14908) - feat(build-rs): Add the 'error' directive (rust-lang/cargo#14910) - fix(build-std): determine root crates by target spec `std:bool` (rust-lang/cargo#14899) - SemVer: Add section on RPIT capturing (rust-lang/cargo#14849)
Update cargo 19 commits in 20a443231846b81c7b909691ec3f15eb173f2b18..769f622e12db0001431d8ae36d1093fb8727c5d9 2024-12-06 21:56:56 +0000 to 2024-12-14 04:27:35 +0000 - test(build-std): dont require rustup (rust-lang/cargo#14933) - fix(base): Support bases in patches in virtual manifests (rust-lang/cargo#14931) - fix(resolver): Report invalid index entries (rust-lang/cargo#14927) - feat: Implement `--depth workspace` for `cargo tree` command (rust-lang/cargo#14928) - fix(resolver): In errors, show rejected versions over alt versions (rust-lang/cargo#14923) - fix: emit_serialized_unit_graph uses the configured shell (rust-lang/cargo#14926) - fix(script): Don't override the release profile (rust-lang/cargo#14925) - feature(SourceId): use stable hash from rustc-stable-hash (rust-lang/cargo#14917) - fix(resolver): Don't report all versions as rejected (rust-lang/cargo#14921) - fix(resolver): Report unmatched versions, rather than saying no package (rust-lang/cargo#14897) - fix(build-rs): Implicitly report rerun-if-env-changed for input (rust-lang/cargo#14911) - a faster hash for ActivationsKey (rust-lang/cargo#14915) - feat(build-script): Pass CARGO_CFG_FEATURE (rust-lang/cargo#14902) - fix(build-rs): Correctly refer to the item in assert (rust-lang/cargo#14913) - chore: update auto-label to include build-rs crate (rust-lang/cargo#14912) - refactor: use Path::push to construct remap-path-prefix (rust-lang/cargo#14908) - feat(build-rs): Add the 'error' directive (rust-lang/cargo#14910) - fix(build-std): determine root crates by target spec `std:bool` (rust-lang/cargo#14899) - SemVer: Add section on RPIT capturing (rust-lang/cargo#14849)
What does this PR try to resolve?
As we abstract away the env variables, users can't do a good job of
reporting these, so we'll do it ourselves.
How should we test and review this PR?
This isn't the shortest path to this solution because I had originally designed this like more like
build_env
but in a way to allow users to choose what policies they want (rather than baking in their HOST/TARGET scheme).I decided to not support a custom
env
yet to make this more incremental and to see what feedback we get but I left the implementation in a way that would make it easy to support it.I made
is_present
part ofEnv
because I was unsure if callers of the API would want to handle that differently forrerun-if-env-changed
or other parts.Additional information
CC @CAD97