11

I'm using Cargo build to compile a project written in Rust. https://github.com/hyperledger-labs/solang

When modifying a small part of source code and compiling the project again (using cargo build --release or cargo build), I notice that Cargo always compiles or checks all dependencies, before it actually compiles the project's source code.

For example, below is the compilation log where all libraries are re-compiled or checked whenever I run cargo build. These steps take a lot of time.

Is there any way to stop Cargo from compiling the library?

Thank you for taking a look at my issue!

cargo build
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
   Compiling unicode-xid v0.2.2
   Compiling proc-macro2 v1.0.36
   Compiling libc v0.2.119
   Compiling cfg-if v1.0.0
   Compiling autocfg v1.1.0
   Compiling memchr v2.4.1
   Compiling version_check v0.9.4
   Compiling smallvec v1.8.0
   Compiling ucd-trie v0.1.3
   Compiling scopeguard v1.1.0
   Compiling regex-syntax v0.6.25
   Compiling lazy_static v1.4.0
   Compiling serde_derive v1.0.136
   Compiling siphasher v0.3.9
   Compiling log v0.4.14
   Compiling parking_lot_core v0.8.5
   Compiling crunchy v0.2.2
   Compiling typenum v1.15.0
   Compiling hashbrown v0.11.2
   Compiling cc v1.0.73
   Compiling futures-core v0.3.21
   Compiling ppv-lite86 v0.2.16
   Compiling tiny-keccak v2.0.2
   Compiling serde_json v1.0.79
   Compiling either v1.6.1
   Compiling futures-channel v0.3.21
   Compiling fixedbitset v0.2.0
   Compiling new_debug_unreachable v1.0.4
   Compiling futures-task v0.3.21
   Compiling bit-vec v0.6.3
   Compiling proc-macro-hack v0.5.19
   Compiling parking_lot_core v0.9.1
   Compiling precomputed-hash v0.1.1
   Compiling futures-util v0.3.21
   Compiling maplit v1.0.2
   Compiling diff v0.1.12
   Compiling pico-args v0.4.2
   Compiling radium v0.6.2
   Compiling semver v1.0.6
   Compiling async-trait v0.1.52
   Compiling arrayvec v0.4.12
   Compiling heck v0.4.0
   Compiling inkwell v0.1.0-beta.4
   Compiling instant v0.1.12
   Compiling lock_api v0.4.6
   Compiling pest v2.1.3
   Compiling generic-array v0.14.5
   Compiling proc-macro-error-attr v1.0.4
   Compiling proc-macro-error v1.0.4
   Compiling nom v7.1.0
   Compiling phf_shared v0.10.0
   Compiling indexmap v1.8.0
   Compiling num-traits v0.2.14
   Compiling num-integer v0.1.44
   Compiling num-bigint v0.4.3
   Compiling num-rational v0.4.0
   Compiling itertools v0.10.3
   Compiling bit-set v0.5.2
   Compiling solang v0.1.10 (/home/trungtq/workspace/sbip/solang-debug-info)
   Compiling semver-parser v0.10.2
   Compiling pest_meta v2.1.3
   Compiling quote v1.0.15
   Compiling aho-corasick v0.7.18
   Compiling os_str_bytes v6.0.0
   Compiling dirs-sys-next v0.1.2
   Compiling getrandom v0.2.5
   Compiling atty v0.2.14
   Compiling num_cpus v1.13.1
   Compiling tempfile v3.3.0
   Compiling ena v0.14.0
   Compiling bitvec v0.20.4
   Compiling semver v0.11.0
   Compiling syn v1.0.86
   Compiling blake2-rfc v0.2.18
   Compiling dirs-next v2.0.0
   Compiling rand_core v0.6.3
   Compiling regex v1.5.4
   Compiling parking_lot v0.11.2
   Compiling parking_lot v0.12.0
   Compiling petgraph v0.5.1
   Compiling clap v3.1.5
   Compiling term v0.7.0
   Compiling rand_chacha v0.3.1
   Compiling string_cache v0.8.3
   Compiling dashmap v5.1.0
   Compiling crypto-common v0.1.3
   Compiling block-buffer v0.10.2
   Compiling ascii-canvas v3.0.0
   Compiling rand v0.8.5
   Compiling lalrpop-util v0.19.7
   Compiling digest v0.10.3
   Compiling sha2 v0.10.2
   Compiling ripemd v0.1.1
   Compiling lalrpop v0.19.7
   Compiling phf_generator v0.10.0
   Compiling pest_generator v2.1.3
   Compiling llvm-sys v130.0.3
   Compiling tokio-macros v1.7.0
   Compiling futures-macro v0.3.21
   Compiling phf_macros v0.10.0
   Compiling serde_repr v0.1.7
   Compiling pest_derive v2.1.0
   Compiling tower-lsp-macros v0.5.0
   Compiling inkwell_internals v0.5.0
   Compiling auto_impl v0.5.0
   Compiling tokio v1.17.0
   Compiling phf v0.10.1
   Compiling tokio-util v0.7.0
   Compiling futures v0.3.21
   Compiling serde v1.0.136
   Compiling solang-parser v0.1.10 (/home/trungtq/workspace/sbip/solang-debug-info/solang-parser)
   Compiling url v2.2.2
   Compiling lsp-types v0.92.0
   Compiling handlebars v4.2.1
   Compiling contract-metadata v0.3.0
   Compiling tower-lsp v0.15.1
    Finished dev [unoptimized + debuginfo] target(s) in 1m 07s
6
  • 3
    Looks like you have some sort of tool or utility which breaks the cache (which would also be why cargo finds itself waiting on the package cache lock). Because it works fine and same as ever for me. Commented Mar 9, 2022 at 7:16
  • 1
    @Masklinn: Thanks a lot for the suggestion! Yes, you're right. I realize that I also use rust-analyzer, a language server protocol for Rust, which causes the problem. When closing that process, then Cargo build will not check other libraries! Commented Mar 9, 2022 at 10:00
  • This issue is also reported here: github.com/rust-analyzer/rust-analyzer/issues/6136 Commented Mar 9, 2022 at 10:04
  • 1
    Nice! So looks like it might be an issue with the target directory (one of the commenters notes that deleting it fixed the issue), otherwise the most common problem seems to be updating relevant envars in your shell's rcfile (leading to their updates on subshells), or even weirder buildflags / configuration issues. Commenter "kassens" mentions an envvar which can be used to dump the cache fingerprinting information on build to diagnose the issue. Commented Mar 9, 2022 at 10:51
  • Thanks! In the end, I run Cargo build with CARGO_TARGET_DIR="./build" cargo build, so that my build command output into a different target folder and will not affect rust-analyzer. Now, I don't need to close rust-analyzer while build my project anymore Commented Mar 9, 2022 at 11:29

3 Answers 3

9

As others have pointed out, this has to do with rust-analyzer running cargo check on your project's /target, and it conflicting with your shell execution of cargo build. You can configure your shell to build into a different directory, by setting CARGO_TARGET_DIR, or you can disable Check On Save (not recommended) by setting:

"rust-analyzer.checkOnSave": false

However, you can actually get rust-analyzer and any shell execution to play nicely together. I was having this same problem, and it was due to mismatched RUSTFLAGS. Whenever RUSTFLAGS (and possibly other/any rust environment variables, I'm not sure) change between cargo build/test/run invocations it seems to invalidate the cache, triggering a re-compilation of all dependencies. We can see this, for example, by running the following:

$ cargo build
$ RUSTFLAGS="-A dead_code" cargo build

Re-running the same command does not re-compile anything, but running a different one causes a full re-compilation, including dependencies. In my case, I had configured RUSTFLAGS="-A dead_code" on my shell, but not on rust-analyzer, hence every time I saved a file it would invalidate the cache. This can be fixed by setting rust-analyzer.cargo.extraEnv. In my case:

"rust-analyzer.cargo.extraEnv": {
    "RUSTFLAGS": "-A dead_code"
}
Sign up to request clarification or add additional context in comments.

2 Comments

Stop suggesting checkOnSave:false to other people. If you make this editor stops all features about rust. For example suggestion, finding imports and showing errors on the line. Don't do that.
I agree, and I wasn't suggesting that anyone do that, but I think those simpler solutions are helpful in understanding my suggested solution. Would adding a warning assuage your concerns?
3

You need to set CARGO_TARGET_DIR envar to your project's target path. for Linux:

export CARGO_TARGET_DIR=~/myproject/target

1 Comment

Doesn't work for me (Cargo 1.83). Everytime when I issue cargo run it recompiles everything. This started with a newer version of VSCodium, the solution was to create a TOML file at the root level with a workspace. But now all code keeps recompiling. How cumbersome.
3

I tried this and it seems to work, if your VSCode has rust-analyzer and you have turned on the Auto Save feature, it might be causing cargo to almost always be attempting to check your rust codes and hence blocking your cargo run/check commands. I tried turning it off and it seems to be working.

"rust-analyzer.checkOnSave": false

1 Comment

You don't want this nice feature to be disabled...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.