Skip to content

Commit 14b8588

Browse files
committed
Don’t crash when opening a file with an empty path
Fixes #1739 rdar://137886470
1 parent 6c5f5c9 commit 14b8588

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Sources/LanguageServerProtocol/SupportTypes/DocumentURI.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ public struct DocumentURI: Codable, Hashable, Sendable {
5656
/// fallback mode that drops semantic functionality.
5757
public var pseudoPath: String {
5858
if storage.isFileURL {
59-
return storage.withUnsafeFileSystemRepresentation {
60-
String(cString: $0!)
59+
return storage.withUnsafeFileSystemRepresentation { filePath in
60+
if let filePath {
61+
String(cString: filePath)
62+
} else {
63+
""
64+
}
6165
}
6266
} else {
6367
return storage.absoluteString

Tests/SourceKitLSPTests/LifecycleTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,9 @@ final class LifecycleTests: XCTestCase {
164164
)
165165
)
166166
}
167+
168+
func testOpenFileWithoutPath() async throws {
169+
let testClient = try await TestSourceKitLSPClient()
170+
testClient.openDocument("", uri: DocumentURI(try XCTUnwrap(URL(string: "file://"))), language: .swift)
171+
}
167172
}

0 commit comments

Comments
 (0)