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

[WIP] Fix: Cargo fails to detect environment variable #13596

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/cargo/core/compiler/fingerprint/dirty_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum DirtyReason {
MetadataChanged,
ConfigSettingsChanged,
CompileKindChanged,
EnvConfigChanged,
LocalLengthsChanged,
PrecalculatedComponentsChanged {
old: String,
Expand Down Expand Up @@ -172,6 +173,9 @@ impl DirtyReason {
DirtyReason::CompileKindChanged => {
s.dirty_because(unit, "the rustc compile kind changed")
}
DirtyReason::EnvConfigChanged => {
s.dirty_because(unit, "the environment variable changed")
}
DirtyReason::LocalLengthsChanged => {
s.dirty_because(unit, "the local lengths changed")?;
s.note(
Expand Down
24 changes: 22 additions & 2 deletions src/cargo/core/compiler/fingerprint/mod.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder some alternatives may be like:

  • Have a set of environment variables Cargo internally sets for rustc, and removes them from Cargo's dep info here. This might need to take cargo::rustc-env build script directive into account, and I feel it will be pretty error-prone as we might miss something when adding more envs.
  • Leverage the unstable --env-set rustc flag from the env sandboxing RFC. However, it is still unstable so Cargo can't use it.

Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,14 @@ pub fn prepare_target(
let mtime_on_use = build_runner.bcx.gctx.cli_unstable().mtime_on_use;
let dirty_reason = compare_old_fingerprint(unit, &loc, &*fingerprint, mtime_on_use, force);

let Some(dirty_reason) = dirty_reason else {
return Ok(Job::new_fresh());
let dirty_reason = match dirty_reason {
Some(dr) => dr,
None => {
let Some(dr) = env_config_modified(bcx.gctx) else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this does fix the case described in #13280, the actual problem is not addressed: Dep-info from Cargo doesn't track # env-var: from rustc's dep-info files.

It's simple to reproduce delete the .cargo/config.toml from the case in #13280, then run

cargo r --config 'env.Z="1"'
cargo r --config 'env.Z="2"'
cargo r --config 'env.Z="3"'

Only Z=1 would be captured, and Cargo never recompile after the first invocation. Hence, the current patch just solves half of it and buries the real bug deeper.

return Ok(Job::new_fresh());
};
dr
}
};

// We're going to rebuild, so ensure the source of the crate passes all
Expand Down Expand Up @@ -2231,3 +2237,17 @@ pub fn parse_rustc_dep_info(rustc_dep_info: &Path) -> CargoResult<RustcDepInfo>
Ok(ret)
}
}

/// Detects if environment variables from config `[env]` is newly seted.
fn env_config_modified(gctx: &crate::GlobalContext) -> Option<DirtyReason> {
for (key, value) in gctx.env_config().unwrap().iter() {
if !gctx.env().any(|(k, _)| k == key) {
continue;
}

if !value.is_force() && gctx.env().find(|(k, _)| k == key).is_some() {
return Some(DirtyReason::EnvConfigChanged);
}
}
None
}
29 changes: 29 additions & 0 deletions tests/testsuite/cargo_env_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,32 @@ fn env_applied_to_target_info_discovery_rustc() {
.with_stderr_contains("MAIN ENV_TEST:from-env")
.run();
}

#[cargo_test]
fn env_reset() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
use std::env;
fn main() {
println!( "{}", env!("ENV_TEST") );
}
"#,
)
.file(
".cargo/config.toml",
r#"
[env]
ENV_TEST = "from-config"
"#,
)
.build();

p.cargo("run").with_stdout_contains("from-config").run();
p.cargo("run")
.env("ENV_TEST", "from-env")
.with_stdout_contains("from-env")
.run();
}