Why does "cargo test" print compile errors twice?


#1

When I run cargo test (as opposed to cargo build), every type error etc gets printed twice (only once when running cargo build) - does anyone know why this is? It’s not a big problem, just makes it a little difficult to read.

Here’s an example:

 % cargo test
   Compiling grade-school v0.0.0 (file:///Users/will/exercism/rust/grade-school)
src/lib.rs:25:13: 25:18 error: mismatched types [E0308]
src/lib.rs:25             names
                          ^~~~~
src/lib.rs:25:13: 25:18 help: run `rustc --explain E0308` to see a detailed explanation
src/lib.rs:25:13: 25:18 note: expected type `std::option::Option<_>`
src/lib.rs:25:13: 25:18 note:    found type `&std::vec::Vec<std::string::String>`
error: aborting due to previous error
Build failed, waiting for other jobs to finish...
src/lib.rs:25:13: 25:18 error: mismatched types [E0308]
src/lib.rs:25             names
                          ^~~~~
src/lib.rs:25:13: 25:18 help: run `rustc --explain E0308` to see a detailed explanation
src/lib.rs:25:13: 25:18 note: expected type `std::option::Option<_>`
src/lib.rs:25:13: 25:18 note:    found type `&std::vec::Vec<std::string::String>`
error: aborting due to previous error
error: Could not compile `grade-school`.

To learn more, run the command again with --verbose.

#2

Indeed, it is messy. You’re lucky you’re getting errors one after another. For me it mashes them together every few words, so I get errorerror: mismatched types [E0308] mismatched types [E0308] etc.

It’s buggy like that, because it compiles doctest in parallel and doesn’t prevent both processes from outputting if one fails.

If you’re not using doctests, then add

doctest = false

in your Cargo.toml’s lib and/or bin sections.


#3

Oh, interesting - thanks. Unfortunately, adding doctest = false to my [lib] section in Cargo.toml didn’t completely stop the duplicate compile errors (it sometimes did, really weird). I had to add -j1 to stop the parallelisation going on. I imagine that’ll be fine for now when I’m only building small trivial projects, but will be a problem on larger projects.


#4

You can run cargo in verbose mode (cargo test -v) to find out what exactly is going on. cargo test invokes rustc --crate-type lib and rustc --test (the first one will be skipped after a successful cargo build) and then rustdoc --test to run doctests.


#5

Does cargo build output errors correctly for you?


#6

Yes cargo build was fine. I think it was, as @gkoz was saying, that it builds 2 targets, one for the lib and one for tests and does that in parallel. With -j1 it does one of those at a time and quits when the build fails - which is what I want really. Without build errors, leaving -j1 off will result in faster builds no doubt, but in my early Rust days I am pretty much seeing compile errors 90% of the time, so clearer output is preferred to me.