cargo icon indicating copy to clipboard operation
cargo copied to clipboard

`-w json` breaks `cargo doc --message-format=json`

Open jyn514 opened this issue 7 months ago • 3 comments

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

  1. Add support for --print=file-names to rustdoc. I think this can be insta-stable; any other values should probably be unstable.
  2. Change cargo to run rustdoc --print=file-names before running it on the crate, just like it already does for rustc.
  3. Use that to emit filenames in 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

jyn514 avatar May 18 '25 18:05 jyn514

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.

weihanglo avatar May 24 '25 01:05 weihanglo

That said, if there are other reasons Cargo wants --print=file-names support in rustdoc, we could do that and benefit from it.

weihanglo avatar May 24 '25 01:05 weihanglo

The current output of cargo rustdoc --message-format json --output-format json -Zunstable-options looks good.

oh, huh, TIL. i agree this is fine and we just need to add support to cargo doc.

jyn514 avatar May 24 '25 04:05 jyn514

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

weihanglo avatar Nov 24 '25 21:11 weihanglo