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 1 commit
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,41 @@ public void SimpleCase()
Assert.Equal("/bogus", config.NormalizedDirectory);
}

[Fact]
public void ConfigWithEscapedValues()
{
var config = ParseConfigFile(@"is_global = true

[c:/\{file1\}.cs]
build_metadata.Compile.ToRetrieve = abc123

[c:/file\#2.cs]
build_metadata.Compile.ToRetrieve = def456

[c:/file\[3\].cs]
build_metadata.Compile.ToRetrieve = ghi789
");

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

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

Assert.Equal("c:/file\\[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
31 changes: 30 additions & 1 deletion src/Compilers/Core/MSBuildTask/GenerateMSBuildEditorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public override bool Execute()
// write the section for this item
builder.AppendLine()
.Append("[")
.Append(group.Key)
.Append(EncodeString(group.Key))
.AppendLine("]");

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

// 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.
captainsafia marked this conversation as resolved.
Show resolved Hide resolved

private static string EncodeString(string p)
{
StringBuilder builder = new StringBuilder();
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
for (var i = 0; i < p.Length; i++)
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
{
builder.Append(p[i] switch
{
'*' => "\\*",
'?' => "\\?",
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
'{' => "\\{",
',' => "\\,",
';' => "\\;",
'}' => "\\}",
'[' => "\\[",
']' => "\\]",
'#' => "\\#",
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
var @default => @default
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
});
}
return builder.ToString();
}

/// <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,34 @@ public void MultipleItemMetaDataCreatesSections()
", result);
}

[Fact]
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
public void MultipleSpecialCharacterItemMetaDataCreatesSections()
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
{
ITaskItem item1 = MSBuildUtil.CreateTaskItem("c:/{file1}.cs", new Dictionary<string, string> { { "ItemType", "Compile" }, { "MetadataName", "ToRetrieve" }, { "ToRetrieve", "abc123" } });
ITaskItem item2 = MSBuildUtil.CreateTaskItem("c:/file#2.cs", new Dictionary<string, string> { { "ItemType", "Compile" }, { "MetadataName", "ToRetrieve" }, { "ToRetrieve", "def456" } });
ITaskItem item3 = MSBuildUtil.CreateTaskItem("c:/file[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:/\{file1\}.cs]
build_metadata.Compile.ToRetrieve = abc123

[c:/file\#2.cs]
build_metadata.Compile.ToRetrieve = def456

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

[Fact]
public void DuplicateItemSpecsAreCombinedInSections()
{
Expand Down