Skip to content

Commit 4b6b243

Browse files
committed
If a build server doesn’t specify -index-store-path in the SourceKit options, add it during background indexing
1 parent 1727a7b commit 4b6b243

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

Sources/BuildSystemIntegration/FixedCompilationDatabaseBuildSystem.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import LanguageServerProtocol
2424
#endif
2525

2626
func lastIndexStorePathArgument(in compilerArgs: [String]) -> String? {
27-
for i in compilerArgs.indices.reversed() {
28-
if compilerArgs[i] == "-index-store-path" && i + 1 < compilerArgs.count {
29-
return compilerArgs[i + 1]
30-
}
27+
if let indexStorePathIndex = compilerArgs.lastIndex(of: "-index-store-path"),
28+
indexStorePathIndex + 1 < compilerArgs.count
29+
{
30+
return compilerArgs[indexStorePathIndex + 1]
3131
}
3232
return nil
3333
}

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
311311
return
312312
}
313313

314-
let args =
314+
var args =
315315
try [swiftc.filePath] + buildSettings.compilerArguments + [
316316
"-index-file",
317317
"-index-file-path", uri.pseudoPath,
@@ -320,6 +320,25 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
320320
// Fake an output path so that we get a different unit file for every Swift file we background index
321321
"-index-unit-output-path", uri.pseudoPath + ".o",
322322
]
323+
guard let buildSystemIndexStorePath = await self.buildSystemManager.initializationData?.indexStorePath else {
324+
struct NoIndexStorePathError: Error {}
325+
throw NoIndexStorePathError()
326+
}
327+
if let indexStorePathIndex = args.lastIndex(of: "-index-store-path"), indexStorePathIndex + 1 < args.count {
328+
let indexStorePath = args[indexStorePathIndex + 1]
329+
if indexStorePath != buildSystemIndexStorePath {
330+
logger.error(
331+
"""
332+
Compiler arguments for \(uri) specify index store path \(indexStorePath) but build system specified an \
333+
incompatible index store path \(buildSystemIndexStorePath). Overriding with the path specified by the build \
334+
system.
335+
"""
336+
)
337+
args[indexStorePathIndex + 1] = buildSystemIndexStorePath
338+
}
339+
} else {
340+
args += ["-index-store-path", buildSystemIndexStorePath]
341+
}
323342

324343
try await runIndexingProcess(
325344
indexFile: uri,

0 commit comments

Comments
 (0)