rust-analyzer: Build cache invalidated due to differences in `PATH` environment variable

When the build script is executed, there is a difference in the PATH environment variable depending on whether it is invoked from:

When invoked from Windows Terminal, VSCode Terminal, or VSCode’s tasks.json: The PATH environment variable has C:\Program Files\PowerShell\7 added at the beginning.

When invoked from rust-analyzer in VSCode: The PATH environment variable does not have C:\Program Files\PowerShell\7 added at the beginning.

Due to this difference, the build script that sets cargo:rerun-if-env-changed=PATH is having its cache invalidated, leading to slower builds.

I confirmed that this issue occurs with the zstd-sys crate, but since the cc crate, which is used by this crate, is the one setting rerun-if-env-changed, I believe other crates using the cc crate might be experiencing the same problem.


rust-analyzer version: 0.3.2062-standalone (c9109f23d 2024-08-04)

rustc version: rustc 1.80.0 (051478957 2024-07-21)

editor or extension: VSCode 1.92.0 ( rust-analyzer extesnion v0.3.2062)

Version: 1.92.0 (user setup)
Commit: b1c0a14de1414fcdaa400695b4db1c0799bc3124
Date: 2024-07-31T23:26:45.634Z (5 days ago)
Electron: 30.1.2
ElectronBuildId: 9870757
Chromium: 124.0.6367.243
Node.js: 20.14.0
V8: 12.4.254.20-electron.0
OS: Windows_NT x64 10.0.22631

code snippet to reproduce:

  1. Initialize the project with cargo init and add the following content to Cargo.toml:
[dependencies]
zstd = "0.13.2"
  1. Build the project by running cargo build from the VSCode terminal.

  2. Add a comment to main.rs, making it look like this:

fn main() {
    println!("Hello, world!");
    // This is a comment
}
  1. Place the cursor before the beginning of the comment line, and press Ctrl+Space to display the completion suggestions from rust-analyzer.

  2. Next, run cargo build --verbose from the VSCode terminal to rebuild the project.

As shown below, a change in the PATH environment variable is detected, invalidating the zstd-sys cache, resulting in an unnecessary rebuild.

Fresh jobserver v0.1.32
Fresh cc v1.1.7
Fresh pkg-config v0.3.30
Dirty zstd-sys v2.0.13+zstd.1.5.6: the env variable PATH changed
Compiling zstd-sys v2.0.13+zstd.1.5.6
...

If the rust-analyzer extension is disabled, changing main.rs and running cargo build --verbose will not cause zstd-sys to be rebuilt.

Fresh jobserver v0.1.32
Fresh cc v1.1.7
Fresh pkg-config v0.3.30
Fresh zstd-sys v2.0.13+zstd.1.5.6
...

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 18 (7 by maintainers)

Most upvoted comments

I am also affected by this, because of a dependency to the ring crate, which also rebuilds when PATH changes.

For me the problem goes away when disabling rust-analyzer, but since thats not an option I tried and found an workaround: Just tell it to use a different build folder. I did this by adding this entry to my VS Code settings.json:

"rust-analyzer.cargo.targetDir": true

This will of double the amounts builds and build files on disk, but it seems to solve the conflicts between the rust-analyzer builds and the normal terminal builds.

After running cargo build in PowerShell and then running cargo build in Command Prompt, a rebuild did occur. Indeed, this seems more like an issue with PowerShell or VSCode rather than with rust-analyzer.

According to this comment, PowerShell’s behavior of modifying the PATH environment variable is now unnecessary and is causing other issues, with potential fixes being considered. If this is resolved, the rebuild problem might also go away.

For now, I’ll use "rust-analyzer.cargo.targetDir": true.

Thank you very much.