Skip to content

Commit

Permalink
Use a custom MSBuild TaskItem when reporting compiler args.
Browse files Browse the repository at this point in the history
The TaskItem implementation from MSBuild treats the ItemSpec as file path and tries to normalize the path separators. When running on non-windows machines this means changing '\' to '/'.  However this breaks how double quotes are escaped in the compiler args. By using our own implemetation of TaskItem we can use the compiler args as the ItemSpec without any modification.

Resolves #72014
  • Loading branch information
JoeRobich committed Jan 23, 2025
1 parent 9cab90a commit 067fe9d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
83 changes: 83 additions & 0 deletions src/Compilers/Core/MSBuildTask/ArgsTaskItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 System.Linq;
using Microsoft.Build.Framework;
using System.Collections.Immutable;

namespace Microsoft.CodeAnalysis.BuildTasks
{
internal sealed class ArgsTaskItem : ITaskItem
{
// This list is taken from src/Shared/Modifiers.cs in the dotnet/msbuild repo.
private static readonly string[] WellKnownItemSpecMetadataNames =
{
"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 => _metadata.Keys.Concat(WellKnownItemSpecMetadataNames).ToImmutableArray();

// Implementation notes that we should include the built-in metadata names as well as our custom ones.
public int MetadataCount => _metadata.Count + WellKnownItemSpecMetadataNames.Length;

public IDictionary CloneCustomMetadata()
{
return _metadata.ToImmutableDictionary();
}

public void CopyMetadataTo(ITaskItem destinationItem)
{
var destinationMetadataNames = destinationItem.MetadataNames.OfType<string>();
var metadataNamesToCopy = _metadata.Keys.Intersect(destinationMetadataNames, StringComparer.OrdinalIgnoreCase).ToArray();

foreach (var metadataName in metadataNamesToCopy)
{
var metadataValue = _metadata[metadataName];
destinationItem.SetMetadata(metadataName, metadataValue);
}
}

public string GetMetadata(string metadataName)
{
return _metadata[metadataName];
}

public void RemoveMetadata(string metadataName)
{
_metadata.Remove(metadataName);
}

public void SetMetadata(string metadataName, string metadataValue)
{
_metadata[metadataName] = metadataValue;
}
}
}
2 changes: 1 addition & 1 deletion src/Compilers/Core/MSBuildTask/ManagedToolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

return items;
Expand Down

0 comments on commit 067fe9d

Please sign in to comment.