cargo: Cargo doesn’t display output from a command in build.rs
When a command, e.g. Command::new("nasm"), in build.rs returns an error, Cargo doesn’t output the error to standard output. This is especially frustrating when debugging a Makefile that’s run by Command::new("make").
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 20
- Comments: 25 (4 by maintainers)
Note to anyone who found this issue by googling: you can pass
-vvnow to cargo to display the output. See commit 2c0d6af1739be737133e2385033d1395fbcd8342 and #1106 for details.I mentioned this on the new linked issue, but it might be worth mentioning here that you can currently do
println!(cargo:warning=MESSAGE)to get output from build scripts. It will always be prefixed with"warning: "however.So, for quick debugging of a build script you could use this drop in replacement for
println:usage example:
This is actually done by default to turn down the noise from builds. If you’re debugging makefiles I’d recommend having the build command itself forcibly fail to ensure that Cargo prints the output of the build script.
Cargo shouldn’t use
println!for transmitting commands.You can check the full output in
target/build/$crate-$hash/outputand you can forcibly fail with apanic!()or astd::os::set_exit_status(nonzero_code). I think that printing this out for--verbosewould be a bit too verbose, but we could consider printing where the output goes I suppose. In general a successful build script is something you largely want to ignore 90% of the time.Why would I write a build script with
println!if I didn’t want to see any output when building? Hopefully this will get fixed.Why not display this output when using
--verbose? How do I forcibly fail?I don’t think this issue should be closed. It’s very convenient to use
printlnwhen debugging. Not being able to use it in part of the codebase is cumbersome and introduces a significant mental burden.You shouldn’t use
println!for debug printing abuild.rsscript; the cargo build system uses the output of that script to record build & rebuild metadata, as documented here: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-scriptFor those who still doesn’t see any output even after passing
-vv:build.rslike adding a new line(force cargo to rebuild it) and usingcargo build -vv, then you’ll see the outputs.find . -name output, you will be able to get the path(target/build/$crate-$hash/output) to the output file, then you cancatit.That link states that any output not starting with
cargo:will be ignored. As long as you avoid thecargo:prefix for debugging, that link implies that it’s fine to useprintln!for debugging.I’ve managed to debug
build.rsscripts by placing apanic!at the end. As it crashes, it will print everything to standard output, including anyprintln!(the universal debugger 😄).Thanks, Really appreciated
At least the output of
eprintln!goes through in that case.Another (terrible) hack, but if you
panic!at the very end of your build script, it will at least allow you to see any prints emitted from the script. This can be useful if you just need some quick insight into what is happening in the script.@gdnathan
That answer is in the second post
As other posts in this thread say
target/build/$crate-$hash/output-vv@notdanilo , I opened a new issue based on your suggestion with an alternative solution:
If anyone likes it, or have a different proposal, please, upvote it or comment on it: https://github.com/rust-lang/cargo/issues/10475
Same issue here. Cargo run does not print. Compiling directly with rustc and running main.exe does print.
There is also
-- --nocapturewhich shows the output when running tests. Maybe it helps but I have not tried.