Skip to content

cargo:rerun-if-env-changed doesn't work with env configuration #10358

Closed
@bsilver8192

Description

Problem

When a build.rs script emits cargo:rerun-if-env-changed, it is not re-run when the value of the specified variable is changed via the env configuration.

Steps

build.rs:

pub fn main() {
    println!("cargo:rerun-if-env-changed=FOO");
    if let Ok(foo) = std::env::var("FOO") {
        assert!(&foo != "bad");
    }
}

.cargo/config.toml:

[env]
FOO = "good"
  1. Run cargo build, it succeeds.
  2. Change .cargo/config.toml to this:
[env]
FOO = "bad"
  1. Run cargo build again, and it uses the cached build and succeeds. It should run build.rs again, and it should fail.

Workaround

  1. Run FOO=bar cargo build, ignore the result.
  2. Run cargo build again, now it runs build.rs with FOO=bad from the config and fails as expected.

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.58.0 (7f08ace4f 2021-11-24)
release: 1.58.0
commit-hash: 7f08ace4f1305de7f3b1b0e2f765911957226bd4
commit-date: 2021-11-24
host: x86_64-unknown-linux-gnu
libgit2: 1.3.0 (sys:0.13.23 vendored)
libcurl: 7.80.0-DEV (sys:0.4.51+curl-7.80.0 vendored ssl:OpenSSL/1.1.1l)
os: Debian 11 (bullseye) [64-bit]

Metadata

Assignees

No one assigned

    Labels

    A-build-scriptsArea: build.rs scriptsA-environment-variablesArea: environment variablesA-rebuild-detectionArea: rebuild detection and fingerprintingC-bugCategory: bugS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions