Skip to content

Commit 628ce3c

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 628ce3c

File tree

6 files changed

+39
-21
lines changed

6 files changed

+39
-21
lines changed

Documentation/Configuration File.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ The structure of the file is currently not guaranteed to be stable. Options may
3737
- `buildSettingsTimeout: integer`: Number of milliseconds to wait for build settings from the build system before using fallback build settings.
3838
- `clangdOptions: string[]`: Extra command line arguments passed to `clangd` when launching it.
3939
- `index`: Options related to indexing.
40-
- `indexStorePath: string`: Directory in which a separate compilation stores the index store. By default, inferred from the build system.
41-
- `indexDatabasePath: string`: Directory in which the indexstore-db should be stored. By default, inferred from the build system.
4240
- `indexPrefixMap: [string: string]`: Path remappings for remapping index data for local use.
4341
- `updateIndexStoreTimeout: integer`: Number of seconds to wait for an update index store task to finish before killing it.
4442
- `logging`: Options related to logging, changing SourceKit-LSP’s logging behavior on non-Apple platforms. On Apple platforms, logging is done through the [system log](Diagnose%20Bundle.md#Enable%20Extended%20Logging). These options can only be set globally and not per workspace.

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/SKTestSupport/IndexedSingleSwiftFileTestProject.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ package struct IndexedSingleSwiftFileTestProject {
3838
package let testClient: TestSourceKitLSPClient
3939
package let fileURI: DocumentURI
4040
package let positions: DocumentPositions
41-
package let indexDBURL: URL
4241

4342
/// Writes a single file to a temporary directory on disk and compiles it to index it.
4443
///
@@ -62,7 +61,6 @@ package struct IndexedSingleSwiftFileTestProject {
6261

6362
let testFileURL = testWorkspaceDirectory.appendingPathComponent("test.swift")
6463
let indexURL = testWorkspaceDirectory.appendingPathComponent("index")
65-
self.indexDBURL = testWorkspaceDirectory.appendingPathComponent("index-db")
6664
guard let swiftc = await ToolchainRegistry.forTesting.default?.swiftc else {
6765
throw Error.swiftcNotFound
6866
}

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,33 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
299299
await indexStoreUpToDateTracker.markUpToDate([file.sourceFile], updateOperationStartDate: startDate)
300300
}
301301

302+
/// If `args` does not contain an `-index-store-path` argument, add it, pointing to the build system's index store
303+
/// path. If an `-index-store-path` already exists, validate that it matches the build system's index store path and
304+
/// replace it by the build system's index store path if they don't match.
305+
private func addOrReplaceIndexStorePath(in args: [String], for uri: DocumentURI) async throws -> [String] {
306+
var args = args
307+
guard let buildSystemIndexStorePath = await self.buildSystemManager.initializationData?.indexStorePath else {
308+
struct NoIndexStorePathError: Error {}
309+
throw NoIndexStorePathError()
310+
}
311+
if let indexStorePathIndex = args.lastIndex(of: "-index-store-path"), indexStorePathIndex + 1 < args.count {
312+
let indexStorePath = args[indexStorePathIndex + 1]
313+
if indexStorePath != buildSystemIndexStorePath {
314+
logger.error(
315+
"""
316+
Compiler arguments for \(uri) specify index store path \(indexStorePath) but build system specified an \
317+
incompatible index store path \(buildSystemIndexStorePath). Overriding with the path specified by the build \
318+
system.
319+
"""
320+
)
321+
args[indexStorePathIndex + 1] = buildSystemIndexStorePath
322+
}
323+
} else {
324+
args += ["-index-store-path", buildSystemIndexStorePath]
325+
}
326+
return args
327+
}
328+
302329
private func updateIndexStore(
303330
forSwiftFile uri: DocumentURI,
304331
buildSettings: FileBuildSettings,
@@ -311,7 +338,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
311338
return
312339
}
313340

314-
let args =
341+
var args =
315342
try [swiftc.filePath] + buildSettings.compilerArguments + [
316343
"-index-file",
317344
"-index-file-path", uri.pseudoPath,
@@ -320,6 +347,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
320347
// Fake an output path so that we get a different unit file for every Swift file we background index
321348
"-index-unit-output-path", uri.pseudoPath + ".o",
322349
]
350+
args = try await addOrReplaceIndexStorePath(in: args, for: uri)
323351

324352
try await runIndexingProcess(
325353
indexFile: uri,
@@ -341,10 +369,13 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
341369
return
342370
}
343371

372+
var args = [try clang.filePath] + buildSettings.compilerArguments
373+
args = try await addOrReplaceIndexStorePath(in: args, for: uri)
374+
344375
try await runIndexingProcess(
345376
indexFile: uri,
346377
buildSettings: buildSettings,
347-
processArguments: [clang.filePath] + buildSettings.compilerArguments,
378+
processArguments: args,
348379
workingDirectory: buildSettings.workingDirectory.map(AbsolutePath.init(validating:))
349380
)
350381
}

Tests/SourceKitLSPTests/IndexTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ final class IndexTests: XCTestCase {
128128
XCTAssertEqual(jump.first?.uri, project.fileURI)
129129
XCTAssertEqual(jump.first?.range.lowerBound, project.positions["1️⃣"])
130130

131-
let tmpContents = try listdir(project.indexDBURL)
131+
let indexDBURL = project.fileURI.fileURL!.deletingLastPathComponent().appendingPathComponent("IndexDatabase")
132+
let tmpContents = try listdir(indexDBURL)
132133
guard let versionedPath = tmpContents.filter({ $0.lastPathComponent.starts(with: "v") }).only else {
133134
XCTFail("expected one version path 'v[0-9]*', found \(tmpContents)")
134135
return nil

config.schema.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@
126126
"description" : "Options related to indexing.",
127127
"markdownDescription" : "Options related to indexing.",
128128
"properties" : {
129-
"indexDatabasePath" : {
130-
"description" : "Directory in which the indexstore-db should be stored. By default, inferred from the build system.",
131-
"markdownDescription" : "Directory in which the indexstore-db should be stored. By default, inferred from the build system.",
132-
"type" : "string"
133-
},
134129
"indexPrefixMap" : {
135130
"additionalProperties" : {
136131
"type" : "string"
@@ -139,11 +134,6 @@
139134
"markdownDescription" : "Path remappings for remapping index data for local use.",
140135
"type" : "object"
141136
},
142-
"indexStorePath" : {
143-
"description" : "Directory in which a separate compilation stores the index store. By default, inferred from the build system.",
144-
"markdownDescription" : "Directory in which a separate compilation stores the index store. By default, inferred from the build system.",
145-
"type" : "string"
146-
},
147137
"updateIndexStoreTimeout" : {
148138
"description" : "Number of seconds to wait for an update index store task to finish before killing it.",
149139
"markdownDescription" : "Number of seconds to wait for an update index store task to finish before killing it.",

0 commit comments

Comments
 (0)