Skip to content

Commit 8ecd8f6

Browse files
committed
Fix the issue of indexPrefixMap being unordered, which causes the IndexStoreDB path to be unpredictable.
1 parent 1bc5e9b commit 8ecd8f6

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Sources/SourceKitLSP/Workspace.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,16 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {
306306
indexDelegate = SourceKitIndexDelegate()
307307
let prefixMappings =
308308
(indexOptions.indexPrefixMap ?? [:])
309+
.sorted {
310+
// Fixes an issue where remapPath might match the shortest path first when multiple common prefixes exist
311+
// Sort by path length descending to prioritize more specific paths;
312+
// when lengths are equal, sort lexicographically in ascending order
313+
if $0.original.count != $1.original.count {
314+
return $0.original.count > $1.original.count // Prefer longer paths (more specific)
315+
} else {
316+
return $0.original < $1.original // Alphabetical sort when lengths are equal, ensures stable ordering
317+
}
318+
}
309319
.map { PathMapping(original: $0.key, replacement: $0.value) }
310320
if let indexInjector = hooks.indexHooks.indexInjector {
311321
let indexStoreDB = try await indexInjector.createIndex(

0 commit comments

Comments
 (0)