Skip to content

Commit 89aa8ee

Browse files
authored
adjust API to changes in SwiftPM (#433)
* adjust API to changes in SwiftPM motivation: stay with the times changes: * use observability system instead of diganostics engine * some APIs in SwiftPM are now throwing, adjust to this change
1 parent 8b71b01 commit 89aa8ee

File tree

2 files changed

+60
-47
lines changed

2 files changed

+60
-47
lines changed

Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#if canImport(SPMBuildCore)
1414
import SPMBuildCore
1515
#endif
16+
import Basics
1617
import Build
1718
import BuildServerProtocol
1819
import LanguageServerProtocol
@@ -157,19 +158,21 @@ extension SwiftPMWorkspace {
157158
/// dependencies.
158159
func reloadPackage() throws {
159160

160-
let diags = DiagnosticsEngine(handlers: [{ diag in
161-
log(diag.localizedDescription, level: diag.behavior.asLogLevel)
162-
}])
161+
let observabilitySystem = ObservabilitySystem({ scope, diagnostic in
162+
log(diagnostic.description, level: diagnostic.severity.asLogLevel)
163+
})
163164

164165
self.packageGraph = try self.workspace.loadPackageGraph(
165166
rootInput: PackageGraphRootInput(packages: [packageRoot]),
166-
diagnostics: diags)
167+
observabilityScope: observabilitySystem.topScope
168+
)
167169

168170
let plan = try BuildPlan(
169171
buildParameters: buildParameters,
170172
graph: packageGraph,
171-
diagnostics: diags,
172-
fileSystem: fileSystem)
173+
fileSystem: fileSystem,
174+
observabilityScope: observabilitySystem.topScope
175+
)
173176

174177
self.fileToTarget = [AbsolutePath: TargetBuildDescription](
175178
packageGraph.allTargets.flatMap { target in
@@ -213,7 +216,7 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
213216

214217
public func settings(
215218
for uri: DocumentURI,
216-
_ language: Language) -> FileBuildSettings?
219+
_ language: Language) throws -> FileBuildSettings?
217220
{
218221
guard let url = uri.fileURL else {
219222
// We can't determine build settings for non-file URIs.
@@ -224,15 +227,15 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
224227
}
225228

226229
if let td = targetDescription(for: path) {
227-
return settings(for: path, language, td)
230+
return try settings(for: path, language, td)
228231
}
229232

230233
if path.basename == "Package.swift" {
231234
return settings(forPackageManifest: path)
232235
}
233236

234237
if path.extension == "h" {
235-
return settings(forHeader: path, language)
238+
return try settings(forHeader: path, language)
236239
}
237240

238241
return nil
@@ -242,9 +245,18 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
242245
guard let delegate = self.delegate else { return }
243246

244247
// TODO: Support for change detection (via file watching)
245-
let settings = self.settings(for: uri, language)
248+
var settings: FileBuildSettings? = nil
249+
do {
250+
settings = try self.settings(for: uri, language)
251+
} catch {
252+
log("error computing settings: \(error)")
253+
}
246254
DispatchQueue.global().async {
247-
delegate.fileBuildSettingsChanged([uri: FileBuildSettingsChange(settings)])
255+
if let settings = settings {
256+
delegate.fileBuildSettingsChanged([uri: FileBuildSettingsChange(settings)])
257+
} else {
258+
delegate.fileBuildSettingsChanged([uri: .removedOrUnavailable])
259+
}
248260
}
249261
}
250262

@@ -291,15 +303,15 @@ extension SwiftPMWorkspace {
291303
public func settings(
292304
for path: AbsolutePath,
293305
_ language: Language,
294-
_ td: TargetBuildDescription) -> FileBuildSettings?
306+
_ td: TargetBuildDescription) throws -> FileBuildSettings?
295307
{
296308
switch (td, language) {
297309
case (.swift(let td), .swift):
298-
return settings(forSwiftFile: path, td)
310+
return try settings(forSwiftFile: path, td)
299311
case (.clang, .swift):
300312
return nil
301313
case (.clang(let td), _):
302-
return settings(forClangFile: path, language, td)
314+
return try settings(forClangFile: path, language, td)
303315
default:
304316
return nil
305317
}
@@ -314,8 +326,8 @@ extension SwiftPMWorkspace {
314326
}
315327
return nil
316328
}
317-
318-
if let result = impl(path) {
329+
330+
if let result = impl(path) {
319331
return result
320332
}
321333

@@ -324,30 +336,30 @@ extension SwiftPMWorkspace {
324336
}
325337

326338
/// Retrieve settings for a given header file.
327-
public func settings(forHeader path: AbsolutePath, _ language: Language) -> FileBuildSettings? {
328-
func impl(_ path: AbsolutePath) -> FileBuildSettings? {
339+
public func settings(forHeader path: AbsolutePath, _ language: Language) throws -> FileBuildSettings? {
340+
func impl(_ path: AbsolutePath) throws -> FileBuildSettings? {
329341
var dir = path.parentDirectory
330342
while !dir.isRoot {
331343
if let td = sourceDirToTarget[dir] {
332-
return settings(for: path, language, td)
344+
return try settings(for: path, language, td)
333345
}
334346
dir = dir.parentDirectory
335347
}
336348
return nil
337349
}
338-
339-
if let result = impl(path) {
350+
351+
if let result = try impl(path) {
340352
return result
341353
}
342354

343355
let canonicalPath = resolveSymlinks(path)
344-
return canonicalPath == path ? nil : impl(canonicalPath)
356+
return try canonicalPath == path ? nil : impl(canonicalPath)
345357
}
346358

347359
/// Retrieve settings for the given swift file, which is part of a known target build description.
348360
public func settings(
349361
forSwiftFile path: AbsolutePath,
350-
_ td: SwiftTargetBuildDescription) -> FileBuildSettings?
362+
_ td: SwiftTargetBuildDescription) throws -> FileBuildSettings?
351363
{
352364
// FIXME: this is re-implementing llbuild's constructCommandLineArgs.
353365
var args: [String] = [
@@ -366,7 +378,7 @@ extension SwiftPMWorkspace {
366378
args += ["-c"]
367379
args += td.sources.map { $0.pathString }
368380
args += ["-I", buildPath.pathString]
369-
args += td.compileArguments()
381+
args += try td.compileArguments()
370382

371383
return FileBuildSettings(
372384
compilerArguments: args,
@@ -380,11 +392,11 @@ extension SwiftPMWorkspace {
380392
public func settings(
381393
forClangFile path: AbsolutePath,
382394
_ language: Language,
383-
_ td: ClangTargetBuildDescription) -> FileBuildSettings?
395+
_ td: ClangTargetBuildDescription) throws -> FileBuildSettings?
384396
{
385397
// FIXME: this is re-implementing things from swiftpm's createClangCompileTarget
386398

387-
var args = td.basicArguments()
399+
var args = try td.basicArguments()
388400

389401
let nativePath: AbsolutePath =
390402
URL(fileURLWithPath: path.pathString).withUnsafeFileSystemRepresentation {
@@ -454,12 +466,13 @@ private func findPackageDirectory(
454466
return path
455467
}
456468

457-
extension TSCBasic.Diagnostic.Behavior {
469+
extension Basics.Diagnostic.Severity {
458470
var asLogLevel: LogLevel {
459471
switch self {
460472
case .error: return .error
461473
case .warning: return .warning
462-
default: return .info
474+
case .debug: return .debug
475+
case .info: return .info
463476
}
464477
}
465478
}

Tests/SKSwiftPMWorkspaceTests/SwiftPMWorkspaceTests.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
110110

111111
XCTAssertEqual(ws.buildPath, build)
112112
XCTAssertNotNil(ws.indexStorePath)
113-
let arguments = ws.settings(for: aswift.asURI, .swift)!.compilerArguments
113+
let arguments = try ws.settings(for: aswift.asURI, .swift)!.compilerArguments
114114

115115
check(
116116
"-module-name", "lib", "-incremental", "-emit-dependencies",
@@ -165,7 +165,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
165165
let build = buildPath(root: packageRoot, config: config, triple: hostTriple)
166166

167167
XCTAssertEqual(ws.buildPath, build)
168-
let arguments = ws.settings(for: aswift.asURI, .swift)!.compilerArguments
168+
let arguments = try ws.settings(for: aswift.asURI, .swift)!.compilerArguments
169169

170170
check("-typecheck", arguments: arguments)
171171
check("-Xcc", "-m32", arguments: arguments)
@@ -195,7 +195,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
195195
buildSetup: TestSourceKitServer.serverOptions.buildSetup)
196196

197197
let source = resolveSymlinks(packageRoot.appending(component: "Package.swift"))
198-
let arguments = ws.settings(for: source.asURI, .swift)!.compilerArguments
198+
let arguments = try ws.settings(for: source.asURI, .swift)!.compilerArguments
199199

200200
check("-swift-version", "4.2", arguments: arguments)
201201
check(source.pathString, arguments: arguments)
@@ -227,10 +227,10 @@ final class SwiftPMWorkspaceTests: XCTestCase {
227227
let aswift = packageRoot.appending(components: "Sources", "lib", "a.swift")
228228
let bswift = packageRoot.appending(components: "Sources", "lib", "b.swift")
229229

230-
let argumentsA = ws.settings(for: aswift.asURI, .swift)!.compilerArguments
230+
let argumentsA = try ws.settings(for: aswift.asURI, .swift)!.compilerArguments
231231
check(aswift.pathString, arguments: argumentsA)
232232
check(bswift.pathString, arguments: argumentsA)
233-
let argumentsB = ws.settings(for: aswift.asURI, .swift)!.compilerArguments
233+
let argumentsB = try ws.settings(for: aswift.asURI, .swift)!.compilerArguments
234234
check(aswift.pathString, arguments: argumentsB)
235235
check(bswift.pathString, arguments: argumentsB)
236236
}
@@ -266,7 +266,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
266266

267267
let aswift = packageRoot.appending(components: "Sources", "libA", "a.swift")
268268
let bswift = packageRoot.appending(components: "Sources", "libB", "b.swift")
269-
let arguments = ws.settings(for: aswift.asURI, .swift)!.compilerArguments
269+
let arguments = try ws.settings(for: aswift.asURI, .swift)!.compilerArguments
270270
check(aswift.pathString, arguments: arguments)
271271
checkNot(bswift.pathString, arguments: arguments)
272272
// Temporary conditional to work around revlock between SourceKit-LSP and SwiftPM
@@ -282,7 +282,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
282282
arguments: arguments)
283283
}
284284

285-
let argumentsB = ws.settings(for: bswift.asURI, .swift)!.compilerArguments
285+
let argumentsB = try ws.settings(for: bswift.asURI, .swift)!.compilerArguments
286286
check(bswift.pathString, arguments: argumentsB)
287287
checkNot(aswift.pathString, arguments: argumentsB)
288288
checkNot("-I", packageRoot.appending(components: "Sources", "libC", "include").pathString,
@@ -316,9 +316,9 @@ final class SwiftPMWorkspaceTests: XCTestCase {
316316

317317
let aswift = packageRoot.appending(components: "Sources", "libA", "a.swift")
318318
let bswift = packageRoot.appending(components: "Sources", "libB", "b.swift")
319-
XCTAssertNotNil(ws.settings(for: aswift.asURI, .swift))
320-
XCTAssertNil(ws.settings(for: bswift.asURI, .swift))
321-
XCTAssertNil(ws.settings(for: DocumentURI(URL(string: "https://www.apple.com")!), .swift))
319+
XCTAssertNotNil(try ws.settings(for: aswift.asURI, .swift))
320+
XCTAssertNil(try ws.settings(for: bswift.asURI, .swift))
321+
XCTAssertNil(try ws.settings(for: DocumentURI(URL(string: "https://www.apple.com")!), .swift))
322322
}
323323
}
324324

@@ -375,7 +375,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
375375
checkNot(bcxx.pathString, arguments: arguments)
376376
}
377377

378-
let args = ws.settings(for: acxx.asURI, .cpp)!.compilerArguments
378+
let args = try ws.settings(for: acxx.asURI, .cpp)!.compilerArguments
379379
checkArgsCommon(args)
380380

381381
URL(fileURLWithPath: build.appending(components: "lib.build", "a.cpp.d").pathString)
@@ -393,7 +393,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
393393
}
394394

395395
let header = packageRoot.appending(components: "Sources", "lib", "include", "a.h")
396-
let headerArgs = ws.settings(for: header.asURI, .cpp)!.compilerArguments
396+
let headerArgs = try ws.settings(for: header.asURI, .cpp)!.compilerArguments
397397
checkArgsCommon(headerArgs)
398398

399399
check("-c", "-x", "c++-header", URL(fileURLWithPath: header.pathString).path,
@@ -424,7 +424,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
424424
buildSetup: TestSourceKitServer.serverOptions.buildSetup)
425425

426426
let aswift = packageRoot.appending(components: "Sources", "lib", "a.swift")
427-
let arguments = ws.settings(for: aswift.asURI, .swift)!.compilerArguments
427+
let arguments = try ws.settings(for: aswift.asURI, .swift)!.compilerArguments
428428
check("-target", arguments: arguments) // Only one!
429429
let hostTriple = ws.buildParameters.triple
430430

@@ -469,16 +469,16 @@ final class SwiftPMWorkspaceTests: XCTestCase {
469469
.appending(components: "Sources", "lib", "a.swift")
470470
let manifest = packageRoot.appending(components: "Package.swift")
471471

472-
let arguments1 = ws.settings(for: aswift1.asURI, .swift)?.compilerArguments
473-
let arguments2 = ws.settings(for: aswift2.asURI, .swift)?.compilerArguments
472+
let arguments1 = try ws.settings(for: aswift1.asURI, .swift)?.compilerArguments
473+
let arguments2 = try ws.settings(for: aswift2.asURI, .swift)?.compilerArguments
474474
XCTAssertNotNil(arguments1)
475475
XCTAssertNotNil(arguments2)
476476
XCTAssertEqual(arguments1, arguments2)
477477

478478
checkNot(aswift1.pathString, arguments: arguments1 ?? [])
479479
check(resolveSymlinks(aswift1).pathString, arguments: arguments1 ?? [])
480480

481-
let argsManifest = ws.settings(for: manifest.asURI, .swift)?.compilerArguments
481+
let argsManifest = try ws.settings(for: manifest.asURI, .swift)?.compilerArguments
482482
XCTAssertNotNil(argsManifest)
483483

484484
checkNot(manifest.pathString, arguments: argsManifest ?? [])
@@ -519,12 +519,12 @@ final class SwiftPMWorkspaceTests: XCTestCase {
519519
let acxx = packageRoot.appending(components: "Sources", "lib", "a.cpp")
520520
let ah = packageRoot.appending(components: "Sources", "lib", "include", "a.h")
521521

522-
let argsCxx = ws.settings(for: acxx.asURI, .cpp)?.compilerArguments
522+
let argsCxx = try ws.settings(for: acxx.asURI, .cpp)?.compilerArguments
523523
XCTAssertNotNil(argsCxx)
524524
check(acxx.pathString, arguments: argsCxx ?? [])
525525
checkNot(resolveSymlinks(acxx).pathString, arguments: argsCxx ?? [])
526526

527-
let argsH = ws.settings(for: ah.asURI, .cpp)?.compilerArguments
527+
let argsH = try ws.settings(for: ah.asURI, .cpp)?.compilerArguments
528528
XCTAssertNotNil(argsH)
529529
checkNot(ah.pathString, arguments: argsH ?? [])
530530
check(resolveSymlinks(ah).pathString, arguments: argsH ?? [])
@@ -558,7 +558,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
558558
buildSetup: TestSourceKitServer.serverOptions.buildSetup)
559559

560560
let aswift = packageRoot.appending(components: "Sources", "lib", "a.swift")
561-
let arguments = ws.settings(for: aswift.asURI, .swift)!.compilerArguments
561+
let arguments = try ws.settings(for: aswift.asURI, .swift)!.compilerArguments
562562
check(aswift.pathString, arguments: arguments)
563563
XCTAssertNotNil(arguments.firstIndex(where: {
564564
$0.hasSuffix(".swift") && $0.contains("DerivedSources")

0 commit comments

Comments
 (0)