Skip to content

Forwarding a build.rs env var and running cargo build within the binary causes rebuilds #12403

@rukai

Description

@rukai

Problem

In some of my projects I do the following:

  • forward the env vars from build.rs up to my actual application
  • invoke cargo build from a test or example, this is useful for certain integration tests.

However the combination of these things is causing my application to be rebuilt.

Steps

build.rs:

fn main() {
    let profile = std::env::var("PROFILE").unwrap();
    println!("cargo:rustc-env=PROFILE={profile}");
    println!("cargo:rerun-if-env-changed=PROFILE");
}

main.rs:

fn main() {
    let output = std::process::Command::new(env!("CARGO"))
        .args(["build", "-vv"])
        .output()
        .unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    let stderr = String::from_utf8(output.stderr).unwrap();
    println!("stdout:\n{stdout}\nstderr:\n{stderr}");
}

Then run cargo run -vv to get the following output every time it reruns:

~2/Projects/Crates/foo_bin> cargo run -vv
       Dirty foo v0.1.0 (/home/rukai2/Projects/Crates/foo_bin): the env variable PROFILE changed
   Compiling foo v0.1.0 (/home/rukai2/Projects/Crates/foo_bin)
     Running `/home/rukai2/Projects/Crates/foo_bin/target/debug/build/foo-9cd20279bf15ecca/build-script-build`
[foo 0.1.0] cargo:rustc-env=PROFILE=debug
[foo 0.1.0] cargo:rerun-if-env-changed=PROFILE
     Running `CARGO=/home/rukai/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_BIN_NAME=foo CARGO_CRATE_NAME=foo CARGO_MANIFEST_DIR=/home/rukai2/Projects/Crates/foo_bin CARGO_PKG_AUTHORS='Rukai <rubickent@gmail.com>' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=foo CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/rukai2/Projects/Crates/foo_bin/target/debug/deps:/home/rukai/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/home/rukai/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' OUT_DIR=/home/rukai2/Projects/Crates/foo_bin/target/debug/build/foo-b67a63e31a813826/out PROFILE=debug /home/rukai/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name foo --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=191 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=3c9e043e35410f2e -C extra-filename=-3c9e043e35410f2e --out-dir /home/rukai2/Projects/Crates/foo_bin/target/debug/deps -C linker=clang -C incremental=/home/rukai2/Projects/Crates/foo_bin/target/debug/incremental -L dependency=/home/rukai2/Projects/Crates/foo_bin/target/debug/deps -C link-arg=-fuse-ld=/usr/bin/mold`
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/foo`
stdout:
[foo 0.1.0] cargo:rustc-env=PROFILE=debug
[foo 0.1.0] cargo:rerun-if-env-changed=PROFILE

stderr:
       Dirty foo v0.1.0 (/home/rukai2/Projects/Crates/foo_bin): the env variable PROFILE changed
   Compiling foo v0.1.0 (/home/rukai2/Projects/Crates/foo_bin)
     Running `/home/rukai2/Projects/Crates/foo_bin/target/debug/build/foo-9cd20279bf15ecca/build-script-build`
     Running `CARGO=/home/rukai/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_BIN_NAME=foo CARGO_CRATE_NAME=foo CARGO_MANIFEST_DIR=/home/rukai2/Projects/Crates/foo_bin CARGO_PKG_AUTHORS='Rukai <rubickent@gmail.com>' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=foo CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/rukai2/Projects/Crates/foo_bin/target/debug/deps:/home/rukai/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/home/rukai2/Projects/Crates/foo_bin/target/debug/deps:/home/rukai2/Projects/Crates/foo_bin/target/debug:/home/rukai/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib:/home/rukai/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' OUT_DIR=/home/rukai2/Projects/Crates/foo_bin/target/debug/build/foo-b67a63e31a813826/out PROFILE=debug /home/rukai/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name foo --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=3c9e043e35410f2e -C extra-filename=-3c9e043e35410f2e --out-dir /home/rukai2/Projects/Crates/foo_bin/target/debug/deps -C linker=clang -C incremental=/home/rukai2/Projects/Crates/foo_bin/target/debug/incremental -L dependency=/home/rukai2/Projects/Crates/foo_bin/target/debug/deps -C link-arg=-fuse-ld=/usr/bin/mold`
    Finished dev [unoptimized + debuginfo] target(s) in 0.07s

the reason for the crate being considered dirty is because the env variable PROFILE changed, however you can see in both the outer and inner cargo it is set to the same debug value.

Possible Solution(s)

No response

Notes

No response

Version

~2/Projects/Crates/foo_bin> cargo --version --verbose
cargo 1.71.0 (cfd3bbd8f 2023-06-08)
release: 1.71.0
commit-hash: cfd3bbd8fe4fd92074dfad04b7eb9a923646839f
commit-date: 2023-06-08
host: x86_64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.1 vendored)
libcurl: 8.0.1-DEV (sys:0.4.61+curl-8.0.1 vendored ssl:OpenSSL/1.1.1t)
ssl: OpenSSL 1.1.1t  7 Feb 2023
os: Arch Linux Rolling Release [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-build-scriptsArea: build.rs scriptsA-documenting-cargo-itselfArea: Cargo's documentationC-bugCategory: bugS-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions