Skip to content

Commit 1b77ac6

Browse files
authored
Document that cargo automatically registers variables used in env! macro to trigger rebuilds (#15062)
Initially discussed in https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/env!.2Foption_env!.20are.20tracked.2C.20can.20documentation.20be.20improved.3F/near/491793318 ### What does this PR try to resolve? There is no documentation that I can find which indicates that cargo will rebuild for changes in the variables used in `env!` macros. Because this was not always the case, when searching for information on this, the main result indicates otherwise https://users.rust-lang.org/t/should-env-cause-rebuild-is-env-var-changes/18013. Users misled by this may turn to `rerun-if-changed-env` in a build script to trigger rebuilds, so this documentation is a useful place to catch users and indicate that it isn't needed for `env!` macros. ### How should we test and review this PR? I'm not sure how to test the new relative link but I matched it to the existing `[env-macro]: ../../std/macro.env.html`. My main concern when writing this was whether it was clear that this applies to `env!` macro usage within the main code of the crate and not just in `build.rs`.
2 parents 54df3c7 + 706e4c8 commit 1b77ac6

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/doc/src/reference/build-scripts.md

+7
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,13 @@ variables like `TARGET` that [Cargo sets for build scripts][build-env]. The
403403
environment variables in use are those received by `cargo` invocations, not
404404
those received by the executable of the build script.
405405

406+
As of 1.46, using [`env!`][env-macro] and [`option_env!`][option-env-macro] in
407+
source code will automatically detect changes and trigger rebuilds.
408+
`rerun-if-env-changed` is no longer needed for variables already referenced by
409+
these macros.
410+
411+
[option-env-macro]: ../../std/macro.option_env.html
412+
406413
## The `links` Manifest Key
407414

408415
The `package.links` key may be set in the `Cargo.toml` manifest to declare

0 commit comments

Comments
 (0)