Skip to content

Commit 66793ff

Browse files
authored
Merge pull request #2000 from ahoppen/index-store-path-option
If a build server doesn’t specify `-index-store-path` in the SourceKit options, add it during background indexing
2 parents 3e7f350 + 628ce3c commit 66793ff

File tree

9 files changed

+42
-57
lines changed

9 files changed

+42
-57
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/SKOptions/SourceKitLSPOptions.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,6 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
182182
}
183183

184184
public struct IndexOptions: Sendable, Codable, Equatable {
185-
/// Directory in which a separate compilation stores the index store. By default, inferred from the build system.
186-
public var indexStorePath: String?
187-
/// Directory in which the indexstore-db should be stored. By default, inferred from the build system.
188-
public var indexDatabasePath: String?
189185
/// Path remappings for remapping index data for local use.
190186
public var indexPrefixMap: [String: String]?
191187
/// A hint indicating how many cores background indexing should use at most (value between 0 and 1). Background indexing is not required to honor this setting.
@@ -208,23 +204,17 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
208204
}
209205

210206
public init(
211-
indexStorePath: String? = nil,
212-
indexDatabasePath: String? = nil,
213207
indexPrefixMap: [String: String]? = nil,
214208
maxCoresPercentageToUseForBackgroundIndexing: Double? = nil,
215209
updateIndexStoreTimeout: Int? = nil
216210
) {
217-
self.indexStorePath = indexStorePath
218-
self.indexDatabasePath = indexDatabasePath
219211
self.indexPrefixMap = indexPrefixMap
220212
self.maxCoresPercentageToUseForBackgroundIndexing = maxCoresPercentageToUseForBackgroundIndexing
221213
self.updateIndexStoreTimeout = updateIndexStoreTimeout
222214
}
223215

224216
static func merging(base: IndexOptions, override: IndexOptions?) -> IndexOptions {
225217
return IndexOptions(
226-
indexStorePath: override?.indexStorePath ?? base.indexStorePath,
227-
indexDatabasePath: override?.indexDatabasePath ?? base.indexDatabasePath,
228218
indexPrefixMap: override?.indexPrefixMap ?? base.indexPrefixMap,
229219
maxCoresPercentageToUseForBackgroundIndexing: override?.maxCoresPercentageToUseForBackgroundIndexing
230220
?? base.maxCoresPercentageToUseForBackgroundIndexing,

Sources/SKTestSupport/IndexedSingleSwiftFileTestProject.swift

Lines changed: 1 addition & 8 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
}
@@ -147,13 +145,8 @@ package struct IndexedSingleSwiftFileTestProject {
147145
}
148146

149147
// Create the test client
150-
var options = try await SourceKitLSPOptions.testDefault()
151-
options.indexOrDefault = SourceKitLSPOptions.IndexOptions(
152-
indexStorePath: try indexURL.filePath,
153-
indexDatabasePath: try indexDBURL.filePath
154-
)
155148
self.testClient = try await TestSourceKitLSPClient(
156-
options: options,
149+
options: try await SourceKitLSPOptions.testDefault(),
157150
capabilities: capabilities,
158151
workspaceFolders: [
159152
WorkspaceFolder(uri: DocumentURI(testWorkspaceDirectory))

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
}

Sources/SourceKitLSP/Workspace.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,13 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {
273273

274274
let indexOptions = options.indexOrDefault
275275
let indexStorePath: URL? =
276-
if let indexStorePath = indexOptions.indexStorePath {
277-
URL(fileURLWithPath: indexStorePath, relativeTo: rootUri?.fileURL)
278-
} else if let indexStorePath = await buildSystemManager.initializationData?.indexStorePath {
276+
if let indexStorePath = await buildSystemManager.initializationData?.indexStorePath {
279277
URL(fileURLWithPath: indexStorePath, relativeTo: rootUri?.fileURL)
280278
} else {
281279
nil
282280
}
283281
let indexDatabasePath: URL? =
284-
if let indexDatabasePath = indexOptions.indexDatabasePath {
285-
URL(fileURLWithPath: indexDatabasePath, relativeTo: rootUri?.fileURL)
286-
} else if let indexDatabasePath = await buildSystemManager.initializationData?.indexDatabasePath {
282+
if let indexDatabasePath = await buildSystemManager.initializationData?.indexDatabasePath {
287283
URL(fileURLWithPath: indexDatabasePath, relativeTo: rootUri?.fileURL)
288284
} else {
289285
nil

Sources/sourcekit-lsp/SourceKitLSP.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,6 @@ struct SourceKitLSP: AsyncParsableCommand {
112112
)
113113
var clangdOptions = [String]()
114114

115-
@Option(
116-
name: .customLong("index-store-path", withSingleDash: true),
117-
help: "Override index-store-path from the build system"
118-
)
119-
var indexStorePath: String?
120-
121-
@Option(
122-
name: .customLong("index-db-path", withSingleDash: true),
123-
help: "Override index-database-path from the build system"
124-
)
125-
var indexDatabasePath: String?
126-
127115
@Option(
128116
name: .customLong("index-prefix-map", withSingleDash: true),
129117
parsing: .unconditionalSingleValue,
@@ -177,8 +165,6 @@ struct SourceKitLSP: AsyncParsableCommand {
177165
compilationDatabase: SourceKitLSPOptions.CompilationDatabaseOptions(searchPaths: compilationDatabaseSearchPaths),
178166
clangdOptions: clangdOptions,
179167
index: SourceKitLSPOptions.IndexOptions(
180-
indexStorePath: indexStorePath,
181-
indexDatabasePath: indexDatabasePath,
182168
indexPrefixMap: [String: String](
183169
indexPrefixMappings.map { ($0.original, $0.replacement) },
184170
uniquingKeysWith: { lhs, rhs in rhs }

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)