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 13 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 @@ -115,6 +115,33 @@ public void CanGetSectionsWithSpecialCharacters()
Assert.Equal("def456", sectionOptions.AnalyzerOptions["build_metadata.compile.toretrieve"]);
}

[ConditionalFact(typeof(WindowsOnly))]
public void CanGetSectionsWithDifferentDriveCasing()
{
var config = ParseConfigFile(@"is_global = true

[c:\goo\file.cs]
build_metadata.compile.toretrieve = abc123

[C:\goo\other.cs]
build_metadata.compile.toretrieve = def456
");

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"]);
}

[ConditionalFact(typeof(WindowsOnly))]
public void WindowsPath()
{
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));
}
}
}
4 changes: 3 additions & 1 deletion src/Compilers/Core/Portable/CommandLine/AnalyzerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,10 @@ public static AnalyzerConfig Parse(SourceText text, string? 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
Loading