Skip to content

Build scripts don't rerun if [env] in config.toml is changed #14350

@Radbuglet

Description

@Radbuglet

Problem

Build scripts should automatically re-run if environment variables registered with cargo::rerun-if-env-changed directives change. While this feature works well with environment variables inherited by the cargo process, it doesn't work with environment variables provided by the [env] section of a cargo.toml file.

Steps

Here's my .cargo/config.toml:

[env]
MY_ENV_VAR = "value 1"

...here's my build.rs:

println!("cargo::warning={:?}", std::env::var("MY_ENV_VAR").ok());
println!("cargo::rerun-if-env-changed=MY_ENV_VAR");

...and here's a sample shell session:

$ cargo clean
     Removed 0 files
$ cargo build
   Compiling foo v0.1.0 (/Users/riley/Desktop/foo)
warning: foo@0.1.0: Some("value 1")
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.98s
$ # `MY_ENV_VAR` in `config.toml` updated to "value 2"
$ cargo build
warning: foo@0.1.0: Some("value 1")
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
$ cargo clean
     Removed 59 files, 2.1MiB total
$ cargo build
   Compiling foo v0.1.0 (/Users/riley/Desktop/foo)
warning: foo@0.1.0: Some("value 2")
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.45s

Possible Solution(s)

No response

Notes

Profile environment variables are not read until apply_env_config is ran since that function is the sole caller of GlobalContext::env_config() and that's the only method which references the EnvConfig type. Perhaps only the process environment was processed at a previous step?

Version

cargo 1.80.0 (376290515 2024-07-16)
release: 1.80.0
commit-hash: 37629051518c3df9ac2c1744589362a02ecafa99
commit-date: 2024-07-16
host: aarch64-apple-darwin
libgit2: 1.7.2 (sys:0.18.3 vendored)
libcurl: 8.4.0 (sys:0.4.72+curl-8.6.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 14.3.1 [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions