Improve error messages when ToolTask overrides exit code 0 to -1 due to logged errors#13303
Conversation
…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".
…Task will be executed multiple times
…exitcode-0-and-logged-error
There was a problem hiding this comment.
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
ExitCodeOverriddenToIndicateErrorsproperty toToolTaskto track when exit code is overridden from 0 to -1 - Updated error messages in
ToolTask,Exec, andXamlDataDrivenToolTaskto 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 |
Co-authored-by: Copilot <[email protected]>
JanProvaznik
left a comment
There was a problem hiding this comment.
functionally lgtm, sort the error codes please
…and-logged-error' of https://github.com/OvesN/msbuild into dev/veronikao/tooltask-new-logging-error-on-exitcode-0-and-logged-error
Can you elaborate on why you chose this approach? |
…exitcode-0-and-logged-error
@rainersigwald |
Fixes #11784
Context
When a tool exits with code 0 but logs some errors,
ToolTaskoverridesExitCodefrom 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
ExitCodeOverriddenToIndicateErrorsprotected property toToolTaskto 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"UtilitiesandTasks.Testing
ToolTask_Tests.HandleExecutionErrorsWhenToolLogsErrorto verify the new message text.ToolTask_Tests.ErrorWhenTextSentToStandardErrorto verify the zero-with-errors path.ToolTask_Tests.HandleExecutionErrorsWhenToolLogsErrorAndExitsNonZeroto verify the non-zero exit path still uses the original message.Exec_Tests.LoggedErrorsCauseFailureDespiteExitCode0to verify MSB3077 is logged and MSB3073/MSB6006 are not.XamlDataDrivenToolTask_Tests.ExitCodeZeroWithLoggedErrors_LogsMSB3725to verify MSB3725 is logged and MSB3721 is not.Notes
No behavior change =
ExitCodeis still set to -1 in this case. Only the log messages are corrected to accurately describe what happened.