fix(cargo-wdk): use --message-format=json-render-diagnostics in the build command to render compiler errors instead of the plain json option#645
Conversation
--message-format=json-render-diagnostics in the build command to show compiler errors instead of json--message-format=json-render-diagnostics in the build command to render compiler errors instead of the plain json option
There was a problem hiding this comment.
Pull request overview
Updates cargo-wdk’s build invocation and error handling so compiler diagnostics are shown to users/tests in a readable form (instead of being buried in JSON), addressing the debugging pain described in #613.
Changes:
- Switch
cargo buildto--message-format=json-render-diagnosticsso diagnostics are rendered tostderrwhile keeping parseable JSON onstdout. - Simplify
BuildTaskError::CargoBuildto store a descriptiveStringmessage instead of the fullCommandError. - Update unit/integration tests to expect the new cargo argument and error shape.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| crates/cargo-wdk/src/actions/build/tests.rs | Updates expected cargo args in build-action tests to use the new message format. |
| crates/cargo-wdk/src/actions/build/error.rs | Changes BuildTaskError::CargoBuild payload from CommandError to String and updates its display format. |
| crates/cargo-wdk/src/actions/build/build_task.rs | Updates cargo args to json-render-diagnostics and maps command failures to a simplified compilation-failed message; adjusts unit tests accordingly. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #645 +/- ##
==========================================
+ Coverage 78.44% 78.69% +0.25%
==========================================
Files 25 25
Lines 5201 5234 +33
Branches 5201 5234 +33
==========================================
+ Hits 4080 4119 +39
+ Misses 1001 995 -6
Partials 120 120 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…type Co-authored-by: Copilot <copilot@github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
… build command to render compiler errors instead of the plain `json` option (microsoft#645) Fixes microsoft#613 ## Problem When `cargo wdk build` encounters a compilation error, the error output is hard to debug: 1. JSON wall — `--message-format=json` sends even the compiler diagnostics as JSON to `stdout` instead of redirecting it to `stderr`. So, compiler errors are not directly visible instead they are buried deep in the output json. This is affecting test outputs because in case of failures, the errors cannot be seen easily in the `json` output. 2. Redundant error content — `BuildTaskError::CargoBuild` wraps the full `CommandError` (containing `stdout` JSON), even though the user may have already seen the real compiler errors on `stderr` in real time. ## Fixes The following changes fix the above problems: 1. Switch to `--message-format=json-render-diagnostics` This option instructs cargo to render diagnostics from `rustc` directly to `stderr` instead of putting it in the `json` output. stdout: only machine-parseable JSON (artifacts, build-finished) — no diagnostic messages stderr: human-readable compiler errors and warnings, rendered by Cargo (visible to the user in real time since stderr is inherited) 2. Omit `stdout` from `CommandError::CommandFailed` before returning from `BuildTask`: When cargo build fails, `BuildTask::run()` now maps the error to a new instance of `CommandFailed` but with the `stdout` field set to `String::new()` to avoid printing machine-readable (`json`) output on the screen. ## Before Running `kmdf_driver_cross_compiles_with_cli_option_successfully` test after removing `aarch64-pc-windows-msvc` target: <img width="947" height="572" alt="image" src="https://github.com/user-attachments/assets/efb391d3-02e8-47fd-907e-91ffe5c1d4ba" /> _____ Compiler diagnostics not printed to `stderr`, instead it is buried in the json output when `cargo wdk build` fails: <img width="1547" height="822" alt="image" src="https://github.com/user-attachments/assets/4a0cf5c0-a9d4-4a8c-be63-c1237d6ed835" /> _____ Output seen when tests fail due to compilation errors: <img width="1936" height="1162" alt="image" src="https://github.com/user-attachments/assets/ac4a5e53-5a95-483d-898c-0e751e790c36" /> ## After Running `kmdf_driver_cross_compiles_with_cli_option_successfully` test after removing `aarch64-pc-windows-msvc` target: <img width="1344" height="824" alt="image" src="https://github.com/user-attachments/assets/f95f94a9-9076-46e3-a706-c07c7ca95b65" /> _____ Compiler diagnostics available on terminal: <img width="1028" height="576" alt="image" src="https://github.com/user-attachments/assets/39d6ba3f-5b6f-4fdb-bbb9-0b39c21874be" /> _____ Output seen when tests fail due to compilation errors: <img width="1867" height="1231" alt="image" src="https://github.com/user-attachments/assets/b282efcd-c748-4129-b2c1-4f5c6b297900" /> --------- Co-authored-by: Copilot <copilot@github.com>
Fixes #613
Problem
When
cargo wdk buildencounters a compilation error, the error output is hard to debug:--message-format=jsonsends even the compiler diagnostics as JSON tostdoutinstead of redirecting it tostderr. So, compiler errors are not directly visible instead they are buried deep in the output json. This is affecting test outputs because in case of failures, the errors cannot be seen easily in thejsonoutput.BuildTaskError::CargoBuildwraps the fullCommandError(containingstdoutJSON), even though the user may have already seen the real compiler errors onstderrin real time.Fixes
The following changes fix the above problems:
Switch to
--message-format=json-render-diagnosticsThis option instructs cargo to render diagnostics from
rustcdirectly tostderrinstead of putting it in thejsonoutput.stdout: only machine-parseable JSON (artifacts, build-finished) — no diagnostic messages
stderr: human-readable compiler errors and warnings, rendered by Cargo (visible to the user in real time since stderr is inherited)
Omit
stdoutfromCommandError::CommandFailedbefore returning fromBuildTask:When cargo build fails,
BuildTask::run()now maps the error to a new instance ofCommandFailedbut with thestdoutfield set toString::new()to avoid printing machine-readable (json) output on the screen.Before
Running
kmdf_driver_cross_compiles_with_cli_option_successfullytest after removingaarch64-pc-windows-msvctarget:Compiler diagnostics not printed to
stderr, instead it is buried in the json output whencargo wdk buildfails:Output seen when tests fail due to compilation errors:
After
Running
kmdf_driver_cross_compiles_with_cli_option_successfullytest after removingaarch64-pc-windows-msvctarget:Compiler diagnostics available on terminal:
Output seen when tests fail due to compilation errors:
