Skip to content

Commit 05c8968

Browse files
authored
Merge pull request #1070 from ahoppen/ahoppen/invalid-compilation-db
Don’t crash if a compilation database is malformed JSON
2 parents acff38b + f7f474a commit 05c8968

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

Sources/LSPLogging/OrLog.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import os
1717
private func logError(prefix: String, error: Error, level: LogLevel) {
1818
logger.log(
1919
level: level,
20-
"\(prefix, privacy: .public)\(prefix.isEmpty ? "" : " ", privacy: .public)\(error.forLogging)"
20+
"\(prefix, privacy: .public)\(prefix.isEmpty ? "" : ": ", privacy: .public)\(error.forLogging)"
2121
)
2222
}
2323

Sources/SKCore/CompilationDatabase.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14+
import LSPLogging
1415
import SKSupport
1516

1617
import struct TSCBasic.AbsolutePath
@@ -81,13 +82,19 @@ public func tryLoadCompilationDatabase(
8182
try! RelativePath(validating: "build"),
8283
]
8384
return
84-
try! searchPaths
85+
searchPaths
8586
.lazy
8687
.map { directory.appending($0) }
87-
.compactMap {
88-
try
89-
(JSONCompilationDatabase(directory: $0, fileSystem)
90-
?? FixedCompilationDatabase(directory: $0, fileSystem))
88+
.compactMap { searchPath in
89+
orLog("Failed to load compilation database") { () -> CompilationDatabase? in
90+
if let compDb = try JSONCompilationDatabase(directory: searchPath, fileSystem) {
91+
return compDb
92+
}
93+
if let compDb = try FixedCompilationDatabase(directory: searchPath, fileSystem) {
94+
return compDb
95+
}
96+
return nil
97+
}
9198
}
9299
.first
93100
}

Tests/SKCoreTests/CompilationDatabaseTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@ final class CompilationDatabaseTests: XCTestCase {
267267
)
268268
}
269269

270+
func testInvalidCompilationDatabase() throws {
271+
let fs = InMemoryFileSystem()
272+
let dir = try AbsolutePath(validating: "/a")
273+
try fs.createDirectory(dir)
274+
try fs.writeFileContents(dir.appending(component: "compile_commands.json"), bytes: "")
275+
276+
XCTAssertNil(tryLoadCompilationDatabase(directory: dir, fs))
277+
}
278+
270279
func testCompilationDatabaseBuildSystem() async throws {
271280
try await checkCompilationDatabaseBuildSystem(
272281
"""

0 commit comments

Comments
 (0)