Conversation
|
r? @matklad (rust_highfive has picked a reviewer for you, use r? to override) |
This allows `cargo doc --message-format=json` to actually work. Note that this explicitly does not attempt to support it for doctests for several reasons: - `rustdoc --test --error-format=json` does not work for some reason. - Since the lib is usually compiled before running rustdoc, warnings/errors will be emitted correctly by rustc. - I'm unaware of any errors/warnings `rustdoc --test` is capable of producing assuming the code passed `rustc`. - The compilation of the tests themselves do not support JSON. - libtest does not output json, so it's utility is limited.
|
cc @GuillaumeGomez I assume this is OK now that |
|
Indeed! |
| &mut json_stdout, | ||
| &mut |line| json_stderr(line, &package_id, &target), | ||
| false, | ||
| ).map(drop) |
There was a problem hiding this comment.
map(drop) probably should not be here, it's just ignore the error. I wonder if the one on the next line should be removed as well?
There was a problem hiding this comment.
Yea, it's a little weird. The intent is so that all branches are CargoResult<()> so they all have the same type so that a common error can be attached. I can't think of a more elegant way to do that.
There was a problem hiding this comment.
Oh, I've misunderstood the intention of the code completely...
Yep, the only better way to write this would probably be a catch block, which is currently unstable.
There was a problem hiding this comment.
You could go the other way and change rustdoc.exec() to rustdoc.exec_with_output(), so all branches type-check to CargoResult<Output>.
There was a problem hiding this comment.
That'll unfortunately hide rustdoc's output from the user, b/c it would be captured.
src/cargo/core/compiler/mod.rs
Outdated
| } | ||
| } | ||
|
|
||
| fn json_stdout(line: &str) -> CargoResult<()> { |
There was a problem hiding this comment.
Let's rename this to assert_is_empty? EDIT: or smthing similar, so it's clear on the call site what actually happens with json on stdout :)
There was a problem hiding this comment.
Also, are we sure that rustdoc's stdout is clear?
There was a problem hiding this comment.
I'll investigate if it is possible for it to print to stdout.
| } | ||
|
|
||
| if bcx.build_config.json_messages() { | ||
| rustdoc.arg("--error-format").arg("json"); |
There was a problem hiding this comment.
We don't need to feature-detect support for json here, right? The user requests --message-format=json explicitly, so they'll see an error and that is expected behavior.
There was a problem hiding this comment.
I don't understand the question. This is the code that translates cargo's --message-format flag to rustdoc's --error-format flag.
There was a problem hiding this comment.
--error-format is a recent addition to rustdoc, so, in theory, using new cargo and old rustdoc could result in a
error: the `-Z unstable-options` flag must also be passed to enable the flag `error-format`
message. However, that should be fine, because the user has to ask for cargo doc --message-format=json explicitly. That is, it's not a flag that we add automatically for existing workflows, so we don't have to feature-detect support for it.
There was a problem hiding this comment.
Oh, I understand now. Yea, my understanding is that the version of cargo is tightly wedded to rustc and rustdoc. I think some distributions have attempted to split them, but afaik cargo doesn't really support that use case, right? At least in this case, it wouldn't break the normal usage.
There was a problem hiding this comment.
It is at least somewhat supported: you can override compiler & rustdoc with env vars, and we try account for different compilers in Rustc cache. I think the current approach is "don't break things without necessity", and I don't remember any actual CLI changes that could have caused breakage.
tests/testsuite/doc.rs
Outdated
| // This can be removed once 1.30 is stable (rustdoc --error-format stabilized). | ||
| return; | ||
| } | ||
| let p = project().file("src/lib.rs", "asdf").build(); |
There was a problem hiding this comment.
Hm, let's use a code that is valid, but has docstring errors? That way, we'll be more sure that we are checking rustdoc output specifically.
There was a problem hiding this comment.
Sure, I was on the fence with that. My concern is that the rustdoc messages might be a little unstable, but there is another test already doing that.
| } | ||
|
|
||
| fn json_stderr(line: &str, package_id: &PackageId, target: &Target) -> CargoResult<()> { | ||
| // stderr from rustc/rustdoc can have a mix of JSON and non-JSON output |
There was a problem hiding this comment.
Hm, does rustdoc put JSON on stderr as well? It's a shame that Cargo and Rustc use different streams for JSON =/
There was a problem hiding this comment.
It is an odd inconsistency. There's an open issue for that (rust-lang/rust#48301). I imagine since the vast majority of people use rustc behind cargo it doesn't really come up often.
|
LGTM! Let's also see what @alexcrichton thinks about this, because formally this extend "public API" of Cargo. |
|
📌 Commit c28823f has been approved by |
Support JSON with rustdoc. This allows `cargo doc --message-format=json` to actually work. Note that this explicitly does not attempt to support it for doctests for several reasons: - `rustdoc --test --error-format=json` does not work for some reason. - Since the lib is usually compiled before running rustdoc, warnings/errors will be emitted correctly by rustc. - I'm unaware of any errors/warnings `rustdoc --test` is capable of producing assuming the code passed `rustc`. - The compilation of the tests themselves do not support JSON. - libtest does not output json, so it's utility is limited.
|
☀️ Test successful - status-appveyor, status-travis |
Update cargo - Update transitioning url (rust-lang/cargo#5889) - Resolve some clippy lint warnings (rust-lang/cargo#5884) - Don't kill child processes on normal exit on Windows (rust-lang/cargo#5887) - fix a bunch of clippy warnings (rust-lang/cargo#5876) - Add support for rustc's --error-format short (rust-lang/cargo#5879) - Support JSON with rustdoc. (rust-lang/cargo#5878) - Fix rustfmt instructions in CONTRIBUTING.md (rust-lang/cargo#5880) - Allow `cargo run` in workspaces. (rust-lang/cargo#5877) - Change target filters in workspaces. (rust-lang/cargo#5873) - Improve verbose console and log for finding git repo in package check (rust-lang/cargo#5858) - Meta rename (rust-lang/cargo#5871) - fetch: skip target tests when cross_compile is disabled (rust-lang/cargo#5870) - Fully capture rustc and rustdoc output when -Zcompile-progress is passed (rust-lang/cargo#5862) - Fix test --example docs. (rust-lang/cargo#5867) - Add a feature to build a vendored OpenSSL (rust-lang/cargo#5865)
This allows
cargo doc --message-format=jsonto actually work.Note that this explicitly does not attempt to support it for doctests for
several reasons:
rustdoc --test --error-format=jsondoes not work for some reason.will be emitted correctly by rustc.
rustdoc --testis capable of producingassuming the code passed
rustc.