-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix unhandled exception in /getItem and /getTargetResult for items with illegal path characters #12841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Hello @@copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo. |
…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>
the command output from original issue makes sense |
JanProvaznik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this 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 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.
| private static string TryGetMetadata(ITaskItem item, string metadataName) | ||
| { | ||
| try | ||
| { | ||
| return item.GetMetadata(metadataName); | ||
| } | ||
| catch (InvalidOperationException) | ||
| { | ||
| // Built-in metadata like FullPath, Directory, etc. require path computation. | ||
| // If the item spec contains illegal path characters, return empty string. | ||
| return string.Empty; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to get metadata value from a ProjectItemInstance. If the metadata is a built-in metadata | ||
| /// (like FullPath, Directory, etc.) and the item spec contains illegal path characters, | ||
| /// this will catch the InvalidOperationException and return an empty string. | ||
| /// </summary> | ||
| private static string TryGetMetadataValue(ProjectItemInstance item, string metadataName) | ||
| { | ||
| try | ||
| { | ||
| return item.GetMetadataValue(metadataName); | ||
| } | ||
| catch (InvalidOperationException) | ||
| { | ||
| // Built-in metadata like FullPath, Directory, etc. require path computation. | ||
| // If the item spec contains illegal path characters, return empty string. | ||
| return string.Empty; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to get metadata value from a ProjectItem. If the metadata is a built-in metadata | ||
| /// (like FullPath, Directory, etc.) and the item spec contains illegal path characters, | ||
| /// this will catch the InvalidOperationException and return an empty string. | ||
| /// </summary> | ||
| private static string TryGetMetadataValue(ProjectItem item, string metadataName) | ||
| { | ||
| try | ||
| { | ||
| return item.GetMetadataValue(metadataName); | ||
| } | ||
| catch (InvalidOperationException) | ||
| { | ||
| // Built-in metadata like FullPath, Directory, etc. require path computation. | ||
| // If the item spec contains illegal path characters, return empty string. | ||
| return string.Empty; | ||
| } | ||
| } |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider making the exception handling more specific to avoid potentially hiding unrelated errors. While GetItemSpecModifier wraps IO-related exceptions in InvalidOperationException, other code paths could also throw InvalidOperationException for different reasons.
Options to make this safer:
- Use an exception filter to check the message:
catch (InvalidOperationException ex) when (ex.Message.Contains("cannot be applied to the path")) - Add logging when exceptions are caught to aid debugging
- Consider proposing a more specific exception type in the underlying code to distinguish path-related errors
Note: This is a minor concern since the current fix correctly addresses the immediate issue and matches the documented behavior in the comments.
Fixes #11726
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,AddItemsInJsonFormatTesting
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.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
rcxvsblobprodcus328.vsblob.vsassets.io/home/REDACTED/work/msbuild/msbuild/artifacts/bin/bootstrap/core/dotnet dotnet test src/MSBuild.UnitTests/Microsoft.Build.CommandLine.UnitTests.csproj --filter FullyQualifiedName~GetTargetResultWithIllegalPathCharacters --no-build(dns block)If you need me to access, download, or install something from one of these locations, you can either:
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.