fix: prevent wildcard pattern from overriding exact match in FindBestPatternMatch#3618
fix: prevent wildcard pattern from overriding exact match in FindBestPatternMatch#3618kuishou68 wants to merge 1 commit into
Conversation
…PatternMatch (Closes microsoft#3617) The original TypeScript uses pattern.prefix.length as the betterness criterion, which for exact matches equals the full string length. The Go port used pattern.StarIndex directly, which is -1 for exact matches, allowing any subsequent wildcard (StarIndex >= 0) to incorrectly override the exact match. Fix: set longestMatchPrefixLength = math.MaxInt when an exact match is found. Signed-off-by: cocoon <54054995+kuishou68@users.noreply.github.com>
|
This will require a test, preferably one we can cross check with the old code to double check it |
|
As far as I remember, this code path isn't supposed to be hit for an exact match. Did you actually run into a problem with a specific project? |
|
Yes — ran into it while tracing how I can add a test that runs both the Go and a reference table side-by-side if that would help verify the fix. |
Closes #3617
Problem
In
internal/core/pattern.go,FindBestPatternMatchusespattern.StarIndexas the betterness criterion when comparing matched patterns. For exact matches,StarIndex == -1, so after selecting an exact match,longestMatchPrefixLengthis set to-1. Any subsequent wildcard pattern withStarIndex >= 0satisfiespattern.StarIndex > longestMatchPrefixLength(N > -1), incorrectly overriding the exact match.Example: given patterns
["foo/bar" (exact), "foo/*" (StarIndex=4)]and candidate"foo/bar":"foo/bar"matches →longestMatchPrefixLength = -1"foo/*"matches →4 > -1= true → overrides exact match ❌Root Cause vs Original TypeScript
The original TypeScript uses
pattern.prefix.length(the full text length for exact matches) as the betterness criterion — a large positive number that cannot be overridden by any wildcard. The Go port incorrectly usedpattern.StarIndex(which is-1for exact matches).Fix
Set
longestMatchPrefixLength = math.MaxIntwhen an exact match is found, so no wildcard can override it — matching the semantics of the original TypeScript.Signed-off-by: cocoon 54054995+kuishou68@users.noreply.github.com