Fix Uri.GetHashCode hash/equality contract violation for file: URIs#124652
Draft
Fix Uri.GetHashCode hash/equality contract violation for file: URIs#124652
Conversation
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
…just IsUncOrDosPath Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix Uri GetHashCode mismatch for equal instances
Fix Uri.GetHashCode hash/equality contract violation for file: URIs
Feb 20, 2026
This was referenced Feb 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uri.GetHashCodeusesIsUncOrDosPathto select betweenOrdinalIgnoreCaseand ordinal hashing, butEqualsusesthis.IsUncOrDosPathfor comparison mode — so two URIs with identical normalizedRemoteUrlbut differentIsUncOrDosPathflags (e.g. a UNC-derived URI and an explicitfile:///) compare equal yet produce different hash codes.Description
Root cause:
Flags.DosPathandFlags.UncPathare only ever set alongsidesyntax = UriParser.FileUri, soIsUncOrDosPath ⇒ IsFileis a provable invariant. A plainfile:///URI never sets those flags, soGetHashCodeused a different hash algorithm than a UNC-derived URI with the same normalized form.Fix: Replace
if (IsUncOrDosPath)withif (IsFile)inGetHashCode. This extendsOrdinalIgnoreCasehashing to allfile:URIs, ensuring consistent hash codes for any twofile:URIs thatEqualscan report as equal. No existing behavior changes for UNC/DOS paths; the only difference is that plainfile:URIs now also useOrdinalIgnoreCase.GetHashCode.Test: Added
new Uri("\\\\\u202a")vsnew Uri("file:///")toEquals_TestData, which is already consumed byEqualsTest— a harness that automatically verifies both equality symmetry and hash code consistency for equal pairs.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.