I'm using watch
with cargo
, in order to quickly see compile time errors. However, cargo build
will only show errors when building the first time.
$ cargo build
Compiling clayman v0.0.1
src/core_math/vector.rs:8:5: 13:6 warning: method is never used: `New`, #[warn(dead_code)] on by default
src/core_math/vector.rs:8 pub fn New(x: i32, y: i32) -> Vector {
src/core_math/vector.rs:9 Vector {
src/core_math/vector.rs:10 x: x,
src/core_math/vector.rs:11 y: y
src/core_math/vector.rs:12 }
src/core_math/vector.rs:13 }
src/core_math/vector.rs:8:5: 13:6 warning: method `New` should have a snake case name such as `new`, #[warn(non_snake_case)] on by default
src/core_math/vector.rs:8 pub fn New(x: i32, y: i32) -> Vector {
src/core_math/vector.rs:9 Vector {
src/core_math/vector.rs:10 x: x,
src/core_math/vector.rs:11 y: y
src/core_math/vector.rs:12 }
src/core_math/vector.rs:13 }
src/main.rs:28:9: 28:10 warning: unused variable: `v`, #[warn(unused_variables)] on by default
src/main.rs:28 let v: vector::Vector;
^
$ cargo build
$
Which means I only get to see the warnings for a few seconds before watch
gives me a clear screen.
Is there any way to make cargo build
always give me the warnings?
A solution, albeit temporarily, was to
touch
a file I rarely edit. This way, I'm avoiding thethis file has been changed
problem, and also don't need to clean the whole project all the time.In addition, I managed to get colors in as well (cargo doesn't have a
--color
option), by not usingwatch
at all, but by simply running the following script:The reason I'm writing and reading to
.tmp
is to make all the output appear at one (rustc
outputs as it runs)Warnings only happen when Rust recompiles your files; however it caches as much as possible and if something hasn't changed it will happily skip an useless compile. There's currently no option in Cargo to force rebuild.
A quick and dirty solution, but easy to setup, is to
touch
your source files so that Cargo believes they changed:Another solution, maybe better, would be to clean off the
target
directory containing binary artifacts, withcargo clean
:It has the advantage of not triggering Vim "file changed!" warnings and is also runnable anywhere within the project dir, not just the root.
This is another variant of the touch pattern described in mdups answer.
Use this command to check for type errors and display all warnings every run, even when no code change has occurred:
touch src/my_tra_la_la.rs && clear && clear && cargo check
The
clear && clear &&
part is mostly irrelevant to this answer but it's a mandatory part of every build command for me, to avoid having the new output visually merge with output from the previous run.It requires two things, first
mod my_tra_la_la;
inmain.rs
, or inlib.rs
if it's a library.The second part is optional but I think it's nice to add some documentation to this special source file,
src/my_tra_la_la.rs
:The whole point of a dedicated touch file like this is to avoid the need to ever have it open in your code editor and avoid popups and warnings like "file changed!" while working.