`-w json` breaks `cargo doc --message-format=json`
Problem
here is cargo, run on a toy crate:
$ RUSTDOCFLAGS="-Zunstable-options -wjson" cargo doc --message-format json
Documenting whatever v0.1.0 (/home/jyn/src/whatever)
{"reason":"compiler-artifact","package_id":"path+file:///home/jyn/src/whatever#0.1.0","manifest_path":"/home/jyn/src/whatever/Cargo.toml","target":{"kind":["bin"],"crate_types":["bin"],"name":"whatever","src_path":"/home/jyn/src/whatever/src/main.rs","edition":"2024","doc":true,"doctest":false,"test":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":[],"filenames":[],"executable":null,"fresh":false}
{"reason":"build-finished","success":true}
this says "filenames": []. that is not correct. rustdoc generated an output file in /home/jyn/.local/lib/cargo/target/doc/whatever.json, which should be reported here.
my understanding is that for this to work, cargo needs rustdoc to support --print=file-names, which it doesn't currently.
Proposed Solution
- Add support for
--print=file-namesto rustdoc. I think this can be insta-stable; any other values should probably be unstable. - Change cargo to run
rustdoc --print=file-namesbefore running it on the crate, just like it already does for rustc. - Use that to emit
filenamesin cargo's own json output.
Notes
here is an strace of cargo running rustc --print=file-names:
[pid 20757] execve("/home/jyn/.local/lib/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc", ["/home/jyn/.local/lib/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc", "-", "--crate-name", "___", "--print=file-names", "--crate-type", "bin", "--crate-type", "rlib", "--crate-type", "dylib", "--crate-type", "cdylib", "--crate-type", "staticlib", "--crate-type", "proc-macro", "--print=sysroot", "--print=split-debuginfo", "--print=crate-name", "--print=cfg", "-Wwarnings"], 0x55bb1434c940 /* 73 vars */) = 0
We have JSON output support in cargo rustdoc https://github.com/rust-lang/cargo/issues/13283 BTW. The --output-format json needs special handling for Cargo to track files.
cargo needs rustdoc to support --print=file-names, which it doesn't currently
I don't think it is needed, unless it starts emitting more than the logic here and Cargo cannot catch up with. The current output of cargo rustdoc --message-format json --output-format json -Zunstable-options looks good.
I think the action item is adding --output-format support in cargo doc.
That said, if there are other reasons Cargo wants --print=file-names support in rustdoc, we could do that and benefit from it.
The current output of
cargo rustdoc --message-format json --output-format json -Zunstable-optionslooks good.
oh, huh, TIL. i agree this is fine and we just need to add support to cargo doc.
oh, huh, TIL. i agree this is fine and we just need to add support to cargo doc.
Close as adding cargo doc support is tracked in #13283