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

Make matching case insensitive regardless of OS Fixes #5749 #5888

Merged
merged 3 commits into from
Dec 4, 2020
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
27 changes: 7 additions & 20 deletions src/Build.OM.UnitTests/Definition/ProjectItem_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2820,7 +2820,7 @@ public void EscapeHatchTurnsOffQualifiedMetadataExpansionInUpdateOperation(bool
public void UpdateFromReferencedItemShouldBeCaseInsensitive()
{
string content = @"
<from Include='a'>
<from Include='A'>
<metadata>m1_contents</metadata>
</from>

Expand Down Expand Up @@ -3177,28 +3177,15 @@ public void UpdateAndRemoveShouldUseCaseInsensitiveMatching()

IList<ProjectItem> items = ObjectModelHelpers.GetItemsFromFragment(content);

if (FileUtilities.GetIsFileSystemCaseSensitive())
{
var expectedUpdated = new Dictionary<string, string>
{
{"m1", "m1_contents"},
{"m2", "m2_contents"},
};
items.ShouldHaveSingleItem();

ObjectModelHelpers.AssertItemHasMetadata(expectedUpdated, items[0]);
}
else
var expectedUpdated = new Dictionary<string, string>
{
items.ShouldHaveSingleItem();

var expectedUpdated = new Dictionary<string, string>
{
{"m1", "m1_updated"},
{"m2", "m2_updated"},
};
{"m1", "m1_updated"},
{"m2", "m2_updated"},
};

ObjectModelHelpers.AssertItemHasMetadata(expectedUpdated, items[0]);
}
ObjectModelHelpers.AssertItemHasMetadata(expectedUpdated, items[0]);
}

public static IEnumerable<Object[]> UpdateAndRemoveShouldWorkWithEscapedCharactersTestData
Expand Down
3 changes: 2 additions & 1 deletion src/Build/Utilities/FileSpecMatchTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Build.Shared;
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -42,7 +43,7 @@ public bool IsMatch(string fileToMatch)
return _regex.IsMatch(normalizedFileToMatch);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we account for ignoring case here as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely. The beautiful (terrible?) part is that it already ignored case:
https://github.com/dotnet/msbuild/blob/master/src/Shared/FileMatcher.cs#L51

I can add a test for that, though.

}

return FileUtilities.ComparePathsNoThrow(_unescapedFileSpec, fileToMatch, _currentDirectory);
return FileUtilities.ComparePathsNoThrow(_unescapedFileSpec, fileToMatch, _currentDirectory, alwaysIgnoreCase: true);
}

// this method parses the glob and extracts the fixed directory part in order to normalize it and make it absolute
Expand Down
8 changes: 5 additions & 3 deletions src/Shared/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,19 +735,21 @@ internal static string GetFullPathNoThrow(string path)
/// <param name="first"></param>
/// <param name="second"></param>
/// <param name="currentDirectory"></param>
/// <param name="alwaysIgnoreCase"></param>
/// <returns></returns>
internal static bool ComparePathsNoThrow(string first, string second, string currentDirectory)
internal static bool ComparePathsNoThrow(string first, string second, string currentDirectory, bool alwaysIgnoreCase = false)
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Not sure why it's 'always'IgnoreCase

Suggested change
internal static bool ComparePathsNoThrow(string first, string second, string currentDirectory, bool alwaysIgnoreCase = false)
internal static bool ComparePathsNoThrow(string first, string second, string currentDirectory, bool ignoreCase = false)

Copy link
Member Author

@Forgind Forgind Nov 20, 2020

Choose a reason for hiding this comment

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

I prefer alwaysIgnoreCase because putting false here doesn't mean that you're case sensitive but that it depends on the OS. OS-dependent and always case-insensitive are the two I think of as common cases, so this distinguishes between them.

{
StringComparison pathComparison = alwaysIgnoreCase ? StringComparison.OrdinalIgnoreCase : PathComparison;
// perf: try comparing the bare strings first
if (string.Equals(first, second, PathComparison))
if (string.Equals(first, second, pathComparison))
{
return true;
}

var firstFullPath = NormalizePathForComparisonNoThrow(first, currentDirectory);
var secondFullPath = NormalizePathForComparisonNoThrow(second, currentDirectory);

return string.Equals(firstFullPath, secondFullPath, PathComparison);
return string.Equals(firstFullPath, secondFullPath, pathComparison);
}

/// <summary>
Expand Down