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

CARGO_RUSTC_CURRENT_DIR is causing forced rebuilds #14798

Open
ehuss opened this issue Nov 8, 2024 · 11 comments · May be fixed by #14811
Open

CARGO_RUSTC_CURRENT_DIR is causing forced rebuilds #14798

ehuss opened this issue Nov 8, 2024 · 11 comments · May be fixed by #14811
Labels
A-environment-variables Area: environment variables A-rebuild-detection Area: rebuild detection and fingerprinting C-bug Category: bug S-triage Status: This issue is waiting on initial triage.

Comments

@ehuss
Copy link
Contributor

ehuss commented Nov 8, 2024

Problem

In the cargo repo, there is an environment override setting the CARGO_RUSTC_CURRENT_DIR environment variable:

[env]
# HACK: Until this is stabilized, `snapbox`s polyfill could get confused
# inside of the rust-lang/rust repo because it looks for the furthest-away `Cargo.toml`
CARGO_RUSTC_CURRENT_DIR = { value = "", relative = true }

This is causing testsuite to always be considered dirty on my system. I am getting:

FsStatusOutdated(StaleItem(ChangedEnv 
{ var: "CARGO_RUSTC_CURRENT_DIR", 
previous: Some("/Users/eric/Proj/rust/cargo"), 
current: Some("/Users/eric/Proj/rust/cargo/") 
}

Note the difference in trailing /

Steps

  1. Check out rust-lang/cargo
  2. cargo test --test testsuite --no-run
  3. cargo test --test testsuite --no-run -- This should build testsuite again.

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.84.0-nightly (031049782 2024-11-01)
release: 1.84.0-nightly
commit-hash: 0310497822a7a673a330a5dd068b7aaa579a265e
commit-date: 2024-11-01
host: aarch64-apple-darwin
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.7.1 (sys:0.4.74+curl-8.9.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 14.6.1 [64-bit]
@ehuss ehuss added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Nov 8, 2024
@epage
Copy link
Contributor

epage commented Nov 8, 2024

I'm not able to reproduce this on my system on stable

❯ cargo test --test testsuite --no-run
warning: ignoring `resolver` config table without `-Zmsrv-policy`
   Compiling cargo v0.85.0 (/home/epage/src/personal/cargo)
   Compiling cargo-util v0.2.17 (/home/epage/src/personal/cargo/crates/cargo-util)
   Compiling cargo-credential v0.4.7 (/home/epage/src/personal/cargo/credential/cargo-credential)
   Compiling crates-io v0.40.7 (/home/epage/src/personal/cargo/crates/crates-io)
   Compiling cargo-util-schemas v0.7.1 (/home/epage/src/personal/cargo/crates/cargo-util-schemas)
   Compiling rustfix v0.9.0 (/home/epage/src/personal/cargo/crates/rustfix)
   Compiling cargo-platform v0.1.9 (/home/epage/src/personal/cargo/crates/cargo-platform)
   Compiling cargo-credential-libsecret v0.4.10 (/home/epage/src/personal/cargo/credential/cargo-credential-libsecret)
   Compiling cargo-test-support v0.6.0 (/home/epage/src/personal/cargo/crates/cargo-test-support)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 37.46s
  Executable tests/testsuite/main.rs (target/debug/deps/testsuite-b494325e491a8e98)

cargo on  clippy is 📦 v0.85.0 took 37s
❯ cargo test --test testsuite --no-run
warning: ignoring `resolver` config table without `-Zmsrv-policy`
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.10s
  Executable tests/testsuite/main.rs (target/debug/deps/testsuite-b494325e491a8e98)

@epage
Copy link
Contributor

epage commented Nov 8, 2024

Reproduces on nightly

cargo on  clippy is 📦 v0.85.0
❯ cargo +nightly test --test testsuite --no-run
   Compiling cargo-util v0.2.17 (/home/epage/src/personal/cargo/crates/cargo-util)
   Compiling cargo v0.85.0 (/home/epage/src/personal/cargo)
   Compiling cargo-credential v0.4.7 (/home/epage/src/personal/cargo/credential/cargo-credential)
   Compiling crates-io v0.40.7 (/home/epage/src/personal/cargo/crates/crates-io)
   Compiling cargo-util-schemas v0.7.1 (/home/epage/src/personal/cargo/crates/cargo-util-schemas)
   Compiling rustfix v0.9.0 (/home/epage/src/personal/cargo/crates/rustfix)
   Compiling cargo-platform v0.1.9 (/home/epage/src/personal/cargo/crates/cargo-platform)
   Compiling cargo-test-support v0.6.0 (/home/epage/src/personal/cargo/crates/cargo-test-support)
   Compiling cargo-test-macro v0.3.3 (/home/epage/src/personal/cargo/crates/cargo-test-macro)
   Compiling cargo-credential-libsecret v0.4.10 (/home/epage/src/personal/cargo/credential/cargo-credential-libsecret)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 32.26s
  Executable tests/testsuite/main.rs (target/debug/deps/testsuite-ffba239b1f3ab958)

cargo on  clippy is 📦 v0.85.0 took 32s
❯ cargo +nightly test --test testsuite --no-run
   Compiling cargo v0.85.0 (/home/epage/src/personal/cargo)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 4.08s
  Executable tests/testsuite/main.rs (target/debug/deps/testsuite-ffba239b1f3ab958)

@epage
Copy link
Contributor

epage commented Nov 8, 2024

Wonder if this was #14701

@epage
Copy link
Contributor

epage commented Nov 8, 2024

My nightly isn't too old yet if I run out of master, I don't see it

cargo-14701 on  main is 📦 v0.1.0
❯ nargo check
warning: ignoring `resolver` config table without `-Zmsrv-policy`
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
     Running `/home/epage/src/personal/cargo/target/debug/cargo -Zscript -Zmsrv-policy check`
    Checking cargo-14701 v0.1.0 (/home/epage/src/personal/dump/cargo-14701)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.09s

cargo-14701 on  main is 📦 v0.1.0
❯ nargo check
warning: ignoring `resolver` config table without `-Zmsrv-policy`
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.10s
     Running `/home/epage/src/personal/cargo/target/debug/cargo -Zscript -Zmsrv-policy check`
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s

I even switched to 8315873 and didn't see it

@ehuss
Copy link
Contributor Author

ehuss commented Nov 8, 2024

A reproduction without the cargo repo:

// src/main.rs
fn main() {
    println!("{:?}", option_env!("CARGO_RUSTC_CURRENT_DIR"));
}
# .cargo/config.toml
[env]
CARGO_RUSTC_CURRENT_DIR = { value = "", relative = true }

cargo build

@epage
Copy link
Contributor

epage commented Nov 8, 2024

When I run nargo, I'm using the default rustc.

I wonder if rustc changed something about how it records paths...

@epage
Copy link
Contributor

epage commented Nov 8, 2024

I switched my default rustc to nightly and still not reproducing this out of cargo master.

@epage
Copy link
Contributor

epage commented Nov 8, 2024

   0.006690626s  INFO main:exec:compile_ws:compile:compile{unit=Unit { pkg: Package { id: PackageId { name: "cargo-147
01", version: "0.1.0", source: "/home/epage/src/personal/dump/cargo-14701" }, ..: ".." }, target: TargetInner { name:
"cargo-14701", doc: true, ..: with_path("/home/epage/src/personal/dump/cargo-14701/src/main.rs", Edition2021) }, profi
le: Profile { debuginfo: Resolved(LineTablesOnly), split_debuginfo: Some("unpacked"), ..: default_dev() }, kind: Host,
 mode: Check { test: false }, features: [], rustflags: [], rustdocflags: [], links_overrides: {}, artifact: false, art
ifact_target_for_features: None, is_std: false, dep_hash: 2202906307356721367 } force_rebuild=false}:prepare_target{fo
rce=false package_id=cargo-14701 v0.1.0 (/home/epage/src/personal/dump/cargo-14701) target="cargo-14701"}: cargo::core
::compiler::fingerprint:     dirty: FsStatusOutdated(StaleItem(ChangedEnv { var: "CARGO_RUSTC_CURRENT_DIR", previous:
Some("/home/epage/src/personal/dump/cargo-14701"), current: Some("/home/epage/src/personal/dump/cargo-14701/") }))
       Dirty cargo-14701 v0.1.0 (/home/epage/src/personal/dump/cargo-14701): the environment variable CARGO_RUSTC_CURR
ENT_DIR changed
     Running `CARGO=/home/epage/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo CARGO_BIN_NAME=cargo-147
01 CARGO_CRATE_NAME=cargo_14701 CARGO_MANIFEST_DIR=/home/epage/src/personal/dump/cargo-14701 CARGO_MANIFEST_PATH=/home
/epage/src/personal/dump/cargo-14701/Cargo.toml CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CA
RGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=cargo-14701 CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CA
RGO_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 CARGO_RUSTC_CURRENT_DIR=/home/epage/src/personal/dump/cargo-1
4701 LD_LIBRARY_PATH='/home/epage/src/personal/dump/cargo-14701/target/debug/deps:/home/epage/.rustup/toolchains/night
ly-x86_64-unknown-linux-gnu/lib' foo=bar /home/epage/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --c
rate-name cargo_14701 --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-
incompat --crate-type bin --emit=dep-info,metadata -C embed-bitcode=no -C debuginfo=line-tables-only -C split-debuginf
o=unpacked --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values())' -C metadata=d0f79a6560e2ae7d -C extra-filena
me=-d0f79a6560e2ae7d --out-dir /home/epage/src/personal/dump/cargo-14701/target/debug/deps -C incremental=/home/epage/
src/personal/dump/cargo-14701/target/debug/incremental -L dependency=/home/epage/src/personal/dump/cargo-14701/target/
debug/deps`

Specifically

dirty: FsStatusOutdated(StaleItem(ChangedEnv { var: "CARGO_RUSTC_CURRENT_DIR", previous:
Some("/home/epage/src/personal/dump/cargo-14701"), current: Some("/home/epage/src/personal/dump/cargo-14701/") }))

yet the env variable we pass to rustc is different.

@epage
Copy link
Contributor

epage commented Nov 8, 2024

Ah, this is specific to CARGO_RUSTC_CURRENT_DIR. We decided to not stabilized it in #13644 but I never removed the code. The [env] is setting it one way and then we are overriding it a different way.

I'll remove that code.

epage added a commit to epage/cargo that referenced this issue Nov 8, 2024
@weihanglo
Copy link
Member

This is indeed related to #14701, and was pointed out as a caveat #14701 (comment).

@weihanglo weihanglo added A-rebuild-detection Area: rebuild detection and fingerprinting A-environment-variables Area: environment variables labels Nov 8, 2024
@weihanglo
Copy link
Member

If any "internal" environment Cargo sets for crates is set the [env] table, it would be captured in dep-info files. If values from [env] and from Cargo internal don't match, Cargo rebuilds.

I think it is generally a mistake to set Cargo internal envs, as they won't have any effect. We could do something to improve the situation,

  • Warn if those are set.
  • Filter out those when needed, like when fingerprint comparison.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-environment-variables Area: environment variables A-rebuild-detection Area: rebuild detection and fingerprinting C-bug Category: bug S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants