From f64a4c4acef18676a8251ab760a51a05ad05e370 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Thu, 28 Nov 2024 22:05:58 -0800 Subject: [PATCH] Fix debugger connection issue on Windows --- src/js/internal/debugger.ts | 39 +++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/js/internal/debugger.ts b/src/js/internal/debugger.ts index 95ef812e36c0a3..0c66c12b10bb7a 100644 --- a/src/js/internal/debugger.ts +++ b/src/js/internal/debugger.ts @@ -1,4 +1,4 @@ -import type { ServerWebSocket, Socket, WebSocketHandler, Server as WebSocketServer } from "bun"; +import type { Socket, ServerWebSocket, WebSocketHandler, Server as WebSocketServer } from "bun"; const enum FramerState { WaitingForLength, WaitingForMessage, @@ -411,8 +411,43 @@ async function connectToUnixServer( send: (message: string) => void, close: () => void, ) { + // Windows uses TCP. + // POSIX uses Unix sockets. + // + // We use TCP on Windows because VSCode/Node doesn't seem to support Unix sockets very well. + // + // Unix sockets are preferred because there's less of a risk of conflicting + // with other tools or a port already being used + sometimes machines don't + // allow binding to TCP ports. + let connectionOptions; + if (unix.startsWith("unix:")) { + unix = unescapeUnixSocketUrl(unix); + if (unix.startsWith("unix:")) { + unix = unix.substring("unix:".length); + } + connectionOptions = { unix }; + } else if (unix.startsWith("tcp:")) { + try { + const { hostname, port } = new URL(unix); + connectionOptions = { + hostname, + port: Number(port), + }; + } catch (error) { + exit("Invalid tcp: URL:" + unix); + return; + } + } else if (unix.startsWith("/")) { + connectionOptions = { unix }; + } else if (unix.startsWith("fd:")) { + connectionOptions = { fd: Number(unix.substring("fd:".length)) }; + } else { + $debug("Invalid inspector URL:" + unix); + return; + } + const socket = await Bun.connect<{ framer: SocketFramer; backend: Backend }>({ - unix, + ...connectionOptions, socket: { open: socket => { const framer = new SocketFramer((message: string | string[]) => {