Skip to content

fix(cargo-wdk): use --message-format=json-render-diagnostics in the build command to render compiler errors instead of the plain json option#645

Merged
gurry merged 4 commits into
microsoft:mainfrom
svasista-ms:fix-build-output
May 1, 2026
Merged

fix(cargo-wdk): use --message-format=json-render-diagnostics in the build command to render compiler errors instead of the plain json option#645
gurry merged 4 commits into
microsoft:mainfrom
svasista-ms:fix-build-output

Conversation

@svasista-ms
Copy link
Copy Markdown
Contributor

@svasista-ms svasista-ms commented Apr 21, 2026

Fixes #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:

image

Compiler diagnostics not printed to stderr, instead it is buried in the json output when cargo wdk build fails:

image

Output seen when tests fail due to compilation errors:

image

After

Running kmdf_driver_cross_compiles_with_cli_option_successfully test after removing aarch64-pc-windows-msvc target:

image

Compiler diagnostics available on terminal:

image

Output seen when tests fail due to compilation errors:
image

Copilot AI review requested due to automatic review settings April 21, 2026 07:43
@svasista-ms svasista-ms changed the title fix(cargo-wdk): use --message-format=json-render-diagnostics in the build command to show compiler errors instead of json fix(cargo-wdk): use --message-format=json-render-diagnostics in the build command to render compiler errors instead of the plain json option Apr 21, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 build to --message-format=json-render-diagnostics so diagnostics are rendered to stderr while keeping parseable JSON on stdout.
  • Simplify BuildTaskError::CargoBuild to store a descriptive String message instead of the full CommandError.
  • 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.

Comment thread crates/cargo-wdk/src/actions/build/build_task.rs Outdated
Comment thread crates/cargo-wdk/src/actions/build/error.rs Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 21, 2026

Codecov Report

❌ Patch coverage is 95.83333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.69%. Comparing base (5748ea2) to head (3105ea8).

Files with missing lines Patch % Lines
crates/cargo-wdk/src/actions/build/build_task.rs 95.83% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…type

Co-authored-by: Copilot <copilot@github.com>
gurry
gurry previously approved these changes Apr 29, 2026
wmmc88
wmmc88 previously approved these changes Apr 30, 2026
Comment thread crates/cargo-wdk/src/actions/build/build_task.rs Outdated
Copilot AI review requested due to automatic review settings April 30, 2026 05:17
@svasista-ms svasista-ms dismissed stale reviews from wmmc88 and gurry via 3105ea8 April 30, 2026 05:17
@svasista-ms svasista-ms requested review from gurry and wmmc88 April 30, 2026 05:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/cargo-wdk/src/actions/build/error.rs
@gurry gurry enabled auto-merge May 1, 2026 02:08
@gurry gurry added this pull request to the merge queue May 1, 2026
Merged via the queue into microsoft:main with commit efa6c11 May 1, 2026
256 of 257 checks passed
@gurry gurry deleted the fix-build-output branch May 1, 2026 03:31
svasista-ms added a commit to svasista-ms/windows-drivers-rs that referenced this pull request May 5, 2026
… 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure output truncates stderr and mixes JSON messages, hiding root cause compilation errors

5 participants