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 2 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 @@ -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
34 changes: 33 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,14 @@ public sealed class GenerateMSBuildEditorConfig : Task
[Required]
public ITaskItem[] PropertyItems { get; set; }

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 +106,31 @@ public override bool Execute()
}

ConfigFileContents = builder.ToString();
return true;
return string.IsNullOrEmpty(FileName.ItemSpec) ? true : 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
{
var targetFileName = FileName.ItemSpec;
if (File.Exists(targetFileName))
{
string existingContents = File.ReadAllText(targetFileName);
if (existingContents.Equals(ConfigFileContents))
{
return true;
}
}
var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
File.WriteAllText(targetFileName, 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,10 +7,12 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -351,5 +353,29 @@ public void ItemMetadataPathIsAdjustedOnWindows()
build_metadata.Compile.ToRetrieve = abc123
", result);
}

[Fact]
public void ConfigFileCanBeWrittenToDisk()
{
ITaskItem property1 = MSBuildUtil.CreateTaskItem("Property1", new Dictionary<string, string> { { "Value", "abc123" } });
ITaskItem property2 = MSBuildUtil.CreateTaskItem("Property2", new Dictionary<string, string> { { "Value", "def456" } });

var fileName = Path.Combine(TempRoot.Root, "ConfigFileCanBeWrittenToDisk.GenerateMSBuildEditorConfig.editorconfig");

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

var expectedContents = @"is_global = true
build_property.Property1 = abc123
build_property.Property2 = def456
";

Assert.True(File.Exists(fileName));
Assert.Equal(expectedContents, File.ReadAllText(fileName));
}
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