Skip to content

Commit

Permalink
Ignore drive casing when comparing on windows platforms (#73380)
Browse files Browse the repository at this point in the history
Razor adds properties for razor files in msbuild targets. The casing for the section names can be inconsistent when pulling in the drive letter, causing the source generator not to run in hot reload scenarios. This normalizes the drive casing for section names and retrieval of options to fix hot reload for vs code
  • Loading branch information
ryzngard committed May 22, 2024
1 parent c221e3f commit 2cd8f0b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
using static Microsoft.CodeAnalysis.AnalyzerConfig;
using static Roslyn.Test.Utilities.TestHelpers;
Expand Down Expand Up @@ -63,30 +64,30 @@ public void ConfigWithEscapedValues()
{
var config = ParseConfigFile(@"is_global = true
[c:/\{f\*i\?le1\}.cs]
[C:/\{f\*i\?le1\}.cs]
build_metadata.Compile.ToRetrieve = abc123
[c:/f\,ile\#2.cs]
[C:/f\,ile\#2.cs]
build_metadata.Compile.ToRetrieve = def456
[c:/f\;i\!le\[3\].cs]
[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);
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);
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);
Assert.Equal("C:/f\\;i\\!le\\[3\\].cs", namedSections[2].Name);
AssertEx.Equal(
new[] { KeyValuePair.Create("build_metadata.compile.toretrieve", "ghi789") },
namedSections[2].Properties
Expand Down Expand Up @@ -115,6 +116,40 @@ public void CanGetSectionsWithSpecialCharacters()
Assert.Equal("def456", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);
}

[ConditionalFact(typeof(WindowsOnly))]
public void CanGetSectionsWithDifferentDriveCasing()
{
var config = Parse(@"is_global = true
build_metadata.compile.toretrieve = global
[c:/goo/file.cs]
build_metadata.compile.toretrieve = abc123
[C:/goo/other.cs]
build_metadata.compile.toretrieve = def456
", pathToFile: @"C:/.editorconfig");

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

var sectionOptions = set.GetOptionsForSourcePath(@"c:\goo\file.cs");
Assert.Equal("abc123", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);

sectionOptions = set.GetOptionsForSourcePath(@"C:\goo\file.cs");
Assert.Equal("abc123", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);

sectionOptions = set.GetOptionsForSourcePath(@"C:\goo\other.cs");
Assert.Equal("def456", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);

sectionOptions = set.GetOptionsForSourcePath(@"c:\goo\other.cs");
Assert.Equal("def456", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);

sectionOptions = set.GetOptionsForSourcePath(@"c:\global.cs");
Assert.Equal("global", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);

sectionOptions = set.GetOptionsForSourcePath(@"C:\global.cs");
Assert.Equal("global", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);
}

[ConditionalFact(typeof(WindowsOnly))]
public void WindowsPath()
{
Expand Down Expand Up @@ -2226,7 +2261,7 @@ public void GlobalConfigOptionsAreEmptyWhenNoGlobalConfig()
[InlineData(@"\", false)] //invalid: editorconfig sees a single escape character
[InlineData(@"\\", false)] //invalid: editorconfig sees an escaped, literal backslash
[InlineData(@"/\{\}\,\[\]\*", true)]
[InlineData(@"c:\my\file.cs", false)] // invalid: editorconfig sees a single file called 'c:(\m)y(\f)ile.cs' (i.e. \m and \f are escape chars)
[InlineData(@"C:\my\file.cs", false)] // invalid: editorconfig sees a single file called 'c:(\m)y(\f)ile.cs' (i.e. \m and \f are escape chars)
[InlineData(@"\my\file.cs", false)] // invalid: editorconfig sees a single file called '(\m)y(\f)ile.cs'
[InlineData(@"\\my\\file.cs", false)] // invalid: editorconfig sees a single file called '\my\file.cs' with literal backslashes
[InlineData(@"\\\\my\\file.cs", false)] // invalid: editorconfig sees a single file called '\\my\file.cs' not a UNC path
Expand Down Expand Up @@ -2261,20 +2296,20 @@ public void GlobalConfigIssuesWarningWithInvalidSectionNames(string sectionName,
}

[Theory]
[InlineData("c:/myfile.cs", true, false)]
[InlineData("C:/myfile.cs", true, false)]
[InlineData("cd:/myfile.cs", false, false)] // windows only allows a single character as a drive specifier
[InlineData(@"\c\:\/myfile.cs", true, false)] // allow escaped characters
[InlineData("/myfile.cs", true, true)] //absolute, with a relative drive root
[InlineData("c:myfile.cs", false, false)] //relative, wit2h an absolute drive root
[InlineData(@"c:\myfile.cs", false, false)] //not a valid editorconfig path
[InlineData(@"C:\myfile.cs", false, false)] //not a valid editorconfig path
[InlineData("//?/C:/Test/Foo.txt", false, false)] // ? is a special char in editorconfig
[InlineData(@"//\?/C:/Test/Foo.txt", true, true)]
[InlineData(@"\\?\C:\Test\Foo.txt", false, false)]
[InlineData(@"c:", false, false)]
[InlineData(@"c\", false, false)]
[InlineData(@"C:", false, false)]
[InlineData(@"C\", false, false)]
[InlineData(@"\c\:", false, false)]
[InlineData("c:/", true, false)]
[InlineData("c:/*.cs", false, false)]
[InlineData("C:/", true, false)]
[InlineData("C:/*.cs", false, false)]
public void GlobalConfigIssuesWarningWithInvalidSectionNames_PlatformSpecific(string sectionName, bool isValidWindows, bool isValidOther)
=> GlobalConfigIssuesWarningWithInvalidSectionNames(sectionName, ExecutionConditionUtil.IsWindows ? isValidWindows : isValidOther);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,5 +435,29 @@ public void CollapseWithForwardSlash(string input, string output)
{
AssertEx.Equal(output, PathUtilities.CollapseWithForwardSlash(input.AsSpan()));
}

[ConditionalTheory(typeof(WindowsOnly))]
[InlineData(@"//a/b/c", @"//a/b/c")]
[InlineData(@"/a\b/c/", @"/a\b/c/")]
[InlineData(@"C:B", @"C:B")]
[InlineData(@"c:b", @"c:b")]
[InlineData(@"c:\b", @"C:\b")]
[InlineData(@"c:/b", @"C:/b")]
public void NormalizeDriveLetter_Windows(string input, string output)
{
AssertEx.Equal(output, PathUtilities.NormalizeDriveLetter(input));
}

[ConditionalTheory(typeof(UnixLikeOnly))]
[InlineData(@"//a/b/c")]
[InlineData(@"/a\b/c/")]
[InlineData(@"C:B")]
[InlineData(@"c:b")]
[InlineData(@"c:\b")]
[InlineData(@"c:/b")]
public void NormalizeDriveLetter_UnixLike(string input)
{
AssertEx.Equal(input, PathUtilities.NormalizeDriveLetter(input));
}
}
}
7 changes: 6 additions & 1 deletion src/Compilers/Core/Portable/CommandLine/AnalyzerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,17 @@ public static AnalyzerConfig Parse(SourceText text, string? pathToFile)
// Add the last section
addNewSection();

// Normalize the path to file the same way named sections are
pathToFile = PathUtilities.NormalizeDriveLetter(pathToFile);

return new AnalyzerConfig(globalSection!, namedSectionBuilder.ToImmutable(), pathToFile);

void addNewSection()
{
var sectionName = PathUtilities.NormalizeDriveLetter(activeSectionName);

// Close out the previous section
var previousSection = new Section(activeSectionName, activeSectionProperties.ToImmutable());
var previousSection = new Section(sectionName, activeSectionProperties.ToImmutable());
if (activeSectionName == "")
{
// This is the global section
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public AnalyzerConfigOptionsResult GetOptionsForSourcePath(string sourcePath)

var normalizedPath = PathUtilities.CollapseWithForwardSlash(sourcePath.AsSpan());
normalizedPath = PathUtilities.ExpandAbsolutePathWithRelativeParts(normalizedPath);
normalizedPath = PathUtilities.NormalizeDriveLetter(normalizedPath);

// If we have a global config, add any sections that match the full path. We can have at most one section since
// we would have merged them earlier.
Expand Down
10 changes: 10 additions & 0 deletions src/Compilers/Core/Portable/FileSystem/PathUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,16 @@ public static string NormalizePathPrefix(string filePath, ImmutableArray<KeyValu
return filePath;
}

public static string NormalizeDriveLetter(string filePath)
{
if (!IsUnixLikePlatform && IsDriveRootedAbsolutePath(filePath))
{
filePath = char.ToUpper(filePath[0]) + filePath.Substring(1);
}

return filePath;
}

/// <summary>
/// Unfortunately, we cannot depend on Path.GetInvalidPathChars() or Path.GetInvalidFileNameChars()
/// From MSDN: The array returned from this method is not guaranteed to contain the complete set of characters
Expand Down

0 comments on commit 2cd8f0b

Please sign in to comment.