Skip to content

Commit 1ebc5a7

Browse files
authored
Fix dedupe/redirect nondeterminism (microsoft#2628)
1 parent 801590b commit 1ebc5a7

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

internal/modulespecifiers/specifiers.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ func getAllModulePathsWorker(
208208
allFileNames[p.FileName] = p
209209
}
210210

211+
useCaseSensitiveFileNames := info.UseCaseSensitiveFileNames
212+
comparePaths := func(a, b ModulePath) int {
213+
return comparePathsByRedirect(a, b, useCaseSensitiveFileNames)
214+
}
215+
211216
// Sort by paths closest to importing file Name directory
212217
sortedPaths := make([]ModulePath, 0, len(paths))
213218
for directory := info.SourceDirectory; len(allFileNames) != 0; {
@@ -220,7 +225,7 @@ func getAllModulePathsWorker(
220225
}
221226
}
222227
if len(pathsInDirectory) > 0 {
223-
slices.SortStableFunc(pathsInDirectory, comparePathsByRedirectAndNumberOfDirectorySeparators)
228+
slices.SortFunc(pathsInDirectory, comparePaths)
224229
sortedPaths = append(sortedPaths, pathsInDirectory...)
225230
}
226231
newDirectory := tspath.GetDirectoryPath(directory)
@@ -231,7 +236,7 @@ func getAllModulePathsWorker(
231236
}
232237
if len(allFileNames) > 0 {
233238
remainingPaths := slices.Collect(maps.Values(allFileNames))
234-
slices.SortStableFunc(remainingPaths, comparePathsByRedirectAndNumberOfDirectorySeparators)
239+
slices.SortFunc(remainingPaths, comparePaths)
235240
sortedPaths = append(sortedPaths, remainingPaths...)
236241
}
237242
return sortedPaths

internal/modulespecifiers/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ var (
2626
regexPatternCache = make(map[regexPatternCacheKey]*regexp2.Regexp)
2727
)
2828

29-
func comparePathsByRedirectAndNumberOfDirectorySeparators(a ModulePath, b ModulePath) int {
29+
func comparePathsByRedirect(a ModulePath, b ModulePath, useCaseSensitiveFileNames bool) int {
3030
if a.IsRedirect == b.IsRedirect {
31-
return strings.Count(a.FileName, "/") - strings.Count(b.FileName, "/")
31+
return tspath.ComparePaths(a.FileName, b.FileName, tspath.ComparePathsOptions{UseCaseSensitiveFileNames: useCaseSensitiveFileNames})
3232
}
3333
if a.IsRedirect {
3434
return 1

0 commit comments

Comments
 (0)