`cargo test` and `RUSTC_LOG` only tests, not dependencies

I'm modifying the Rust compiler for a project. I've built my own toolchain called stage1. To test my project, I'd like to run tests with cargo +stage1 test and observe the compiler's logging output using RUSTC_LOG.

Goal: Logging output from the Rust compiler compiling my tests

Attempt: RUSTC_LOG=rustc_mir::transform=info cargo +stage1 test (for example)

Result: ALL logging output from compiling my dependencies (libc, rand, etc.) as well as my tests.
I don't want to see the logs from libc, rand, etc.. I only want to see the logs from compiling my tests.

Is there a way to achieve this?

This would definitely be a workaround solution, but what about running cargo build first? That should build at least most of the dependencies so they don't have to be rebuilt... Or if you're willing to have just the info from external tests, you could run cargo test --lib first, to compile all dev dependencies & the library first, and then run cargo test with RUSTC_LOG set to get only the external tests' output.

A more exact solution would be to use cargo's json message format and write a tool which parses it to figure out when each package is being built. Specifically, if you run cargo test -j 1 --message-format json, it will output a json string for every build start/stop, and the -j 1 flag should ensure no two things build simultaneously. This would require writing a custom script, but I think it should be doable, and won't be that hacky.

1 Like

Another workaround is to run cargo +stage1 test -v to obtain the arguments with which cargo invoked rustc for your tests, then run RUSTC_LOG=... rustc <args>.

1 Like

Thanks for your responses :smiley: Here is my workflow now:

  1. cargo +stage1 build as @daboross recommended to build most of the tests' dependencies.
  2. RUSTC_LOG=... cargo +stage1 test to build any extra dependencies, but ignore the extra logs.
  3. Trivially touch/edit one of the test files (e.g. change a constant)
  4. RUSTC_LOG=... cargo +stage1 test -- this time, only the changed test file is rebuilt and I can read through the relevant logs by piping output to a file (RUSTC_LOG=... cargo +stage1 test &> out.log).

This isn't the cleanest, but it's sufficient for my work. I might put this in a Makefile eventually. Thank you @sinkuu as well; breaking down a cargo command with -v is a super useful tip!