Skip to content

Improve error messages when ToolTask overrides exit code 0 to -1 due to logged errors#13303

Merged
OvesN merged 10 commits intodotnet:mainfrom
OvesN:dev/veronikao/tooltask-new-logging-error-on-exitcode-0-and-logged-error
Feb 28, 2026
Merged

Improve error messages when ToolTask overrides exit code 0 to -1 due to logged errors#13303
OvesN merged 10 commits intodotnet:mainfrom
OvesN:dev/veronikao/tooltask-new-logging-error-on-exitcode-0-and-logged-error

Conversation

@OvesN
Copy link
Contributor

@OvesN OvesN commented Feb 27, 2026

Fixes #11784

Context

When a tool exits with code 0 but logs some errors, ToolTask overrides ExitCode from 0 to -1. The subsequent error message says "The command exited with code -1", which is misleading - the tool actually exited with code 0.

Changes Made

  • Added ExitCodeOverriddenToIndicateErrors protected property to ToolTask to track when the exit code was overridden from 0 to -1 due to logged errors.
  • ToolTask.HandleTaskExecutionErrors: When the override occurred, logs a new low-importance message: "The command exited with return value 0, but errors were detected during execution. ExitCode was set to -1." instead of "The command exited with code -1."
  • Exec.HandleTaskExecutionErrors: Logs new error MSB3077 instead of MSB3073. "MSB3077: The command "{0}" exited with return value 0, but errors were detected during execution. ExitCode was set to -1"
  • XamlDataDrivenToolTask.HandleTaskExecutionErrors: Logs new error MSB3725 instead of MSB3721. "MSB3725 : The command "{0}" exited with return value 0, but errors were detected during execution. ExitCode was set to -1"
  • Added resource strings for the new messages in both Utilities and Tasks.

Testing

  • Updated ToolTask_Tests.HandleExecutionErrorsWhenToolLogsError to verify the new message text.
  • Updated ToolTask_Tests.ErrorWhenTextSentToStandardError to verify the zero-with-errors path.
  • Added ToolTask_Tests.HandleExecutionErrorsWhenToolLogsErrorAndExitsNonZero to verify the non-zero exit path still uses the original message.
  • Updated Exec_Tests.LoggedErrorsCauseFailureDespiteExitCode0 to verify MSB3077 is logged and MSB3073/MSB6006 are not.
  • Added XamlDataDrivenToolTask_Tests.ExitCodeZeroWithLoggedErrors_LogsMSB3725 to verify MSB3725 is logged and MSB3721 is not.

Notes

No behavior change =ExitCode is still set to -1 in this case. Only the log messages are corrected to accurately describe what happened.

…d logged errors

Enhance Exec, ToolTask, and XamlDataDrivenToolTask to detect when a tool exits with code 0 but logs errors. Log a specific message in this case, not just the standard "exited with code -1".
Copilot AI review requested due to automatic review settings February 27, 2026 12:14
Copy link
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

This PR improves error messages when a tool exits with code 0 but MSBuild's ToolTask overrides the exit code to -1 due to logged errors. Previously, the error message would misleadingly claim "The command exited with code -1" when the tool actually returned 0. The fix tracks when this override occurs and displays a clearer message explaining what happened.

Changes:

  • Added a protected ExitCodeOverriddenToIndicateErrors property to ToolTask to track when exit code is overridden from 0 to -1
  • Updated error messages in ToolTask, Exec, and XamlDataDrivenToolTask to use new, clearer messages when the override occurs
  • Added new error codes MSB3077 (Exec) and MSB3725 (XamlDataDrivenToolTask) with localized resource strings

Reviewed changes

Copilot reviewed 34 out of 34 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Utilities/ToolTask.cs Added ExitCodeOverriddenToIndicateErrors property, set flag when overriding exit code, updated HandleTaskExecutionErrors to use flag for message selection
src/Tasks/Exec.cs Updated HandleTaskExecutionErrors to log MSB3077 when exit code was overridden
src/Tasks/XamlTaskFactory/XamlDataDrivenToolTask.cs Updated HandleTaskExecutionErrors to log MSB3725 when exit code was overridden
src/Utilities/Resources/Strings.resx Added new resource string for ToolTask message
src/Tasks/Resources/Strings.resx Added new resource strings for MSB3077 and MSB3725 error codes
src/Utilities/Resources/xlf/*.xlf Added localized resource entries (marked as "new" for translation)
src/Tasks/Resources/xlf/*.xlf Added localized resource entries for MSB3077 and MSB3725 (marked as "new" for translation)
src/Utilities.UnitTests/ToolTask_Tests.cs Updated and added tests to verify new message behavior
src/Tasks.UnitTests/Exec_Tests.cs Updated test to verify MSB3077 is logged instead of MSB3073/MSB6006
src/Tasks.UnitTests/XamlDataDrivenToolTask_Tests.cs Added new test class to verify MSB3725 is logged correctly

Copy link
Member

@JanProvaznik JanProvaznik left a comment

Choose a reason for hiding this comment

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

functionally lgtm, sort the error codes please

@OvesN OvesN enabled auto-merge (squash) February 27, 2026 16:42
@rainersigwald
Copy link
Member

No behavior change =ExitCode is still set to -1 in this case. Only the log messages are corrected to accurately describe what happened.

Can you elaborate on why you chose this approach?

@OvesN
Copy link
Contributor Author

OvesN commented Feb 28, 2026

No behavior change =ExitCode is still set to -1 in this case. Only the log messages are corrected to accurately describe what happened.

Can you elaborate on why you chose this approach?

@rainersigwald
I did not change the ExitCode behavior because this is not a bug, the behavior is documented and expected: https://learn.microsoft.com/en-us/dotnet/api/microsoft.build.utilities.tooltask.exitcode?view=msbuild-17-netcore.

@OvesN OvesN merged commit 414f0ac into dotnet:main Feb 28, 2026
10 checks passed
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.

Confusing and inconsistent behavior around AfterTargets="Build" in referenced projects

5 participants