Simple project recompiles all dependencies when files change


#1

Hello, I’m new to Rust and have started my first project which uses the flate2 crate.

Whenever I edit my own code and run cargo build, all dependencies appear to get recompiled. Is this normal behavior or a bug? I found an issue on GitHub, but it seems to be resolved.

cargo --version: cargo 0.13.0-nightly (109cb7c 2016-08-19)
rustc --version: rustc 1.12.0 (3191fbae9 2016-09-23)

Below the output of a project created via cargo new testapp --bin. Only Cargo.toml has been modified to contain flate2 as a dependency. Between each build, main.rs is modified, e.g. by adding a blank line:

user@host:/tmp/testapp$ cargo build
    Blocking waiting for file lock on build directory
   Compiling gcc v0.3.35
   Compiling libc v0.2.16
   Compiling miniz-sys v0.1.7
   Compiling flate2 v0.2.14
   Compiling testapp v0.1.0 (file:///tmp/testapp)
    Finished debug [unoptimized + debuginfo] target(s) in 4.20 secs

# --- no change to main.rs here --

user@host:/tmp/testapp$ cargo build
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs

# --- main.rs modified here --

user@host:/tmp/testapp$ cargo build
    Blocking waiting for file lock on build directory
   Compiling gcc v0.3.35
   Compiling libc v0.2.16
   Compiling miniz-sys v0.1.7
   Compiling flate2 v0.2.14
   Compiling testapp v0.1.0 (file:///tmp/testapp)
    Finished debug [unoptimized + debuginfo] target(s) in 4.15 secs

#2

I can’t reproduce this. I’m on Debian Linux.

[steve@becoming tmp]$ cargo new testapp --bin                                                                                                                                                         
     Created binary (application) `testapp` project                                                                                                                                                   
[steve@becoming tmp]$ cd testapp/                                                                                                                                                                     
[steve@becoming testapp (master)]$ echo "flate2 = '*'" >> Cargo.toml                                                                                                                                  
[steve@becoming testapp (master)]$ cargo build                                                                                                                                                        
    Updating registry `https://github.com/rust-lang/crates.io-index`                                                                                                                                  
   Compiling gcc v0.3.35                                                                                                                                                                              
   Compiling libc v0.2.16                                                                                                                                                                             
   Compiling miniz-sys v0.1.7                                                                                                                                                                         
   Compiling flate2 v0.2.14                                                                                                                                                                           
   Compiling testapp v0.1.0 (file:///home/steve/tmp/testapp)                                                                                                                                          
    Finished debug [unoptimized + debuginfo] target(s) in 3.26 secs                                                                                                                                   
[steve@becoming testapp (master)]$ cargo build                                                                                                                                                        
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs                                                                                                                                    
[steve@becoming testapp (master)]$ touch src/main.rs                                                                                                                                                  
[steve@becoming testapp (master)]$ cargo build                                                                                                                                                        
   Compiling testapp v0.1.0 (file:///home/steve/tmp/testapp)                                                                                                                                          
    Finished debug [unoptimized + debuginfo] target(s) in 0.25 secs    

This log is on nightly, but seeing the same on stable 1.12.

This seems very suspicious to me…


#3

Do you have something compiling in the background? Like an editor plugin, perhaps with a different version of rustc? That would definitely cause cargo to redo everything with different compilers.


#4

Extending a bit on what @cuviper said, I’ve hit a similar problem when using Atom’s linter: https://github.com/AtomLinter/linter-rust/issues/77 … so it’s possible your editor tools are contributing to the behavior.

Apparently this particular bug might be solved soon by this recently merged cargo issue.


#5

Is it possible that some of your tools are touching stray files in the source directories?

Then it’s possible that misconfigured build scripts cause the rebuilds. Do you have a “build script” (build.rs)? Use rerun-if-changed by default


#6

@cuviper, @codingcampbell: I think you guys were on the right track. I’m using Atom with the Rust linter and if I edit main.rs with a plain text editor, the behavior is as in steveklabnik’s demonstration.

I had already disabled Atom’s Racer plugin, which I suspected could be responsible due to the local files is creates when running. I felt certain the other Rust related tools wouldn’t have such an effect, but apparently they do.

Hopefully the aforementioned changes to cargo will affect this issue. Thanks for the help!


#7

Also it should be possible to use a nightly cargo and a stable rustc to mitigate this issue until 1.14 is released.


#8

A helpful trick to diagnose these sorts of problems is to set this environment variable:

RUST_LOG=cargo::ops::cargo_rustc::fingerprint=info

That should help print diagnostic information about why recompiles are happening. Note that I’d definitely love to make this an official feature!


#9

Re-upping for reference: it is the Atom Linter plugin, which has a flag to use Cargo if it’s available for linting. If you use it (e.g. with cargo clippy, cargo check, etc.), it will apparently make a nice mess of your .cargo directory and trigger this rebuilding issue. Gladly, it should be gone by the end of the year; and I may just be running on nightly for a little while for this alone.