Skip to content

Instead of returning an empty array in prepareTypeHierarchy, sourcekit-lsp should return nil #1178

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
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
7 changes: 7 additions & 0 deletions Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,13 @@ extension SourceKitLSPServer {
}
.sorted(by: { $0.name < $1.name })

if typeHierarchyItems.isEmpty {
// When returning an empty array, VS Code fails with the following two errors. Returning `nil` works around those
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is because it always expects at least the original element itself? Does call hierarchy have the same issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call hierarchy doesn’t seem to have that issue.

// VS Code-internal errors showing up
// - MISSING provider
// - Cannot read properties of null (reading 'kind')
return nil
}
// Ideally, we should show multiple symbols. But VS Code fails to display type hierarchies with multiple root items,
// failing with `Cannot read properties of undefined (reading 'map')`. Pick the first one.
return Array(typeHierarchyItems.prefix(1))
Expand Down
23 changes: 23 additions & 0 deletions Tests/SourceKitLSPTests/TypeHierarchyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import ISDBTestSupport
import LSPTestSupport
import LanguageServerProtocol
import SKTestSupport
import TSCBasic
Expand Down Expand Up @@ -185,6 +186,28 @@ final class TypeHierarchyTests: XCTestCase {
]
)
}

func testTypeHierarchyForFileWithoutIndex() async throws {
let project = try await SwiftPMTestProject(files: [
"Test.swift": """
class MyClass {}
class 1️⃣MySubclass: MyClass {}

let x = MySubclass()
"""
])

let (uri, positions) = try project.openDocument("Test.swift")

// We don't have the type hierarchy because the file hasn't been indexed yet. Returning an empty array makes VS Code
// fail with the following two errors, so we should return `nil` instead.
// - MISSING provider
// - Cannot read properties of null (reading 'kind')
let response = try await project.testClient.send(
TypeHierarchyPrepareRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
XCTAssertNil(response)
}
}

// MARK: - Utilities
Expand Down