Skip to content

Commit 43c072d

Browse files
committed
Adopt to path API changes
We are moving to a better model for TSC's path APIs in swiftlang/swift-tools-support-core#353. The previous API is still available (but deprecated) as much as possible, but since SourceKit-LSP was using `resolveSymlinks` (which is now throwing) quite a bit, there are some changes necessary.
1 parent 9597081 commit 43c072d

File tree

10 files changed

+81
-80
lines changed

10 files changed

+81
-80
lines changed

Sources/LanguageServerProtocol/SupportTypes/DocumentURI.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public struct DocumentURI: Codable, Hashable {
2020
private let storage: URL
2121

2222
public var nativeURI: Self {
23-
DocumentURI(URL(fileURLWithPath: resolveSymlinks(AbsolutePath(self.pseudoPath)).pathString))
23+
get throws {
24+
DocumentURI(URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(validating: self.pseudoPath)).pathString))
25+
}
2426
}
2527

2628
public var fileURL: URL? {

Sources/SKCore/BuildServerBuildSystem.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,7 @@ extension BuildServerBuildSystem: BuildSystem {
275275
return .handled
276276
}
277277

278-
let realpath = resolveSymlinks(path)
279-
if realpath != path, projectRoot.isAncestorOfOrEqual(to: realpath) {
278+
if let realpath = try? resolveSymlinks(path), realpath != path, projectRoot.isAncestorOfOrEqual(to: realpath) {
280279
return .handled
281280
}
282281

Sources/SKCore/CompilationDatabase.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public struct JSONCompilationDatabase: CompilationDatabase, Equatable {
143143
var commands: [Command] = []
144144

145145
public init(_ commands: [Command] = []) {
146-
commands.forEach { add($0) }
146+
commands.forEach { try! add($0) }
147147
}
148148

149149
public subscript(_ url: URL) -> [Command] {
@@ -158,11 +158,11 @@ public struct JSONCompilationDatabase: CompilationDatabase, Equatable {
158158

159159
public var allCommands: AnySequence<Command> { AnySequence(commands) }
160160

161-
public mutating func add(_ command: Command) {
161+
public mutating func add(_ command: Command) throws {
162162
let url = command.url
163163
pathToCommands[url, default: []].append(commands.count)
164164

165-
let canonical = URL(fileURLWithPath: resolveSymlinks(AbsolutePath(url.path)).pathString)
165+
let canonical = URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(url.path)).pathString)
166166
if canonical != url {
167167
pathToCommands[canonical, default: []].append(commands.count)
168168
}
@@ -175,7 +175,7 @@ extension JSONCompilationDatabase: Codable {
175175
public init(from decoder: Decoder) throws {
176176
var container = try decoder.unkeyedContainer()
177177
while !container.isAtEnd {
178-
self.add(try container.decode(Command.self))
178+
try self.add(try container.decode(Command.self))
179179
}
180180
}
181181

Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public final class SwiftPMWorkspace {
9696
throw Error.noManifest(workspacePath: workspacePath)
9797
}
9898

99-
self.packageRoot = resolveSymlinks(packageRoot)
99+
self.packageRoot = try resolveSymlinks(packageRoot)
100100

101101
guard let destinationToolchainBinDir = toolchainRegistry.default?.swiftc?.parentDirectory else {
102102
throw Error.cannotDetermineHostToolchain
@@ -105,7 +105,7 @@ public final class SwiftPMWorkspace {
105105
let destination = try Destination.hostDestination(destinationToolchainBinDir)
106106
let toolchain = try UserToolchain(destination: destination)
107107

108-
var location = Workspace.Location(forRootPackage: packageRoot, fileSystem: fileSystem)
108+
var location = try Workspace.Location(forRootPackage: packageRoot, fileSystem: fileSystem)
109109
if let scratchDirectory = buildSetup.path {
110110
location.scratchDirectory = scratchDirectory
111111
}
@@ -272,12 +272,12 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
272272
return nil
273273
}
274274

275-
if let td = targetDescription(for: path) {
275+
if let td = try targetDescription(for: path) {
276276
return try settings(for: path, language, td)
277277
}
278278

279279
if path.basename == "Package.swift" {
280-
return settings(forPackageManifest: path)
280+
return try settings(forPackageManifest: path)
281281
}
282282

283283
if path.extension == "h" {
@@ -330,13 +330,13 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
330330

331331
/// Returns the resolved target description for the given file, if one is known.
332332
/// Must only be called on `queue`.
333-
private func targetDescription(for file: AbsolutePath) -> TargetBuildDescription? {
333+
private func targetDescription(for file: AbsolutePath) throws -> TargetBuildDescription? {
334334
dispatchPrecondition(condition: .onQueue(queue))
335335
if let td = fileToTarget[file] {
336336
return td
337337
}
338338

339-
let realpath = resolveSymlinks(file)
339+
let realpath = try resolveSymlinks(file)
340340
if realpath != file, let td = fileToTarget[realpath] {
341341
fileToTarget[file] = td
342342
return td
@@ -376,7 +376,7 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
376376
return .unhandled
377377
}
378378
return self.queue.sync {
379-
if targetDescription(for: AbsolutePath(fileUrl.path)) != nil {
379+
if (try? targetDescription(for: AbsolutePath(fileUrl.path))) != nil {
380380
return .handled
381381
} else {
382382
return .unhandled
@@ -409,7 +409,7 @@ extension SwiftPMWorkspace {
409409

410410
/// Retrieve settings for a package manifest (Package.swift).
411411
/// Must only be called on `queue`.
412-
private func settings(forPackageManifest path: AbsolutePath) -> FileBuildSettings? {
412+
private func settings(forPackageManifest path: AbsolutePath) throws -> FileBuildSettings? {
413413
dispatchPrecondition(condition: .onQueue(queue))
414414
func impl(_ path: AbsolutePath) -> FileBuildSettings? {
415415
for package in packageGraph.packages where path == package.manifest.path {
@@ -423,7 +423,7 @@ extension SwiftPMWorkspace {
423423
return result
424424
}
425425

426-
let canonicalPath = resolveSymlinks(path)
426+
let canonicalPath = try resolveSymlinks(path)
427427
return canonicalPath == path ? nil : impl(canonicalPath)
428428
}
429429

@@ -446,7 +446,7 @@ extension SwiftPMWorkspace {
446446
return result
447447
}
448448

449-
let canonicalPath = resolveSymlinks(path)
449+
let canonicalPath = try resolveSymlinks(path)
450450
return try canonicalPath == path ? nil : impl(canonicalPath)
451451
}
452452

@@ -496,7 +496,7 @@ extension SwiftPMWorkspace {
496496
URL(fileURLWithPath: path.pathString).withUnsafeFileSystemRepresentation {
497497
AbsolutePath(String(cString: $0!))
498498
}
499-
let compilePath = td.compilePaths().first(where: { $0.source == nativePath })
499+
let compilePath = try td.compilePaths().first(where: { $0.source == nativePath })
500500
if let compilePath = compilePath {
501501
args += [
502502
"-MD",

Sources/SKTestSupport/SKSwiftPMTestWorkspace.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public final class SKSwiftPMTestWorkspace {
5555
public init(projectDir: URL, tmpDir: URL, toolchain: Toolchain, testServer: TestSourceKitServer? = nil) throws {
5656
self.testServer = testServer ?? TestSourceKitServer(connectionKind: .local)
5757

58-
self.projectDir = URL(fileURLWithPath: resolveSymlinks(AbsolutePath(projectDir.path)).pathString)
59-
self.tmpDir = URL(fileURLWithPath: resolveSymlinks(AbsolutePath(tmpDir.path)).pathString)
58+
self.projectDir = URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(projectDir.path)).pathString)
59+
self.tmpDir = URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(tmpDir.path)).pathString)
6060
self.toolchain = toolchain
6161

6262
let fm = FileManager.default

Tests/SKSwiftPMWorkspaceTests/SwiftPMWorkspaceTests.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
9797
targets: [.target(name: "lib", dependencies: [])])
9898
"""
9999
])
100-
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
100+
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
101101
let tr = ToolchainRegistry.shared
102102
let ws = try! SwiftPMWorkspace(
103103
workspacePath: packageRoot,
@@ -195,7 +195,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
195195
fileSystem: fs,
196196
buildSetup: TestSourceKitServer.serverOptions.buildSetup)
197197

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

201201
check("-swift-version", "4.2", arguments: arguments)
@@ -217,7 +217,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
217217
targets: [.target(name: "lib", dependencies: [])])
218218
"""
219219
])
220-
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
220+
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
221221
let tr = ToolchainRegistry.shared
222222
let ws = try! SwiftPMWorkspace(
223223
workspacePath: packageRoot,
@@ -257,7 +257,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
257257
])
258258
"""
259259
])
260-
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
260+
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
261261
let tr = ToolchainRegistry.shared
262262
let ws = try! SwiftPMWorkspace(
263263
workspacePath: packageRoot,
@@ -339,7 +339,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
339339
cxxLanguageStandard: .cxx14)
340340
"""
341341
])
342-
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
342+
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
343343
let tr = ToolchainRegistry.shared
344344
let ws = try! SwiftPMWorkspace(
345345
workspacePath: packageRoot,
@@ -477,13 +477,13 @@ final class SwiftPMWorkspaceTests: XCTestCase {
477477
XCTAssertEqual(arguments1, arguments2)
478478

479479
checkNot(aswift1.pathString, arguments: arguments1 ?? [])
480-
check(resolveSymlinks(aswift1).pathString, arguments: arguments1 ?? [])
480+
check(try resolveSymlinks(aswift1).pathString, arguments: arguments1 ?? [])
481481

482482
let argsManifest = try ws._settings(for: manifest.asURI, .swift)?.compilerArguments
483483
XCTAssertNotNil(argsManifest)
484484

485485
checkNot(manifest.pathString, arguments: argsManifest ?? [])
486-
check(resolveSymlinks(manifest).pathString, arguments: argsManifest ?? [])
486+
check(try resolveSymlinks(manifest).pathString, arguments: argsManifest ?? [])
487487
}
488488
}
489489

@@ -523,12 +523,12 @@ final class SwiftPMWorkspaceTests: XCTestCase {
523523
let argsCxx = try ws._settings(for: acxx.asURI, .cpp)?.compilerArguments
524524
XCTAssertNotNil(argsCxx)
525525
check(acxx.pathString, arguments: argsCxx ?? [])
526-
checkNot(resolveSymlinks(acxx).pathString, arguments: argsCxx ?? [])
526+
checkNot(try resolveSymlinks(acxx).pathString, arguments: argsCxx ?? [])
527527

528528
let argsH = try ws._settings(for: ah.asURI, .cpp)?.compilerArguments
529529
XCTAssertNotNil(argsH)
530530
checkNot(ah.pathString, arguments: argsH ?? [])
531-
check(resolveSymlinks(ah).pathString, arguments: argsH ?? [])
531+
check(try resolveSymlinks(ah).pathString, arguments: argsH ?? [])
532532
}
533533
}
534534

@@ -550,7 +550,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
550550
resources: [.copy("a.txt")])])
551551
"""
552552
])
553-
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
553+
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
554554
let tr = ToolchainRegistry.shared
555555
let ws = try! SwiftPMWorkspace(
556556
workspacePath: packageRoot,
@@ -579,15 +579,15 @@ final class SwiftPMWorkspaceTests: XCTestCase {
579579
targets: [.target(name: "lib", dependencies: [])])
580580
"""
581581
])
582-
let packageRoot = resolveSymlinks(tempDir.appending(components: "pkg", "Sources", "lib"))
582+
let packageRoot = try resolveSymlinks(tempDir.appending(components: "pkg", "Sources", "lib"))
583583
let tr = ToolchainRegistry.shared
584584
let ws = try! SwiftPMWorkspace(
585585
workspacePath: packageRoot,
586586
toolchainRegistry: tr,
587587
fileSystem: fs,
588588
buildSetup: TestSourceKitServer.serverOptions.buildSetup)
589589

590-
XCTAssertEqual(ws._packageRoot, resolveSymlinks(tempDir.appending(component: "pkg")))
590+
XCTAssertEqual(ws._packageRoot, try resolveSymlinks(tempDir.appending(component: "pkg")))
591591
}
592592
}
593593
}

Tests/SourceKitLSPTests/CallHierarchyTests.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ final class CallHierarchyTests: XCTestCase {
7474
Location(badUTF16: ws.testLoc(name))
7575
}
7676

77-
func item(_ name: String, _ kind: SymbolKind, detail: String = "main", usr: String, at locName: String) -> CallHierarchyItem {
77+
func item(_ name: String, _ kind: SymbolKind, detail: String = "main", usr: String, at locName: String) throws -> CallHierarchyItem {
7878
let location = loc(locName)
7979
return CallHierarchyItem(
8080
name: name,
8181
kind: kind,
8282
tags: nil,
8383
detail: detail,
84-
uri: location.uri.nativeURI,
84+
uri: try location.uri.nativeURI,
8585
range: location.range,
8686
selectionRange: location.range,
8787
data: .dictionary([
8888
"usr": .string(usr),
89-
"uri": .string(location.uri.nativeURI.stringValue)
89+
"uri": .string(try location.uri.nativeURI.stringValue)
9090
])
9191
)
9292
}
@@ -114,27 +114,27 @@ final class CallHierarchyTests: XCTestCase {
114114

115115
XCTAssertEqual(try outgoingCalls(at: testLoc("a")), [])
116116
XCTAssertEqual(try outgoingCalls(at: testLoc("b")), [
117-
outCall(item("a()", .function, usr: aUsr, at: "a"), at: "b->a"),
118-
outCall(item("c()", .function, usr: cUsr, at: "c"), at: "b->c"),
119-
outCall(item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->b"),
117+
outCall(try item("a()", .function, usr: aUsr, at: "a"), at: "b->a"),
118+
outCall(try item("c()", .function, usr: cUsr, at: "c"), at: "b->c"),
119+
outCall(try item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->b"),
120120
])
121121
XCTAssertEqual(try outgoingCalls(at: testLoc("c")), [
122-
outCall(item("a()", .function, usr: aUsr, at: "a"), at: "c->a"),
123-
outCall(item("d()", .function, usr: dUsr, at: "d"), at: "c->d"),
124-
outCall(item("c()", .function, usr: cUsr, at: "c"), at: "c->c"),
122+
outCall(try item("a()", .function, usr: aUsr, at: "a"), at: "c->a"),
123+
outCall(try item("d()", .function, usr: dUsr, at: "d"), at: "c->d"),
124+
outCall(try item("c()", .function, usr: cUsr, at: "c"), at: "c->c"),
125125
])
126126

127127
// Test incoming call hierarchy
128128

129129
XCTAssertEqual(try incomingCalls(at: testLoc("a")), [
130-
inCall(item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->a"),
131-
inCall(item("c()", .function, usr: cUsr, at: "c"), at: "c->a"),
130+
inCall(try item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->a"),
131+
inCall(try item("c()", .function, usr: cUsr, at: "c"), at: "c->a"),
132132
])
133133
XCTAssertEqual(try incomingCalls(at: testLoc("b")), [
134-
inCall(item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->b"),
134+
inCall(try item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->b"),
135135
])
136136
XCTAssertEqual(try incomingCalls(at: testLoc("d")), [
137-
inCall(item("c()", .function, usr: cUsr, at: "c"), at: "c->d"),
137+
inCall(try item("c()", .function, usr: cUsr, at: "c"), at: "c->d"),
138138
])
139139
}
140140
}

Tests/SourceKitLSPTests/ImplementationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ final class ImplementationTests: XCTestCase {
3636
func testLoc(_ name: String) -> TestLocation {
3737
ws.testLoc(name)
3838
}
39-
func loc(_ name: String) -> Location {
39+
func loc(_ name: String) throws -> Location {
4040
let location: TestLocation = ws.testLoc(name)
41-
return Location(badUTF16: TestLocation(url: location.docUri.nativeURI.fileURL!, line: location.line, utf8Column: location.utf8Column, utf16Column: location.utf16Column))
41+
return Location(badUTF16: TestLocation(url: try location.docUri.nativeURI.fileURL!, line: location.line, utf8Column: location.utf8Column, utf16Column: location.utf16Column))
4242
}
4343

4444
try XCTAssertEqual(impls(at: testLoc("Protocol")), [loc("StructConformance")])

Tests/SourceKitLSPTests/SourceKitTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ final class SKTests: XCTestCase {
8282
}
8383

8484
XCTAssertEqual(jump.count, 1)
85-
XCTAssertEqual(jump.first?.uri, locDef.docUri.nativeURI)
85+
XCTAssertEqual(jump.first?.uri, try locDef.docUri.nativeURI)
8686
XCTAssertEqual(jump.first?.range.lowerBound, locDef.position)
8787

8888
// MARK: Find references
@@ -94,9 +94,9 @@ final class SKTests: XCTestCase {
9494

9595
let call = ws.testLoc("aaa:call")
9696
XCTAssertEqual(Set(refs), [
97-
Location(TestLocation(url: URL(fileURLWithPath: resolveSymlinks(AbsolutePath(locDef.url.path)).pathString), line: locDef.line, utf8Column: locDef.utf8Column, utf16Column: locDef.utf16Column)),
98-
Location(TestLocation(url: URL(fileURLWithPath: resolveSymlinks(AbsolutePath(locRef.url.path)).pathString), line: locRef.line, utf8Column: locRef.utf8Column, utf16Column: locRef.utf16Column)),
99-
Location(TestLocation(url: URL(fileURLWithPath: resolveSymlinks(AbsolutePath(call.url.path)).pathString), line: call.line, utf8Column: call.utf8Column, utf16Column: call.utf16Column)),
97+
Location(TestLocation(url: URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(locDef.url.path)).pathString), line: locDef.line, utf8Column: locDef.utf8Column, utf16Column: locDef.utf16Column)),
98+
Location(TestLocation(url: URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(locRef.url.path)).pathString), line: locRef.line, utf8Column: locRef.utf8Column, utf16Column: locRef.utf16Column)),
99+
Location(TestLocation(url: URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(call.url.path)).pathString), line: call.line, utf8Column: call.utf8Column, utf16Column: call.utf16Column)),
100100
])
101101
}
102102

@@ -131,7 +131,7 @@ final class SKTests: XCTestCase {
131131
return nil
132132
}
133133
XCTAssertEqual(jump.count, 1)
134-
XCTAssertEqual(jump.first?.uri, locDef.docUri.nativeURI)
134+
XCTAssertEqual(jump.first?.uri, try locDef.docUri.nativeURI)
135135
XCTAssertEqual(jump.first?.range.lowerBound, locDef.position)
136136

137137
let tmpContents = try listdir(tmpDir)
@@ -334,7 +334,7 @@ final class SKTests: XCTestCase {
334334
guard ToolchainRegistry.shared.default?.clangd != nil else { return }
335335

336336
let refLoc = ws.testLoc("Object:ref:main")
337-
let expectedDoc = ws.testLoc("Object").docIdentifier.uri.nativeURI
337+
let expectedDoc = try ws.testLoc("Object").docIdentifier.uri.nativeURI
338338
let refPos = Position(line: refLoc.position.line, utf16index: refLoc.utf16Column + 2)
339339

340340
try ws.openDocument(refLoc.url, language: .c)

0 commit comments

Comments
 (0)