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

Ignore drive casing when comparing on windows platforms #73380

Merged
merged 19 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why we have separate normalize helpers vs. putting them all in one method? Basically I'm wondering if there is ever a case where it's okay to call only some of these normalized methods but not othhers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To my understanding the section name storage from parsing only needs to call this one, while retrieval of options for a filepath should call both. @jjonescz is that correct?

Copy link
Member

@jjonescz jjonescz May 21, 2024

Choose a reason for hiding this comment

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

It's correct that calling just the one for section names fixes the bug at hand. But I agree with Jared that we could call both even for section names, I imagine that would fix some more inconsistencies. For example, a section [*//*.cs] doesn't match file a/b.cs today probably. I'm not sure it should per editorconfig spec (if there is such a thing) though!

If we are not going to do that in this PR, we should at least file an issue to follow up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm happy for issue + follow up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

{
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