Skip to content
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

Escape special characters in section name of editor config #52515

Merged
merged 5 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,42 @@ public void SimpleCase()
Assert.Equal("/bogus", config.NormalizedDirectory);
}

[Fact]
[WorkItem(52469, "https://github.com/dotnet/roslyn/issues/52469")]
public void ConfigWithEscapedValues()
{
var config = ParseConfigFile(@"is_global = true

[c:/\{f\*i\?le1\}.cs]
build_metadata.Compile.ToRetrieve = abc123

[c:/f\,ile\#2.cs]
build_metadata.Compile.ToRetrieve = def456

[c:/f\;i\!le\[3\].cs]
build_metadata.Compile.ToRetrieve = ghi789
");

var namedSections = config.NamedSections;
Assert.Equal("c:/\\{f\\*i\\?le1\\}.cs", namedSections[0].Name);
AssertEx.Equal(
new[] { KeyValuePair.Create("build_metadata.compile.toretrieve", "abc123") },
namedSections[0].Properties
);

Assert.Equal("c:/f\\,ile\\#2.cs", namedSections[1].Name);
AssertEx.Equal(
new[] { KeyValuePair.Create("build_metadata.compile.toretrieve", "def456") },
namedSections[1].Properties
);

Assert.Equal("c:/f\\;i\\!le\\[3\\].cs", namedSections[2].Name);
AssertEx.Equal(
new[] { KeyValuePair.Create("build_metadata.compile.toretrieve", "ghi789") },
namedSections[2].Properties
);
}

[ConditionalFact(typeof(WindowsOnly))]
public void WindowsPath()
{
Expand Down
27 changes: 24 additions & 3 deletions src/Compilers/Core/MSBuildTask/GenerateMSBuildEditorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public override bool Execute()
{
// write the section for this item
builder.AppendLine()
.Append("[")
.Append(group.Key)
.AppendLine("]");
.Append("[");
EncodeString(builder, group.Key);
builder.AppendLine("]");

foreach (var item in group)
{
Expand All @@ -101,6 +101,27 @@ public override bool Execute()
return true;
}

/// <remarks>
/// Filenames with special characters like '#' and'{' get written
/// into the section names in the resulting .editorconfig file. Later,
/// when the file is parsed in configuration options these special
/// characters are interpretted as invalid values and ignored by the
/// processor. We encode the special characters in these strings
/// before writing them here.
/// </remarks>

private static void EncodeString(StringBuilder builder, string value)
{
foreach (var c in value)
{
if (c is '*' or '?' or '{' or ',' or ';' or '}' or '[' or ']' or '#' or '!')
{
builder.Append("\\");
}
builder.Append(c);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the issue you're trying to solve, but does this need to escape slashes (\)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this scenario, I tried to stick to values that were defined as special characters per the editor config specification (http://docs.editorconfig.org/en/master/editorconfig-format.html).

}
}

/// <remarks>
/// Equivalent to Roslyn.Utilities.PathUtilities.NormalizeWithForwardSlash
/// Both methods should be kept in sync.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ public void MultipleItemMetaDataCreatesSections()
", result);
}

[Fact]
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
[WorkItem(52469, "https://github.com/dotnet/roslyn/issues/52469")]
public void MultipleSpecialCharacterItemMetaDataCreatesSections()
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
{
ITaskItem item1 = MSBuildUtil.CreateTaskItem("c:/{f*i?le1}.cs", new Dictionary<string, string> { { "ItemType", "Compile" }, { "MetadataName", "ToRetrieve" }, { "ToRetrieve", "abc123" } });
ITaskItem item2 = MSBuildUtil.CreateTaskItem("c:/f,ile#2.cs", new Dictionary<string, string> { { "ItemType", "Compile" }, { "MetadataName", "ToRetrieve" }, { "ToRetrieve", "def456" } });
ITaskItem item3 = MSBuildUtil.CreateTaskItem("c:/f;i!le[3].cs", new Dictionary<string, string> { { "ItemType", "Compile" }, { "MetadataName", "ToRetrieve" }, { "ToRetrieve", "ghi789" } });

GenerateMSBuildEditorConfig configTask = new GenerateMSBuildEditorConfig()
{
MetadataItems = new[] { item1, item2, item3 }
};
configTask.Execute();

var result = configTask.ConfigFileContents;

Assert.Equal(@"is_global = true

[c:/\{f\*i\?le1\}.cs]
build_metadata.Compile.ToRetrieve = abc123

[c:/f\,ile\#2.cs]
build_metadata.Compile.ToRetrieve = def456

[c:/f\;i\!le\[3\].cs]
build_metadata.Compile.ToRetrieve = ghi789
", result);
}

[Fact]
public void DuplicateItemSpecsAreCombinedInSections()
{
Expand Down