Skip to content

Adopt to path API changes #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public struct DocumentURI: Codable, Hashable {
private let storage: URL

public var nativeURI: Self {
DocumentURI(URL(fileURLWithPath: resolveSymlinks(AbsolutePath(self.pseudoPath)).pathString))
get throws {
DocumentURI(URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(validating: self.pseudoPath)).pathString))
}
}

public var fileURL: URL? {
Expand Down
3 changes: 1 addition & 2 deletions Sources/SKCore/BuildServerBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,7 @@ extension BuildServerBuildSystem: BuildSystem {
return .handled
}

let realpath = resolveSymlinks(path)
if realpath != path, projectRoot.isAncestorOfOrEqual(to: realpath) {
if let realpath = try? resolveSymlinks(path), realpath != path, projectRoot.isAncestorOfOrEqual(to: realpath) {
return .handled
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/SKCore/CompilationDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public struct JSONCompilationDatabase: CompilationDatabase, Equatable {
var commands: [Command] = []

public init(_ commands: [Command] = []) {
commands.forEach { add($0) }
commands.forEach { try! add($0) }
}

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

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

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

let canonical = URL(fileURLWithPath: resolveSymlinks(AbsolutePath(url.path)).pathString)
let canonical = URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(url.path)).pathString)
if canonical != url {
pathToCommands[canonical, default: []].append(commands.count)
}
Expand All @@ -175,7 +175,7 @@ extension JSONCompilationDatabase: Codable {
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
self.add(try container.decode(Command.self))
try self.add(try container.decode(Command.self))
}
}

Expand Down
22 changes: 11 additions & 11 deletions Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public final class SwiftPMWorkspace {
throw Error.noManifest(workspacePath: workspacePath)
}

self.packageRoot = resolveSymlinks(packageRoot)
self.packageRoot = try resolveSymlinks(packageRoot)

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

var location = Workspace.Location(forRootPackage: packageRoot, fileSystem: fileSystem)
var location = try Workspace.Location(forRootPackage: packageRoot, fileSystem: fileSystem)
if let scratchDirectory = buildSetup.path {
location.scratchDirectory = scratchDirectory
}
Expand Down Expand Up @@ -272,12 +272,12 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
return nil
}

if let td = targetDescription(for: path) {
if let td = try targetDescription(for: path) {
return try settings(for: path, language, td)
}

if path.basename == "Package.swift" {
return settings(forPackageManifest: path)
return try settings(forPackageManifest: path)
}

if path.extension == "h" {
Expand Down Expand Up @@ -330,13 +330,13 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {

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

let realpath = resolveSymlinks(file)
let realpath = try resolveSymlinks(file)
if realpath != file, let td = fileToTarget[realpath] {
fileToTarget[file] = td
return td
Expand Down Expand Up @@ -376,7 +376,7 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
return .unhandled
}
return self.queue.sync {
if targetDescription(for: AbsolutePath(fileUrl.path)) != nil {
if (try? targetDescription(for: AbsolutePath(fileUrl.path))) != nil {
return .handled
} else {
return .unhandled
Expand Down Expand Up @@ -409,7 +409,7 @@ extension SwiftPMWorkspace {

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

let canonicalPath = resolveSymlinks(path)
let canonicalPath = try resolveSymlinks(path)
return canonicalPath == path ? nil : impl(canonicalPath)
}

Expand All @@ -446,7 +446,7 @@ extension SwiftPMWorkspace {
return result
}

let canonicalPath = resolveSymlinks(path)
let canonicalPath = try resolveSymlinks(path)
return try canonicalPath == path ? nil : impl(canonicalPath)
}

Expand Down Expand Up @@ -496,7 +496,7 @@ extension SwiftPMWorkspace {
URL(fileURLWithPath: path.pathString).withUnsafeFileSystemRepresentation {
AbsolutePath(String(cString: $0!))
}
let compilePath = td.compilePaths().first(where: { $0.source == nativePath })
let compilePath = try td.compilePaths().first(where: { $0.source == nativePath })
if let compilePath = compilePath {
args += [
"-MD",
Expand Down
4 changes: 2 additions & 2 deletions Sources/SKTestSupport/SKSwiftPMTestWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public final class SKSwiftPMTestWorkspace {
public init(projectDir: URL, tmpDir: URL, toolchain: Toolchain, testServer: TestSourceKitServer? = nil) throws {
self.testServer = testServer ?? TestSourceKitServer(connectionKind: .local)

self.projectDir = URL(fileURLWithPath: resolveSymlinks(AbsolutePath(projectDir.path)).pathString)
self.tmpDir = URL(fileURLWithPath: resolveSymlinks(AbsolutePath(tmpDir.path)).pathString)
self.projectDir = URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(projectDir.path)).pathString)
self.tmpDir = URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(tmpDir.path)).pathString)
self.toolchain = toolchain

let fm = FileManager.default
Expand Down
24 changes: 12 additions & 12 deletions Tests/SKSwiftPMWorkspaceTests/SwiftPMWorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
targets: [.target(name: "lib", dependencies: [])])
"""
])
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
let tr = ToolchainRegistry.shared
let ws = try! SwiftPMWorkspace(
workspacePath: packageRoot,
Expand Down Expand Up @@ -195,7 +195,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
fileSystem: fs,
buildSetup: TestSourceKitServer.serverOptions.buildSetup)

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

check("-swift-version", "4.2", arguments: arguments)
Expand All @@ -217,7 +217,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
targets: [.target(name: "lib", dependencies: [])])
"""
])
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
let tr = ToolchainRegistry.shared
let ws = try! SwiftPMWorkspace(
workspacePath: packageRoot,
Expand Down Expand Up @@ -257,7 +257,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
])
"""
])
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
let tr = ToolchainRegistry.shared
let ws = try! SwiftPMWorkspace(
workspacePath: packageRoot,
Expand Down Expand Up @@ -339,7 +339,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
cxxLanguageStandard: .cxx14)
"""
])
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
let tr = ToolchainRegistry.shared
let ws = try! SwiftPMWorkspace(
workspacePath: packageRoot,
Expand Down Expand Up @@ -477,13 +477,13 @@ final class SwiftPMWorkspaceTests: XCTestCase {
XCTAssertEqual(arguments1, arguments2)

checkNot(aswift1.pathString, arguments: arguments1 ?? [])
check(resolveSymlinks(aswift1).pathString, arguments: arguments1 ?? [])
check(try resolveSymlinks(aswift1).pathString, arguments: arguments1 ?? [])

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

checkNot(manifest.pathString, arguments: argsManifest ?? [])
check(resolveSymlinks(manifest).pathString, arguments: argsManifest ?? [])
check(try resolveSymlinks(manifest).pathString, arguments: argsManifest ?? [])
}
}

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

let argsH = try ws._settings(for: ah.asURI, .cpp)?.compilerArguments
XCTAssertNotNil(argsH)
checkNot(ah.pathString, arguments: argsH ?? [])
check(resolveSymlinks(ah).pathString, arguments: argsH ?? [])
check(try resolveSymlinks(ah).pathString, arguments: argsH ?? [])
}
}

Expand All @@ -550,7 +550,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
resources: [.copy("a.txt")])])
"""
])
let packageRoot = resolveSymlinks(tempDir.appending(component: "pkg"))
let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
let tr = ToolchainRegistry.shared
let ws = try! SwiftPMWorkspace(
workspacePath: packageRoot,
Expand Down Expand Up @@ -579,15 +579,15 @@ final class SwiftPMWorkspaceTests: XCTestCase {
targets: [.target(name: "lib", dependencies: [])])
"""
])
let packageRoot = resolveSymlinks(tempDir.appending(components: "pkg", "Sources", "lib"))
let packageRoot = try resolveSymlinks(tempDir.appending(components: "pkg", "Sources", "lib"))
let tr = ToolchainRegistry.shared
let ws = try! SwiftPMWorkspace(
workspacePath: packageRoot,
toolchainRegistry: tr,
fileSystem: fs,
buildSetup: TestSourceKitServer.serverOptions.buildSetup)

XCTAssertEqual(ws._packageRoot, resolveSymlinks(tempDir.appending(component: "pkg")))
XCTAssertEqual(ws._packageRoot, try resolveSymlinks(tempDir.appending(component: "pkg")))
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions Tests/SourceKitLSPTests/CallHierarchyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ final class CallHierarchyTests: XCTestCase {
Location(badUTF16: ws.testLoc(name))
}

func item(_ name: String, _ kind: SymbolKind, detail: String = "main", usr: String, at locName: String) -> CallHierarchyItem {
func item(_ name: String, _ kind: SymbolKind, detail: String = "main", usr: String, at locName: String) throws -> CallHierarchyItem {
let location = loc(locName)
return CallHierarchyItem(
name: name,
kind: kind,
tags: nil,
detail: detail,
uri: location.uri.nativeURI,
uri: try location.uri.nativeURI,
range: location.range,
selectionRange: location.range,
data: .dictionary([
"usr": .string(usr),
"uri": .string(location.uri.nativeURI.stringValue)
"uri": .string(try location.uri.nativeURI.stringValue)
])
)
}
Expand Down Expand Up @@ -114,27 +114,27 @@ final class CallHierarchyTests: XCTestCase {

XCTAssertEqual(try outgoingCalls(at: testLoc("a")), [])
XCTAssertEqual(try outgoingCalls(at: testLoc("b")), [
outCall(item("a()", .function, usr: aUsr, at: "a"), at: "b->a"),
outCall(item("c()", .function, usr: cUsr, at: "c"), at: "b->c"),
outCall(item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->b"),
outCall(try item("a()", .function, usr: aUsr, at: "a"), at: "b->a"),
outCall(try item("c()", .function, usr: cUsr, at: "c"), at: "b->c"),
outCall(try item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->b"),
])
XCTAssertEqual(try outgoingCalls(at: testLoc("c")), [
outCall(item("a()", .function, usr: aUsr, at: "a"), at: "c->a"),
outCall(item("d()", .function, usr: dUsr, at: "d"), at: "c->d"),
outCall(item("c()", .function, usr: cUsr, at: "c"), at: "c->c"),
outCall(try item("a()", .function, usr: aUsr, at: "a"), at: "c->a"),
outCall(try item("d()", .function, usr: dUsr, at: "d"), at: "c->d"),
outCall(try item("c()", .function, usr: cUsr, at: "c"), at: "c->c"),
])

// Test incoming call hierarchy

XCTAssertEqual(try incomingCalls(at: testLoc("a")), [
inCall(item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->a"),
inCall(item("c()", .function, usr: cUsr, at: "c"), at: "c->a"),
inCall(try item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->a"),
inCall(try item("c()", .function, usr: cUsr, at: "c"), at: "c->a"),
])
XCTAssertEqual(try incomingCalls(at: testLoc("b")), [
inCall(item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->b"),
inCall(try item("b(x:)", .function, usr: bUsr, at: "b"), at: "b->b"),
])
XCTAssertEqual(try incomingCalls(at: testLoc("d")), [
inCall(item("c()", .function, usr: cUsr, at: "c"), at: "c->d"),
inCall(try item("c()", .function, usr: cUsr, at: "c"), at: "c->d"),
])
}
}
4 changes: 2 additions & 2 deletions Tests/SourceKitLSPTests/ImplementationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ final class ImplementationTests: XCTestCase {
func testLoc(_ name: String) -> TestLocation {
ws.testLoc(name)
}
func loc(_ name: String) -> Location {
func loc(_ name: String) throws -> Location {
let location: TestLocation = ws.testLoc(name)
return Location(badUTF16: TestLocation(url: location.docUri.nativeURI.fileURL!, line: location.line, utf8Column: location.utf8Column, utf16Column: location.utf16Column))
return Location(badUTF16: TestLocation(url: try location.docUri.nativeURI.fileURL!, line: location.line, utf8Column: location.utf8Column, utf16Column: location.utf16Column))
}

try XCTAssertEqual(impls(at: testLoc("Protocol")), [loc("StructConformance")])
Expand Down
12 changes: 6 additions & 6 deletions Tests/SourceKitLSPTests/SourceKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ final class SKTests: XCTestCase {
}

XCTAssertEqual(jump.count, 1)
XCTAssertEqual(jump.first?.uri, locDef.docUri.nativeURI)
XCTAssertEqual(jump.first?.uri, try locDef.docUri.nativeURI)
XCTAssertEqual(jump.first?.range.lowerBound, locDef.position)

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

let call = ws.testLoc("aaa:call")
XCTAssertEqual(Set(refs), [
Location(TestLocation(url: URL(fileURLWithPath: resolveSymlinks(AbsolutePath(locDef.url.path)).pathString), line: locDef.line, utf8Column: locDef.utf8Column, utf16Column: locDef.utf16Column)),
Location(TestLocation(url: URL(fileURLWithPath: resolveSymlinks(AbsolutePath(locRef.url.path)).pathString), line: locRef.line, utf8Column: locRef.utf8Column, utf16Column: locRef.utf16Column)),
Location(TestLocation(url: URL(fileURLWithPath: resolveSymlinks(AbsolutePath(call.url.path)).pathString), line: call.line, utf8Column: call.utf8Column, utf16Column: call.utf16Column)),
Location(TestLocation(url: URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(locDef.url.path)).pathString), line: locDef.line, utf8Column: locDef.utf8Column, utf16Column: locDef.utf16Column)),
Location(TestLocation(url: URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(locRef.url.path)).pathString), line: locRef.line, utf8Column: locRef.utf8Column, utf16Column: locRef.utf16Column)),
Location(TestLocation(url: URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(call.url.path)).pathString), line: call.line, utf8Column: call.utf8Column, utf16Column: call.utf16Column)),
])
}

Expand Down Expand Up @@ -131,7 +131,7 @@ final class SKTests: XCTestCase {
return nil
}
XCTAssertEqual(jump.count, 1)
XCTAssertEqual(jump.first?.uri, locDef.docUri.nativeURI)
XCTAssertEqual(jump.first?.uri, try locDef.docUri.nativeURI)
XCTAssertEqual(jump.first?.range.lowerBound, locDef.position)

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

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

try ws.openDocument(refLoc.url, language: .c)
Expand Down
Loading