Skip to content

Commit 78b4955

Browse files
committed
Log full initialize request and response
The initialize request and response are often critical to debug issues with editor setups. Log them completely over multiple log messages, instead of truncating them in the normal logging path.
1 parent 444ce41 commit 78b4955

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

Sources/LanguageServerProtocolJSONRPC/LoggableMessageTypes.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ import SKLogging
1616

1717
// MARK: - RequestType
1818

19-
fileprivate struct AnyRequestType: CustomLogStringConvertible {
19+
package struct AnyRequestType: CustomLogStringConvertible {
2020
let request: any RequestType
2121

22-
public var description: String {
22+
package init(request: any RequestType) {
23+
self.request = request
24+
}
25+
26+
package var description: String {
2327
return """
2428
\(type(of: request).method)
2529
\(request.prettyPrintedJSON)
2630
"""
2731
}
2832

29-
public var redactedDescription: String {
33+
package var redactedDescription: String {
3034
return """
3135
\(type(of: request).method)
3236
\(request.prettyPrintedRedactedJSON)
@@ -42,17 +46,21 @@ extension RequestType {
4246

4347
// MARK: - NotificationType
4448

45-
fileprivate struct AnyNotificationType: CustomLogStringConvertible {
49+
package struct AnyNotificationType: CustomLogStringConvertible {
4650
let notification: any NotificationType
4751

48-
public var description: String {
52+
package init(notification: any NotificationType) {
53+
self.notification = notification
54+
}
55+
56+
package var description: String {
4957
return """
5058
\(type(of: notification).method)
5159
\(notification.prettyPrintedJSON)
5260
"""
5361
}
5462

55-
public var redactedDescription: String {
63+
package var redactedDescription: String {
5664
return """
5765
\(type(of: notification).method)
5866
\(notification.prettyPrintedRedactedJSON)
@@ -68,17 +76,21 @@ extension NotificationType {
6876

6977
// MARK: - ResponseType
7078

71-
fileprivate struct AnyResponseType: CustomLogStringConvertible {
79+
package struct AnyResponseType: CustomLogStringConvertible {
7280
let response: any ResponseType
7381

74-
var description: String {
82+
package init(response: any ResponseType) {
83+
self.response = response
84+
}
85+
86+
package var description: String {
7587
return """
7688
\(type(of: response))
7789
\(response.prettyPrintedJSON)
7890
"""
7991
}
8092

81-
var redactedDescription: String {
93+
package var redactedDescription: String {
8294
return """
8395
\(type(of: response))
8496
\(response.prettyPrintedRedactedJSON)

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Dispatch
1616
import Foundation
1717
import IndexStoreDB
1818
import LanguageServerProtocol
19+
import LanguageServerProtocolJSONRPC
1920
import PackageLoading
2021
import SKLogging
2122
import SKOptions
@@ -873,6 +874,7 @@ extension SourceKitLSPServer {
873874
}
874875

875876
func initialize(_ req: InitializeRequest) async throws -> InitializeResult {
877+
logger.logFullObjectInMultipleLogMessages(header: "Initialize request", AnyRequestType(request: req))
876878
// If the client can handle `PeekDocumentsRequest`, they can enable the
877879
// experimental client capability `"workspace/peekDocuments"` through the `req.capabilities.experimental`.
878880
//
@@ -983,12 +985,14 @@ extension SourceKitLSPServer {
983985

984986
assert(!self.workspaces.isEmpty)
985987

986-
return InitializeResult(
988+
let result = InitializeResult(
987989
capabilities: await self.serverCapabilities(
988990
for: req.capabilities,
989991
registry: self.capabilityRegistry!
990992
)
991993
)
994+
logger.logFullObjectInMultipleLogMessages(header: "Initialize response", AnyRequestType(request: req))
995+
return result
992996
}
993997

994998
func serverCapabilities(

Tests/SKLoggingTests/PrettyPrintedRedactedJSONTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,22 @@ class PrettyPrintedRedactedJSONTests: XCTestCase {
100100
"""
101101
)
102102
}
103+
104+
func testArrayOfStrings() {
105+
struct Struct: Codable {
106+
var value: [String]
107+
}
108+
109+
XCTAssertEqual(
110+
Struct(value: ["password", "admin"]).prettyPrintedRedactedJSON,
111+
"""
112+
{
113+
"value" : [
114+
"<private 5e884898da280471>",
115+
"<private 8c6976e5b5410415>"
116+
]
117+
}
118+
"""
119+
)
120+
}
103121
}

0 commit comments

Comments
 (0)