flycheck / flycheck-rust

Better Rust/Cargo support for Flycheck

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flycheck does not include dev-dependencies when running tests

Dushistov opened this issue · comments

Steps to reproduce:

  1. cargo new --lib foo
  2. Add to Cargo.toml such lines:
[dev-dependencies]
lazy_static = "0.2.2"

  1. Add to lib.rs such lines:
#[cfg(test)]
#[macro_use]
extern crate lazy_static;
  1. Run cargo build

after that flycheck underline with red line: extern crate lazy_static; and show hint around this line:
can't find create for 'lazy_static',
but cargo test|build|check reports no errors.

I can reproduce.

So with this setup, we end up calling cargo rustc --lib -- --test. But the lazy_static crate is never included that way, because cargo doesn't know we want to build tests (we pass the flag directly to rustc).

Note that here, neither cargo build nor cargo check do compile the test code; only cargo test does. But not only cargo test compiles the test, it runs it as well. In Flycheck, we only want compilation errors, so at the very least we should add --no-run.

$ cargo test --no-run -v -v
   Compiling foo v0.1.0 (file:///tmp/foo)
   Compiling lazy_static v0.2.6
     Running `rustc --crate-name foo src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=3f482a6be9713984 -C extra-filename=-3f482a6be9713984 --out-dir /tmp/foo/target/debug/deps -L dependency=/tmp/foo/target/debug/deps`
     Running `rustc --crate-name lazy_static /home/fmdkdd/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-0.2.6/src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=3b17cb290f94de07 -C extra-filename=-3b17cb290f94de07 --out-dir /tmp/foo/target/debug/deps -L dependency=/tmp/foo/target/debug/deps`
     Running `rustc --crate-name foo src/lib.rs --emit=dep-info,link -C debuginfo=2 --test -C metadata=21239f184ac91d21 -C extra-filename=-21239f184ac91d21 --out-dir /tmp/foo/target/debug/deps -L dependency=/tmp/foo/target/debug/deps --extern lazy_static=/tmp/foo/target/debug/deps/liblazy_static-3b17cb290f94de07.rlib`
warning: unused `#[macro_use]` import, #[warn(unused_imports)] on by default
 --> src/lib.rs:2:1
  |
2 | #[macro_use]
  | ^^^^^^^^^^^^

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

So we may be able to fix it by running cargo test --no-run in a second pass instead of passing --test to cargo rustc. Or, we can wait for rust-lang/cargo/issues/3431.

I'll look into the first option tomorrow.

I've found a way to make it work without breaking other cases using a combination of cargo check and cargo test. You can try out the branch.

There is one downside though: unlike cargo rustc, cargo check and cargo test will emit warnings only the first time they are run. I enquire about this behavior on cargo itself.

Untill this is fixed, I think I'd rather not merge it as-is, since I've got some flickering. Sometimes after you've saved, warnings appear, sometimes not.