-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Stop escaped compiler args from being mangled by MSBuild #76888
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,98 @@ | ||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||
| // See the LICENSE file in the project root for more information. | ||||||||
|
|
||||||||
| using System; | ||||||||
| using System.Collections; | ||||||||
| using System.Collections.Generic; | ||||||||
| using Microsoft.Build.Framework; | ||||||||
|
|
||||||||
| namespace Microsoft.CodeAnalysis.BuildTasks | ||||||||
| { | ||||||||
| internal sealed class ArgsTaskItem : ITaskItem | ||||||||
| { | ||||||||
| // This list is taken from https://github.com/dotnet/msbuild/blob/291a8108761ed347562228f2f8f25477996a5a93/src/Shared/Modifiers.cs#L36-L70 | ||||||||
| private static readonly string[] WellKnownItemSpecMetadataNames = | ||||||||
JoeRobich marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| [ | ||||||||
| "FullPath", | ||||||||
| "RootDir", | ||||||||
| "Filename", | ||||||||
| "Extension", | ||||||||
| "RelativeDir", | ||||||||
| "Directory", | ||||||||
| "RecursiveDir", | ||||||||
| "Identity", | ||||||||
| "ModifiedTime", | ||||||||
| "CreatedTime", | ||||||||
| "AccessedTime", | ||||||||
| "DefiningProjectFullPath", | ||||||||
| "DefiningProjectDirectory", | ||||||||
| "DefiningProjectName", | ||||||||
| "DefiningProjectExtension", | ||||||||
| ]; | ||||||||
|
|
||||||||
| private readonly Dictionary<string, string> _metadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||||||||
|
|
||||||||
| public ArgsTaskItem(string itemSpec) | ||||||||
| { | ||||||||
| ItemSpec = itemSpec; | ||||||||
| } | ||||||||
|
|
||||||||
| public string ItemSpec { get; set; } | ||||||||
|
|
||||||||
| // Implementation notes that we should include the built-in metadata names as well as our custom ones. | ||||||||
| public ICollection MetadataNames | ||||||||
| { | ||||||||
| get | ||||||||
| { | ||||||||
| var clone = new List<string>(_metadata.Keys); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Avoid unnecessary allocations. |
||||||||
| clone.AddRange(WellKnownItemSpecMetadataNames); | ||||||||
| return clone; | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Implementation notes that we should include the built-in metadata names as well as our custom ones. | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which implementation notes this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add links. |
||||||||
| public int MetadataCount => _metadata.Count + WellKnownItemSpecMetadataNames.Length; | ||||||||
|
|
||||||||
| public IDictionary CloneCustomMetadata() | ||||||||
| { | ||||||||
| return new Dictionary<string, string>(_metadata, StringComparer.OrdinalIgnoreCase); | ||||||||
| } | ||||||||
|
|
||||||||
| public void CopyMetadataTo(ITaskItem destinationItem) | ||||||||
| { | ||||||||
| // Implementation notes that we should not overwrite existing metadata on the destination. | ||||||||
| var destinationMetadataNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase); | ||||||||
| foreach (var name in destinationItem.MetadataNames) | ||||||||
| { | ||||||||
| destinationMetadataNames.Add((string)name); | ||||||||
| } | ||||||||
|
|
||||||||
| foreach (var metadataName in _metadata.Keys) | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we only using
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my reading of the interface comments. There seems to be a distinction made between "built-in" and "custom" metadata. The "built-in" metadata seems to be related to the ItemSpec which this method is not supposed to update. |
||||||||
| { | ||||||||
| if (destinationMetadataNames.Contains(metadataName)) | ||||||||
| { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| var metadataValue = _metadata[metadataName]; | ||||||||
| destinationItem.SetMetadata(metadataName, metadataValue); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| public string GetMetadata(string metadataName) | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rainersigwald, @baronfel can you help me understand the contract of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have a specific instance we could look at? From what we can see TaskItem MetadataNames + GetMetadata should be safe for all of the returned names, which include explicit Item Metadata as well as the historically-significant set of metadata that all Items have (because all items are files, don'tchanknow).
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I misinterpreted the comments on the interface and the implementation here. |
||||||||
| { | ||||||||
| return _metadata[metadataName]; | ||||||||
| } | ||||||||
|
|
||||||||
| public void RemoveMetadata(string metadataName) | ||||||||
| { | ||||||||
| _metadata.Remove(metadataName); | ||||||||
| } | ||||||||
|
|
||||||||
| public void SetMetadata(string metadataName, string metadataValue) | ||||||||
| { | ||||||||
| _metadata[metadataName] = metadataValue; | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -159,7 +159,7 @@ protected static ITaskItem[] GenerateCommandLineArgsTaskItems(List<string> comma | |
| var items = new ITaskItem[commandLineArgs.Count]; | ||
| for (var i = 0; i < commandLineArgs.Count; i++) | ||
| { | ||
| items[i] = new TaskItem(commandLineArgs[i]); | ||
| items[i] = new ArgsTaskItem(commandLineArgs[i]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will change from us unconditionally escaping to unconditionally not escaping. Won't this regress scenarios where escaping is making builds work today? Consider for example that
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oof. Luckily many args such as Analyzers, References, Source all come from TaskItems of their own which apply the path correction. So I need to look at args which come from properties and may contain file paths.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So is the expectation that MSBuild project files might do things like Foo\Bar and this conversion is what makes things still work on non-Windows? That seems terrifying, but I guess project files using \ to make paths is very common...
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had thought we were going to try some trick about cloning a custom implementation. Does something like this not work?
That way we're returning an existing implementation rather than returning our own which risks bugs in case we don't get the semantics of the interface right.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, let's do that instead. |
||
| } | ||
|
|
||
| return items; | ||
|
|
||
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.
Can we add a comment summarizing that we're using this to avoid unnecessary path escaping?