Fix unhandled exception in /getItem and /getTargetResult for items with illegal path characters#12841
Merged
JanProvaznik merged 6 commits intomainfrom Jan 5, 2026
Conversation
…egal path characters Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
…ilt-in modifier' Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix unhandled exception for GetCompileCommands target
Fix unhandled exception in /getItem and /getTargetResult for items with illegal path characters
Nov 27, 2025
Member
the command output from original issue makes sense |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes an unhandled exception that occurs when using /getItem or /getTargetResult with items whose specs contain illegal path characters, such as compiler command-line flags from ClangTidy's GetCompileCommands target.
Key Changes
- Added exception handling for
InvalidOperationExceptionwhen retrieving metadata that requires path computation - Created three helper methods (
TryGetMetadataand twoTryGetMetadataValueoverloads) to safely retrieve metadata - Added regression test to verify the fix works for both
/getItemand/getTargetResultswitches
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/MSBuild/JsonOutputFormatter.cs | Added exception handling in metadata retrieval to prevent crashes when item specs contain illegal path characters; added three helper methods to safely get metadata values |
| src/MSBuild.UnitTests/XMake_Tests.cs | Added regression test that verifies both /getItem and /getTargetResult work correctly with items containing compiler flags as item specs |
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
Co-authored-by: YuliiaKovalova <95473390+YuliiaKovalova@users.noreply.github.com>
YuliiaKovalova
approved these changes
Jan 5, 2026
Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
JanProvaznik
approved these changes
Jan 5, 2026
This was referenced Jan 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
/getItemand/getTargetResultthrow an unhandledInvalidOperationExceptionwhen serializing items whose item specs contain illegal path characters (e.g., compiler command line flags from ClangTidy'sGetCompileCommandstarget).The JSON output formatter iterates over all metadata names including built-in metadata like
FullPath,Directory, etc. WhenGetMetadata()is called on these, it attempts path computation which fails for non-path item specs.Changes Made
JsonOutputFormatter.cswith try-catch forInvalidOperationExceptionTryGetMetadataandTryGetMetadataValuehelper methods that return empty string on failureAddTargetResultsInJsonFormat,AddItemInstancesInJsonFormat,AddItemsInJsonFormatInvalidOperationExceptioninstances to ensure compatibility across all locales (error messages are localized)Testing
Added
GetTargetResultWithIllegalPathCharacterstest that verifies both/getTargetResultand/getItemsucceed with items containing compiler flags as item specs.Notes
On Linux, the test shows the path-like metadata is still computed (since
/is valid in paths). On Windows, these would return empty strings. The key fix is preventing the unhandled exception crash.The exception handling is intentionally broad (catching all
InvalidOperationExceptionwithout message filtering) to ensure the fix works correctly in all locales, as error messages from MSBuild are localized and checking for specific English text would fail in non-English environments.Original prompt
This section details on the original issue you should resolve
<issue_title>[Unhandled Exception]: /getItem and /getTargetResult fail for target GetCompileCommands</issue_title>
<issue_description>### Issue Description
There is an uncaught exception when trying to access certain information from the GetCompileCommands target (from ClangTidy), in particular
/getTargetResult:GetCompileCommandsand/getTargetResult:GetCompileCommandsSteps to Reproduce
tidytest.zip
Run either of the following commands (You could also run with target
/t:ClangTidyfor the same result)I expect to get a json formatted target result or item.
Actual Behavior
I get the following error when building
Analysis
The error suggests that it is trying to interpret the flags of the compile commands as a path, which causes an error when it can't be resolved. The function
%(FullPath)is used, though it is not clear to me exactly where, but my best guess is it used on a variable that is supposed to just be the path tocl, but has been extended with flags.See the clang tidy targets file at something similar to
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Microsoft\VC\v170\Microsoft.Cpp.ClangTidy.targetsVersions & Co...
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.