Skip to content

Fix the Pipe handling on Windows to be correct #341

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
Nov 2, 2020
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
8 changes: 4 additions & 4 deletions Sources/LSPTestSupport/TestJSONRPCConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public final class TestJSONRPCConnection {
public init() {
clientConnection = JSONRPCConnection(
protocol: testMessageRegistry,
inFD: serverToClient.fileHandleForReading.fileDescriptor,
outFD: clientToServer.fileHandleForWriting.fileDescriptor
inFD: serverToClient.fileHandleForReading,
outFD: clientToServer.fileHandleForWriting
)

serverConnection = JSONRPCConnection(
protocol: testMessageRegistry,
inFD: clientToServer.fileHandleForReading.fileDescriptor,
outFD: serverToClient.fileHandleForWriting.fileDescriptor
inFD: clientToServer.fileHandleForReading,
outFD: serverToClient.fileHandleForWriting
)

client = TestClient(server: clientConnection)
Expand Down
27 changes: 13 additions & 14 deletions Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
//===----------------------------------------------------------------------===//

#if canImport(CDispatch)
import CDispatch
import struct CDispatch.dispatch_fd_t
#endif
import Dispatch
import Foundation
import LanguageServerProtocol
import LSPLogging
#if os(Windows)
import ucrt
#endif

/// A connection between a message handler (e.g. language server) in the same process as the connection object and a remote message handler (e.g. language client) that may run in another process using JSON RPC messages sent over a pair of in/out file descriptors.
///
Expand Down Expand Up @@ -62,8 +59,8 @@ public final class JSONRPCConnection {

public init(
protocol messageRegistry: MessageRegistry,
inFD: Int32,
outFD: Int32,
inFD: FileHandle,
outFD: FileHandle,
syncRequests: Bool = false)
{
state = .created
Expand All @@ -72,26 +69,28 @@ public final class JSONRPCConnection {

let ioGroup = DispatchGroup()

ioGroup.enter()
#if os(Windows)
let inDesc: dispatch_fd_t = dispatch_fd_t(_get_osfhandle(inFD))
let rawInFD = dispatch_fd_t(bitPattern: inFD._handle)
#else
let inDesc: Int32 = Int32(inFD)
let rawInFD = inFD.fileDescriptor
#endif
receiveIO = DispatchIO(type: .stream, fileDescriptor: inDesc, queue: queue) { (error: Int32) in

ioGroup.enter()
receiveIO = DispatchIO(type: .stream, fileDescriptor: rawInFD, queue: queue) { (error: Int32) in
if error != 0 {
log("IO error \(error)", level: .error)
}
ioGroup.leave()
}

ioGroup.enter()
#if os(Windows)
let outDesc: dispatch_fd_t = dispatch_fd_t(_get_osfhandle(outFD))
let rawOutFD = dispatch_fd_t(bitPattern: outFD._handle)
#else
let outDesc: Int32 = Int32(outFD)
let rawOutFD = outFD.fileDescriptor
#endif
sendIO = DispatchIO(type: .stream, fileDescriptor: outDesc, queue: sendQueue) { (error: Int32) in

ioGroup.enter()
sendIO = DispatchIO(type: .stream, fileDescriptor: rawOutFD, queue: sendQueue) { (error: Int32) in
if error != 0 {
log("IO error \(error)", level: .error)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SKCore/BuildServerBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ private func makeJSONRPCBuildServer(client: MessageHandler, serverPath: Absolute

let connection = JSONRPCConnection(
protocol: BuildServerProtocol.bspRegistry,
inFD: serverToClient.fileHandleForReading.fileDescriptor,
outFD: clientToServer.fileHandleForWriting.fileDescriptor
inFD: serverToClient.fileHandleForReading,
outFD: clientToServer.fileHandleForWriting
)

connection.start(receiveHandler: client) {
Expand Down
8 changes: 4 additions & 4 deletions Sources/SKTestSupport/TestServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public final class TestSourceKitServer {

let clientConnection = JSONRPCConnection(
protocol: MessageRegistry.lspProtocol,
inFD: serverToClient.fileHandleForReading.fileDescriptor,
outFD: clientToServer.fileHandleForWriting.fileDescriptor
inFD: serverToClient.fileHandleForReading,
outFD: clientToServer.fileHandleForWriting
)
let serverConnection = JSONRPCConnection(
protocol: MessageRegistry.lspProtocol,
inFD: clientToServer.fileHandleForReading.fileDescriptor,
outFD: serverToClient.fileHandleForWriting.fileDescriptor
inFD: clientToServer.fileHandleForReading,
outFD: serverToClient.fileHandleForWriting
)

client = TestClient(server: clientConnection)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SourceKitLSP/Clang/ClangLanguageServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ func makeJSONRPCClangServer(

let connection = JSONRPCConnection(
protocol: MessageRegistry.lspProtocol,
inFD: clangdToUs.fileHandleForReading.fileDescriptor,
outFD: usToClangd.fileHandleForWriting.fileDescriptor
inFD: clangdToUs.fileHandleForReading,
outFD: usToClangd.fileHandleForWriting
)

let connectionToClient = LocalConnection()
Expand Down
4 changes: 2 additions & 2 deletions Sources/sourcekit-lsp/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ struct Main: ParsableCommand {

let clientConnection = JSONRPCConnection(
protocol: MessageRegistry.lspProtocol,
inFD: fileno(stdin),
outFD: realStdout,
inFD: FileHandle.standardInput,
outFD: FileHandle(fileDescriptor: realStdout, closeOnDealloc: false),
syncRequests: syncRequests
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ class ConnectionTests: XCTestCase {

let conn = JSONRPCConnection(
protocol: MessageRegistry(requests: [], notifications: []),
inFD: to.fileHandleForReading.fileDescriptor,
outFD: from.fileHandleForWriting.fileDescriptor)
inFD: to.fileHandleForReading,
outFD: from.fileHandleForWriting)

final class DummyHandler: MessageHandler {
func handle<N: NotificationType>(_: N, from: ObjectIdentifier) {}
Expand Down