From 14b858822126f50d54c81e733f81451fce7f9f69 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Fri, 22 Nov 2024 15:59:07 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20crash=20when=20opening=20a=20fi?= =?UTF-8?q?le=20with=20an=20empty=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #1739 rdar://137886470 --- .../LanguageServerProtocol/SupportTypes/DocumentURI.swift | 8 ++++++-- Tests/SourceKitLSPTests/LifecycleTests.swift | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Sources/LanguageServerProtocol/SupportTypes/DocumentURI.swift b/Sources/LanguageServerProtocol/SupportTypes/DocumentURI.swift index a0016bcd9..5f593d747 100644 --- a/Sources/LanguageServerProtocol/SupportTypes/DocumentURI.swift +++ b/Sources/LanguageServerProtocol/SupportTypes/DocumentURI.swift @@ -56,8 +56,12 @@ public struct DocumentURI: Codable, Hashable, Sendable { /// fallback mode that drops semantic functionality. public var pseudoPath: String { if storage.isFileURL { - return storage.withUnsafeFileSystemRepresentation { - String(cString: $0!) + return storage.withUnsafeFileSystemRepresentation { filePath in + if let filePath { + String(cString: filePath) + } else { + "" + } } } else { return storage.absoluteString diff --git a/Tests/SourceKitLSPTests/LifecycleTests.swift b/Tests/SourceKitLSPTests/LifecycleTests.swift index 956dfdf68..fef386c80 100644 --- a/Tests/SourceKitLSPTests/LifecycleTests.swift +++ b/Tests/SourceKitLSPTests/LifecycleTests.swift @@ -164,4 +164,9 @@ final class LifecycleTests: XCTestCase { ) ) } + + func testOpenFileWithoutPath() async throws { + let testClient = try await TestSourceKitLSPClient() + testClient.openDocument("", uri: DocumentURI(try XCTUnwrap(URL(string: "file://"))), language: .swift) + } }