Skip to content

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

Closed

Description

@joshtriplett

(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.

Activity

  1. wycats commented on Aug 24, 2017

    @wycats
    Contributor

    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.

  2. alexcrichton commented on Aug 24, 2017

    @alexcrichton
    Member

    I believe this may be a dupe of #40364?

  3. added
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 25, 2017
  4. joshtriplett commented on Aug 28, 2017

    @joshtriplett
    MemberAuthor

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

  5. added a commit that references this issue on Jun 25, 2020
  6. added a commit that references this issue on Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-reproducibilityArea: Reproducible / deterministic buildsC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions