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

Fix up bugs in parsing special chracters in EditorConfig #54511

Merged
merged 4 commits into from
Jul 25, 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 @@ -93,6 +93,28 @@ public void ConfigWithEscapedValues()
);
}

[Fact]
[WorkItem(52469, "https://github.com/dotnet/roslyn/issues/52469")]
public void CanGetSectionsWithSpecialCharacters()
{
var config = ParseConfigFile(@"is_global = true
[/home/foo/src/\{releaseid\}.cs]
build_metadata.Compile.ToRetrieve = abc123
[/home/foo/src/Pages/\#foo/HomePage.cs]
build_metadata.Compile.ToRetrieve = def456
");

var set = AnalyzerConfigSet.Create(ImmutableArray.Create(config));

var sectionOptions = set.GetOptionsForSourcePath("/home/foo/src/{releaseid}.cs");
Assert.Equal("abc123", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);

sectionOptions = set.GetOptionsForSourcePath("/home/foo/src/Pages/#foo/HomePage.cs");
Assert.Equal("def456", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);
}

[ConditionalFact(typeof(WindowsOnly))]
public void WindowsPath()
{
Expand Down
37 changes: 36 additions & 1 deletion src/Compilers/Core/MSBuildTask/GenerateMSBuildEditorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
Expand Down Expand Up @@ -38,6 +39,10 @@ namespace Microsoft.CodeAnalysis.BuildTasks
/// </remarks>
public sealed class GenerateMSBuildEditorConfig : Task
{
/// <remarks>
/// Although this task does its own writing to disk, this
/// output parameter is here for testing purposes.
/// </remarks>
[Output]
public string ConfigFileContents { get; set; }

Expand All @@ -47,11 +52,15 @@ public sealed class GenerateMSBuildEditorConfig : Task
[Required]
public ITaskItem[] PropertyItems { get; set; }

[Required]
public ITaskItem FileName { get; set; }

public GenerateMSBuildEditorConfig()
{
ConfigFileContents = string.Empty;
MetadataItems = Array.Empty<ITaskItem>();
PropertyItems = Array.Empty<ITaskItem>();
FileName = new TaskItem();
}

public override bool Execute()
Expand Down Expand Up @@ -98,7 +107,33 @@ public override bool Execute()
}

ConfigFileContents = builder.ToString();
return true;
return WriteMSBuildEditorConfig();
}

public bool WriteMSBuildEditorConfig()
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

@cston cston Jul 19, 2021

Choose a reason for hiding this comment

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

public

private or internal?

{
try
{
if (File.Exists(FileName.ItemSpec))
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
{
string existingContents = File.ReadAllText(FileName.ItemSpec);
if (existingContents.Length == ConfigFileContents.Length)
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
{
if (existingContents.Equals(ConfigFileContents))
{
return true;
}
}
}
var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
File.WriteAllText(FileName.ItemSpec, ConfigFileContents, encoding);
return true;
}
catch (IOException ex)
{
Log.LogErrorFromException(ex);
return false;
}
}

/// <remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,10 @@
<!-- Transform the collected properties and items into an editor config file -->
<GenerateMSBuildEditorConfig
PropertyItems="@(_GeneratedEditorConfigProperty)"
MetadataItems="@(_GeneratedEditorConfigMetadata)">

<Output TaskParameter="ConfigFileContents"
PropertyName="_GeneratedEditorConfigFileContent" />
MetadataItems="@(_GeneratedEditorConfigMetadata)"
FileName="$(GeneratedMSBuildEditorConfigFile)">
</GenerateMSBuildEditorConfig>

<!-- Write the output to the generated file, if it's changed -->
<WriteLinesToFile Lines="$(_GeneratedEditorConfigFileContent)" File="$(GeneratedMSBuildEditorConfigFile)" Overwrite="True" WriteOnlyWhenDifferent="True" />
</Target>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
Expand All @@ -21,7 +22,10 @@ public class GenerateMSBuildEditorConfigTests
[Fact]
public void GlobalPropertyIsGeneratedIfEmpty()
{
GenerateMSBuildEditorConfig configTask = new GenerateMSBuildEditorConfig();
GenerateMSBuildEditorConfig configTask = new GenerateMSBuildEditorConfig()
{
FileName = GetTestFilePath()
};
configTask.Execute();

var result = configTask.ConfigFileContents;
Expand All @@ -37,7 +41,8 @@ public void PropertiesAreGeneratedInGlobalSection()

GenerateMSBuildEditorConfig configTask = new GenerateMSBuildEditorConfig()
{
PropertyItems = new[] { property1, property2 }
PropertyItems = new[] { property1, property2 },
FileName = GetTestFilePath()
};
configTask.Execute();

Expand All @@ -56,7 +61,8 @@ public void ItemMetaDataCreatesSection()

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

Expand All @@ -78,7 +84,8 @@ public void MultipleItemMetaDataCreatesSections()

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

Expand Down Expand Up @@ -107,7 +114,8 @@ public void MultipleSpecialCharacterItemMetaDataCreatesSections()

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

Expand All @@ -134,7 +142,8 @@ public void DuplicateItemSpecsAreCombinedInSections()

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

Expand All @@ -155,7 +164,8 @@ public void ItemIsMissingRequestedMetadata()

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

Expand All @@ -177,7 +187,8 @@ public void ItemIsMissingRequiredMetadata()

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

Expand All @@ -203,7 +214,8 @@ public void PropertiesAreGeneratedBeforeItems()
GenerateMSBuildEditorConfig configTask = new GenerateMSBuildEditorConfig()
{
MetadataItems = new[] { item1, item2, item3, item4 },
PropertyItems = new[] { property1, property2 }
PropertyItems = new[] { property1, property2 },
FileName = GetTestFilePath()
};
configTask.Execute();

Expand Down Expand Up @@ -234,7 +246,8 @@ public void ItemIsNotFullyQualifiedPath()

GenerateMSBuildEditorConfig configTask = new GenerateMSBuildEditorConfig()
{
MetadataItems = new[] { item1, item2, item3 }
MetadataItems = new[] { item1, item2, item3 },
FileName = GetTestFilePath()
};
configTask.Execute();
var result = configTask.ConfigFileContents;
Expand Down Expand Up @@ -268,7 +281,8 @@ public void ItemsWithDifferentRelativeButSameFullPathAreCombined()

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

Expand Down Expand Up @@ -311,7 +325,8 @@ public void PropertiesWithNewLines()

GenerateMSBuildEditorConfig configTask = new GenerateMSBuildEditorConfig()
{
PropertyItems = new[] { property1, property2 }
PropertyItems = new[] { property1, property2 },
FileName = GetTestFilePath()
};
configTask.Execute();

Expand Down Expand Up @@ -339,7 +354,8 @@ public void ItemMetadataPathIsAdjustedOnWindows()

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

Expand All @@ -351,5 +367,12 @@ public void ItemMetadataPathIsAdjustedOnWindows()
build_metadata.Compile.ToRetrieve = abc123
", result);
}

private static ITaskItem GetTestFilePath([CallerMemberName] string callerName = "")
{
string executingLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)?.Replace('\\', '/') ?? string.Empty;
string path = $"{executingLocation}/{callerName}.GenerateMSBuildEditorConfig.editorconfig";
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
return new TaskItem(path);
}
Copy link
Member

Choose a reason for hiding this comment

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

}

Consider adding Assert.False(configTask.WriteMSBuildEditorConfig());

Copy link
Member

Choose a reason for hiding this comment

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

I was hoping to test that WriteMSBuildEditorConfig() did not update the file if the contents were already correct, but it looks like It looks the method returns true in either case.

Should we add an out bool updated parameter that indicated whether the file was modified, and check that value?

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ public bool IsMatch(string s)
numberRangePairs.ToImmutableAndFree());
}

internal static bool TryUnescapeSectionName(string sectionName, out string? escapedSectionName)
{
var sb = new StringBuilder();
SectionNameLexer lexer = new SectionNameLexer(sectionName);
while (!lexer.IsDone)
{
var tokenKind = lexer.Lex();
if (tokenKind == TokenKind.SimpleCharacter)
{
sb.Append(lexer.EatCurrentCharacter());
}
else
{
sb.Append(lexer.CurrentCharacter);
Copy link
Member

@cston cston Jul 19, 2021

Choose a reason for hiding this comment

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

sb.Append(lexer.CurrentCharacter)

Do we ever hit this code path? I may be misreading the Lex() method, but it looks like CurrentCharacter is the character after the special character in this case. Is that expected here? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, you're right. This path never gets hit. I'm not sure why I had this in the first place (perhaps something came up when I was doing the end-to-end testing with the dotnet/website repo), but it's not needed. CanGetSectionsWithSpecialCharacters still passes without this.

}
}
escapedSectionName = sb.ToString();
return true;
}

/// <summary>
/// Test if a section name is an absolute path with no special chars
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ public AnalyzerConfigOptionsResult GetOptionsForSourcePath(string sourcePath)
// If we have a global config, add any sections that match the full path
foreach (var section in _globalConfig.NamedSections)
{
if (normalizedPath.Equals(section.Name, Section.NameComparer))
var escapedSectionName = TryUnescapeSectionName(section.Name, out var sectionName);
if (escapedSectionName && normalizedPath.Equals(sectionName, Section.NameComparer))
{
sectionKey.Add(section);
}
Expand Down