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

Detect and report usage of environment variables at build time and run time. #44074

Closed
joshtriplett opened this issue Aug 24, 2017 · 3 comments · Fixed by #71858
Closed

Detect and report usage of environment variables at build time and run time. #44074

joshtriplett opened this issue Aug 24, 2017 · 3 comments · Fixed by #71858
Labels
A-reproducibility Area: Reproducible / deterministic builds C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@joshtriplett
Copy link
Member

(As discussed with @wycats on IRC.)

Rustc should have an option to obtain a list, as part of building, of every environment variable that was requested via the env! or option_env! macros (tagged accordingly with "required" or "optional"), as well as providing a way to instrument intrinsics like env::var called by build.rs scripts. This will make it more feasible to detect (and lint about) the usage of environment variables only set by newer build bits (e.g. Cargo) without an appropriate versioned dependency; for instance, it'd be possible to say "you use the $CARGO_FOO environment variable without a dependency on a version of Cargo that supplies $CARGO_FOO. This would also help people doing reproducible builds to see what environment variables might affect a build.

This will never be perfect; it can't capture variables read from native code or from code that goes out of its way to bypass the normal mechanism (e.g. calling libc::getenv.) However, this would still catch many common cases.

@wycats
Copy link
Contributor

wycats commented Aug 24, 2017

Just following up to say that this is an accurate reflection of what we discussed.

As @joshtriplett said, it's part of a larger goal to detect usage of newer Cargo features on older versions of Cargo and let the user know which version of Cargo they need to upgrade to.

For keys in Cargo.toml, it's enough to simply keep a list of when a particular key was added (since Cargo is the one parsing Cargo.toml).

But if Cargo adds an environment variable, we need some compiler help to detect that the environment variable was used from Rust code.

@alexcrichton
Copy link
Member

I believe this may be a dupe of #40364?

@shepmaster shepmaster added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 25, 2017
@joshtriplett
Copy link
Member Author

@alexcrichton They're related, though I think #40364 seems more narrow in scope.

@jonas-schievink jonas-schievink added the A-reproducibility Area: Reproducible / deterministic builds label Aug 19, 2019
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 25, 2020
Print environment variables accessed by rustc as special comments into depinfo files

So cargo (and perhaps others tools) can use them for linting (at least) or for actually rebuilding crates on env var changes.

---
I've recently observed one more forgotten environment variable in a build script rust-lang@8a77d1c and thought it would be nice to provide the list of accessed variables to cargo automatically as a part of depinfo.

Unsurprisingly, I wasn't the first who had this idea - cc rust-lang#70517 rust-lang#40364 rust-lang#44074.

Also, there are dozens of uses of `(option_)env!` in rustc repo and, like, half of them are not registered in build scripts.

---
Description:
- depinfo files are extended with special comments containing info about environment variables accessed during compilation.
- Comment format for environment variables with successfully retrieved value: `# env-dep:KEY=VALUE`.
- Comment format for environment variables without successfully retrieved value: `# env-dep:KEY` (can happen with `option_env!`).
- `KEY` and `VALUE` are minimally escaped (`\n`, `\r`, `\\`) so they don't break makefile comments and can be unescaped by anything that can unescape standard `escape_default` and friends.

FCP report: rust-lang#71858 (comment)

Closes rust-lang#70517
Closes rust-lang#40364
Closes rust-lang#44074
A new issue in the cargo repo will be needed to track the cargo side of this feature.

r? @ehuss
@bors bors closed this as completed in 1033351 Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-reproducibility Area: Reproducible / deterministic builds C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants