Why is cargo recompiling for every change

314 views Asked by At

Recently I noticed that with every change I make to my project, cargo build recompiles a good chunk of dependencies. I ran the build with the verbose option and I see that it is caused by x11-dl which uses pkg-config.

Dirty x11-dl v2.21.0: the env variable PKG_CONFIG_PATH changed
Compiling x11-dl v2.21.0
2023-11-28T16:00:29.568162Z DEBUG cargo::core::compiler::fingerprint: new local fingerprints deps "/home/m2johnson/.cargo/registry/src/index.crates.io-6f17d22bba15001f/x11-dl-2.21.0"
2023-11-28T16:00:29.568527Z DEBUG cargo::core::compiler::fingerprint: write fingerprint (59a258ee54e4dd64) : /home/m2johnson/project/target/debug/.fingerprint/x11-dl-63f654e4528e2978/run-build-script-build-script-build
     Running `/home/m2johnson/.rustup/toolchains/1.73.0-x86_64-unknown-linux-gnu/bin/rustc --crate-name x11_dl --edition=2021 /home/m2johnson/.cargo/registry/src/index.crates.io-6f17d22bba15001f/x11-dl-2.21.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=309 --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C debuginfo=2 -C debug-assertions=on -C metadata=a622a30e33ea17c3 -C extra-filename=-a622a30e33ea17c3 --out-dir /home/m2johnson/project/target/debug/deps -C linker=clang -L dependency=/home/m2johnson/project/target/debug/deps --extern libc=/home/m2johnson/project/target/debug/deps/liblibc-eaa316218c225a80.rmeta --extern once_cell=/home/m2johnson/project/target/debug/deps/libonce_cell-d50086842dfc3ee6.rmeta --cap-lints allow -C link-arg=-fuse-ld=lld -l dl`

What I've tried:

  • I saved a file in the project with no changes and it seems to give the same "Dirty" warning with the same fingerprint calculated.
  • I checked my PKG_CONFIG_PATH in the terminal and with a build.rs file and they were identical.
  • I closed vscode and ran entirely from the terminal. This did seem to stop the recompile. I guess this means that either vscode (rust-analyser) is modifying PKG_CONFIG_PATH or that it isn't reading the value when its started (i.e. clearing it).

So, how does the change detection work? I've traced it back to here so far but no luck yet in understanding whats going on.

This is with Rust 1.73.0

rustc 1.73.0 (cc66ad468 2023-10-03)
1

There are 1 answers

1
jonathan On

I had a similar problem with lots of dependencies being needlessly recompiled. For me, it was fixed when I disabled rust-analyzer in vscode, but this is not ideal.

A better solution for me was running the task rust: cargo run from the command menu (the same as clicking the run button above the main fn). This solved the issue.

You can also create a shortcut for this:

{
    "key": "ctrl+[Backslash] r",
    "command": "workbench.action.tasks.runTask",
    "args": "rust: cargo run"
}