From dc25977584ae25e45791ec720e136564174b1a08 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Sun, 25 Jul 2021 15:26:47 -0700 Subject: [PATCH] Fix up bugs in parsing special chracters in EditorConfig (#54511) * Fix up bugs in parsing special chracters in EditorConfig * Address feedback from peer review * Update tests, clean lexer logic, update method mdoifiers * Fix method modifier --- .../Analyzers/AnalyzerConfigTests.cs | 22 ++++++++++++ .../GenerateMSBuildEditorConfig.cs | 34 ++++++++++++++++++- .../Microsoft.Managed.Core.targets | 8 ++--- .../GenerateMSBuildEditorConfigTests.cs | 27 +++++++++++++++ .../AnalyzerConfig.SectionNameMatching.cs | 16 +++++++++ .../Portable/CommandLine/AnalyzerConfigSet.cs | 3 +- 6 files changed, 102 insertions(+), 8 deletions(-) diff --git a/src/Compilers/Core/CodeAnalysisTest/Analyzers/AnalyzerConfigTests.cs b/src/Compilers/Core/CodeAnalysisTest/Analyzers/AnalyzerConfigTests.cs index b3b540ef660da..82c45f60b5ffa 100644 --- a/src/Compilers/Core/CodeAnalysisTest/Analyzers/AnalyzerConfigTests.cs +++ b/src/Compilers/Core/CodeAnalysisTest/Analyzers/AnalyzerConfigTests.cs @@ -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() { diff --git a/src/Compilers/Core/MSBuildTask/GenerateMSBuildEditorConfig.cs b/src/Compilers/Core/MSBuildTask/GenerateMSBuildEditorConfig.cs index d0eead0082aa4..a6b2f77ccfa03 100644 --- a/src/Compilers/Core/MSBuildTask/GenerateMSBuildEditorConfig.cs +++ b/src/Compilers/Core/MSBuildTask/GenerateMSBuildEditorConfig.cs @@ -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; @@ -38,6 +39,10 @@ namespace Microsoft.CodeAnalysis.BuildTasks /// public sealed class GenerateMSBuildEditorConfig : Task { + /// + /// Although this task does its own writing to disk, this + /// output parameter is here for testing purposes. + /// [Output] public string ConfigFileContents { get; set; } @@ -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(); PropertyItems = Array.Empty(); + FileName = new TaskItem(); } public override bool Execute() @@ -98,7 +106,31 @@ public override bool Execute() } ConfigFileContents = builder.ToString(); - return true; + return string.IsNullOrEmpty(FileName.ItemSpec) ? true : WriteMSBuildEditorConfig(); + } + + internal bool WriteMSBuildEditorConfig() + { + 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; + } } /// diff --git a/src/Compilers/Core/MSBuildTask/Microsoft.Managed.Core.targets b/src/Compilers/Core/MSBuildTask/Microsoft.Managed.Core.targets index d742060eeeab5..d9e01cfec3bcd 100644 --- a/src/Compilers/Core/MSBuildTask/Microsoft.Managed.Core.targets +++ b/src/Compilers/Core/MSBuildTask/Microsoft.Managed.Core.targets @@ -186,14 +186,10 @@ - - + MetadataItems="@(_GeneratedEditorConfigMetadata)" + FileName="$(GeneratedMSBuildEditorConfigFile)"> - -