BSP integration - ambiguity in compilerArguments format (original) (raw)
I've been working on a BSP to integrate with the LSP and noticed a strange ambiguity, specifically whether to include the compiler itself as the first argument in TextDocumentSourceKitOptionsResponse.compilerArguments.
In my BSP currently, I follow the clang compile DB format, so I do include it. This results in SourceKitService errors like these:
[1:getBufferStamp:15619: 1.0330] failed to stat file: <working-dir>/swiftc (No such file or directory)|
[1:fileContentsForFilesInCompilerInvocation:15619: 1.0330] failed getting file contents for <working-dir>/swiftc: error opening input file '<working-dir>/swiftc' (No such file or directory)|
So clearly something's trying to interpret the compiler relative to the working dir, which is almost always incorrect.
Looking at the built in build system in the LSP, I see that FixedCompilationDatabaseBuildSystem does include it (without absolute path, like my implementation):
return TextDocumentSourceKitOptionsResponse(
compilerArguments: [compilerName] + compilerArgs + [request.textDocument.uri.pseudoPath],
workingDirectory: try? configPath.deletingLastPathComponent().filePath
)
and both JSONCompilationDatabaseBuildSystem and SwiftPMBuildSystem drop it:
return TextDocumentSourceKitOptionsResponse(
compilerArguments: Array(command.commandLine.dropFirst()),
workingDirectory: command.directory
)
func compileArguments(for fileURL: URL) throws -> [String] {
// Note: we ignore the `fileURL` here as the expectation is that we get a command line for the entire target
// in case of Swift.
let commandLine = try description.emitCommandLine(scanInvocation: false, writeOutputFileMap: false)
// First element on the command line is the compiler itself, not an argument.
return Array(commandLine.dropFirst())
}
Some clarity would be appreciated here. It's unclear whether this is spec ambiguity, or a bug in the LSP / sourcekitd.
ahoppen (Alex Hoppen) May 6, 2025, 7:15am 2
Including the compiler in the response FixedCompilationDatabaseBuildSystem was a bug that was fixed by Fix issue that caused code completion to fail using compile_flags.txt by ahoppen · Pull Request #2063 · swiftlang/sourcekit-lsp · GitHub. The textDocument/sourceKitOptions response should not return the path to the compiler. The toolchain that contains the compiler can be specified in the workspace/buildTargets response.
lukascap (Lukas) May 6, 2025, 12:49pm 3
Gotcha. Should that be documented? I think this will be a common bug for people transitioning from compile_commands.json, which should have the executable:
https://clang.llvm.org/docs/JSONCompilationDatabase.html
I can make a PR.
ahoppen (Alex Hoppen) May 7, 2025, 7:12am 4
Yes, a PR would be very welcome. Thank you!