rust-analyzer: Rust analyzer "cargo check" blocks debug builds

I’m working on a project where iterative compile times are important. If i make a change, save it, and immediately start a debug build then it will almost always get blocked by rust analyzer’s invocation of the “cargo check” command. My build then needs to wait until cargo check completes.

Blocking doesn’t occur if I run a release build. This creates the weird situation where release builds are actually faster to complete:

Finished release [optimized] target(s) in 0.80s

vs

Finished dev [unoptimized + debuginfo] target(s) in 1.31s

This is especially troublesome for initial cargo check runs, which can block builds for minutes at a time.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 30 (15 by maintainers)

Commits related to this issue

Most upvoted comments

The workarounds are:

  • set "rust-analyzer.checkOnSave.enable": false (you should still get errors in the editor if you invoke build manually)
  • set "rust-analyzer.checkOnSave.extraArgs": ["--target-dir", "/path/to/proect/target/check"]

I think it’s fair to close this: although the probelm is real, I don’t think we’ll be able to come up with anything better than the current set of work-arounds.

Documenting for anyone else running into this issue:

I was having lots of spurious rebuilds for the openssl-sys package (and some dependees of it). It turned out to be because I was setting $PKG_CONFIG_PATH to something custom in my .bashrc. I run VSCode via the Unity launcher, not from a terminal, so it was not getting the setting from .bashrc… But as soon as I opened a terminal, whether within VSCode or not, that terminal had a different $PKG_CONFIG_PATH and so triggered a rebuild.

Removing the line in my .bashrc that sets $PKG_CONFIG_PATH fixed it.

Here’s a minimal project to reproduce:

src/main.rs

fn main() {
    println!("Hello, world6!");
}

src/Cargo.toml

[package]
name = "rebuild-repro"
version = "0.1.0"
[dependencies]
openssl-sys = "0.9.77"

~/.bashrc

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig"

cargo 1.67.0-nightly (eb5d35917 2022-11-17) VSCode 1.73.1

And here’s how I debugged:

  1. In an editor pane, change the program in a trivial way and save.

  2. Note that cargo check takes a few seconds.

  3. In a terminal pane, run cargo build. Note that it takes a few seconds, and rebuilds openssl-sys.

  4. Repeat back-and-forth, noting the slowness each time.

  5. In the terminal pane, run:

    CARGO_LOG=cargo::core::compiler::fingerprint=info RUST_LOG=trace cargo -vv build > ~/slow-rebuild-log-1.txt 2>&1

  6. In ~/slow-rebuild-log-1.txt, notice the lines:

[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint]     err: current filesystem status shows we're outdated
[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint] fingerprint error for openssl-sys v0.9.77/RunCustomBuild/TargetInner { ..: custom_build_target("build-script-main", "/home/jsha/.cargo/registry/src/index.crates.io-93a9e06d8945f2c0/openssl-sys-0.9.77/build/main.rs", Edition2015) }
[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint]     err: env var `PKG_CONFIG_PATH` changed: previously Some("/home/jsha/local/lib/pkgconfig:/home/jsha/local/lib/pkgconfig::/usr/local/lib/pkgconfig:/usr/local/lib/pkgconfig"), now Some("/home/jsha/local/lib/pkgconfig:/home/jsha/local/lib/pkgconfig:/home/jsha/local/lib/pkgconfig::/usr/local/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/lib/pkgconfig")

Note the mention of PKG_CONFIG_PATH. This happens because openssl-sys has a build script: build.rs. That invokes pkg-config, which emits a bunch of rerun-if-changed commands for Cargo, including cargo:rerun-if-env-changed=PKG_CONFIG.

I noticed that the “new” version of PKG_CONFIG_PATH included some directories that were specific to my user, which led me to look for that variable in my dotfiles and find it in .bashrc.

Hope this is helpful to someone in the future!

Bonus helpful link: https://doc.rust-lang.org/stable/cargo/faq.html#why-is-cargo-rebuilding-my-code

slow-rebuild-log-1.txt
    Blocking waiting for file lock on build directory
[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint] dependency on `build_script_main` is newer than we are 1669150759.255947158s > 1669150603.416814653s "/home/jsha/.cargo/registry/src/index.crates.io-93a9e06d8945f2c0/openssl-sys-0.9.77"
[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint] fingerprint error for rebuild-repro v0.1.0 (/home/jsha/rust/rebuild-repro)/Build/TargetInner { name: "rebuild-repro", doc: true, ..: with_path("/home/jsha/rust/rebuild-repro/src/main.rs", Edition2021) }
[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint]     err: current filesystem status shows we're outdated
[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint] fingerprint error for openssl-sys v0.9.77/Build/TargetInner { ..: lib_target("openssl-sys", ["lib"], "/home/jsha/.cargo/registry/src/index.crates.io-93a9e06d8945f2c0/openssl-sys-0.9.77/src/lib.rs", Edition2015) }
[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint]     err: current filesystem status shows we're outdated
[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint] fingerprint error for openssl-sys v0.9.77/RunCustomBuild/TargetInner { ..: custom_build_target("build-script-main", "/home/jsha/.cargo/registry/src/index.crates.io-93a9e06d8945f2c0/openssl-sys-0.9.77/build/main.rs", Edition2015) }
[2022-11-22T20:59:21Z INFO  cargo::core::compiler::fingerprint]     err: env var `PKG_CONFIG_PATH` changed: previously Some("/home/jsha/local/lib/pkgconfig:/home/jsha/local/lib/pkgconfig::/usr/local/lib/pkgconfig:/usr/local/lib/pkgconfig"), now Some("/home/jsha/local/lib/pkgconfig:/home/jsha/local/lib/pkgconfig:/home/jsha/local/lib/pkgconfig::/usr/local/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/lib/pkgconfig")
       Fresh autocfg v1.1.0
       Fresh cc v1.0.77
       Fresh pkg-config v0.3.26
   Compiling openssl-sys v0.9.77
       Fresh libc v0.2.137
     Running `/home/jsha/rust/rebuild-repro/target/debug/build/openssl-sys-ea23160e4c72873c/build-script-main`
[openssl-sys 0.9.77] cargo:rustc-cfg=const_fn
[openssl-sys 0.9.77] cargo:rustc-cfg=openssl
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR
[openssl-sys 0.9.77] X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR unset
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
[openssl-sys 0.9.77] OPENSSL_LIB_DIR unset
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_INCLUDE_DIR
[openssl-sys 0.9.77] X86_64_UNKNOWN_LINUX_GNU_OPENSSL_INCLUDE_DIR unset
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
[openssl-sys 0.9.77] OPENSSL_INCLUDE_DIR unset
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR
[openssl-sys 0.9.77] X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR unset
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_DIR
[openssl-sys 0.9.77] OPENSSL_DIR unset
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_PKG_CONFIG
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_STATIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_DYNAMIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_PATH
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=SYSROOT
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_STATIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_DYNAMIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
[openssl-sys 0.9.77] cargo:rustc-link-lib=ssl
[openssl-sys 0.9.77] cargo:rustc-link-lib=crypto
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_PKG_CONFIG
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_STATIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=OPENSSL_DYNAMIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_PATH
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
[openssl-sys 0.9.77] cargo:rerun-if-changed=build/expando.c
[openssl-sys 0.9.77] OPT_LEVEL = Some("0")
[openssl-sys 0.9.77] TARGET = Some("x86_64-unknown-linux-gnu")
[openssl-sys 0.9.77] HOST = Some("x86_64-unknown-linux-gnu")
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=CC_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] CC_x86_64-unknown-linux-gnu = None
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=CC_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] CC_x86_64_unknown_linux_gnu = None
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_CC
[openssl-sys 0.9.77] HOST_CC = None
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=CC
[openssl-sys 0.9.77] CC = None
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=CFLAGS_x86_64-unknown-linux-gnu
[openssl-sys 0.9.77] CFLAGS_x86_64-unknown-linux-gnu = None
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=CFLAGS_x86_64_unknown_linux_gnu
[openssl-sys 0.9.77] CFLAGS_x86_64_unknown_linux_gnu = None
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=HOST_CFLAGS
[openssl-sys 0.9.77] HOST_CFLAGS = None
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=CFLAGS
[openssl-sys 0.9.77] CFLAGS = None
[openssl-sys 0.9.77] cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
[openssl-sys 0.9.77] CRATE_CC_NO_DEFAULTS = None
[openssl-sys 0.9.77] DEBUG = Some("true")
[openssl-sys 0.9.77] CARGO_CFG_TARGET_FEATURE = Some("fxsr,llvm14-builtins-abi,sse,sse2")
[openssl-sys 0.9.77] running: "cc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "-m64" "-I" "/usr/include" "-Wall" "-Wextra" "-E" "build/expando.c"
[openssl-sys 0.9.77] exit status: 0
[openssl-sys 0.9.77] version: 3_0_2
[openssl-sys 0.9.77] cargo:rustc-cfg=osslconf="OPENSSL_NO_SSL3_METHOD"
[openssl-sys 0.9.77] cargo:conf=OPENSSL_NO_SSL3_METHOD
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl300
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl101
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl102
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl102f
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl102h
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl110
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl110f
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl110g
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl110h
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl111
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl111b
[openssl-sys 0.9.77] cargo:rustc-cfg=ossl111c
[openssl-sys 0.9.77] cargo:version_number=30000020
[openssl-sys 0.9.77] cargo:include=/usr/include
     Running `CARGO=/home/jsha/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo CARGO_CRATE_NAME=openssl_sys CARGO_MANIFEST_DIR=/home/jsha/.cargo/registry/src/index.crates.io-93a9e06d8945f2c0/openssl-sys-0.9.77 CARGO_PKG_AUTHORS='Alex Crichton <alex@alexcrichton.com>:Steven Fackler <sfackler@gmail.com>' CARGO_PKG_DESCRIPTION='FFI bindings to OpenSSL' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE=MIT CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=openssl-sys CARGO_PKG_REPOSITORY='https://github.com/sfackler/rust-openssl' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.9.77 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=9 CARGO_PKG_VERSION_PATCH=77 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/jsha/rust/rebuild-repro/target/debug/deps:/home/jsha/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib:/home/jsha/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib' OUT_DIR=/home/jsha/rust/rebuild-repro/target/debug/build/openssl-sys-d302818e9941e583/out rustc --crate-name openssl_sys /home/jsha/.cargo/registry/src/index.crates.io-93a9e06d8945f2c0/openssl-sys-0.9.77/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=5000c6011fb6dcd0 -C extra-filename=-5000c6011fb6dcd0 --out-dir /home/jsha/rust/rebuild-repro/target/debug/deps -L dependency=/home/jsha/rust/rebuild-repro/target/debug/deps --extern libc=/home/jsha/rust/rebuild-repro/target/debug/deps/liblibc-be771ccdf8acea64.rmeta --cap-lints warn -l ssl -l crypto --cfg const_fn --cfg openssl --cfg 'osslconf="OPENSSL_NO_SSL3_METHOD"' --cfg ossl300 --cfg ossl101 --cfg ossl102 --cfg ossl102f --cfg ossl102h --cfg ossl110 --cfg ossl110f --cfg ossl110g --cfg ossl110h --cfg ossl111 --cfg ossl111b --cfg ossl111c`
   Compiling rebuild-repro v0.1.0 (/home/jsha/rust/rebuild-repro)
     Running `CARGO=/home/jsha/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo CARGO_BIN_NAME=rebuild-repro CARGO_CRATE_NAME=rebuild_repro CARGO_MANIFEST_DIR=/home/jsha/rust/rebuild-repro CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=rebuild-repro CARGO_PKG_REPOSITORY='' CARGO_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 LD_LIBRARY_PATH='/home/jsha/rust/rebuild-repro/target/debug/deps:/home/jsha/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib:/home/jsha/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib' rustc --crate-name rebuild_repro --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=b6f75a22ff169989 -C extra-filename=-b6f75a22ff169989 --out-dir /home/jsha/rust/rebuild-repro/target/debug/deps -C incremental=/home/jsha/rust/rebuild-repro/target/debug/incremental -L dependency=/home/jsha/rust/rebuild-repro/target/debug/deps --extern openssl_sys=/home/jsha/rust/rebuild-repro/target/debug/deps/libopenssl_sys-5000c6011fb6dcd0.rlib`
    Finished dev [unoptimized + debuginfo] target(s) in 2.26s

This is continuing to be a (very frustrating) problem. It looks like every time RA declares it’s “loading metadata”, it clobbers my debug compile cache. This happens at least once every time I launch vscode, and often at other times, sometimes several times in a row (maybe every time I touch a Cargo.toml? but also sometimes when I haven’t…). My environment is set consistently between my terminal and rust-analyzer.server.extraEnv; it seems like RA is ignoring the latter for some of its operations.

Tried that, now it’s clobbering my cache every time I try to build instead of every startup 😦

e: that seems to have stopped for now, might have involved the “loading metadata” step rather than the check?

I wonder, if you let it share the target folders, does the cargo build that runs after check has finished have to rebuild all dependencies? Otherwise you might spend some time waiting for the lock, but it’s maybe just time you’d otherwise spend waiting for the dependencies to build?

Brilliant. The second option is exactly what I was looking for. My first instinct was “why isn’t this the default?”, but then I looked at the target folder doubling in size. The current default behavior seems like the right choice for most people. Then people (like me) who are willing to trade some disk space for non-blocking behavior can do so. Ideally theres a way to avoid duplicate artifacts, have check on save enabled, and avoid blocks, but I understand that such an ask might be outside the bounds of the constraints rust-analyzer operates in. Maybe RA could detect when there is a user-initiated cargo lock contention and force-stop its internal cargo check execution?

Thanks for your help! I consider this issue solved for me.