diff --git a/.vscode/launch.json b/.vscode/launch.json index 24279f7d2d..3e8ab83e74 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -18,7 +18,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -32,7 +32,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "0", + "BUN_GARBAGE_COLLECTOR_LEVEL": "0" }, "console": "internalConsole" }, @@ -46,7 +46,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "0", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -60,7 +60,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -74,7 +74,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -93,7 +93,7 @@ }, "console": "internalConsole", "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" } @@ -113,7 +113,7 @@ }, "console": "internalConsole", "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" } @@ -129,9 +129,9 @@ "env": { "FORCE_COLOR": "0", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, - "console": "internalConsole", + "console": "internalConsole" }, { "type": "lldb", @@ -143,7 +143,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "0", + "BUN_GARBAGE_COLLECTOR_LEVEL": "0" }, "console": "internalConsole" }, @@ -157,7 +157,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "0", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -171,7 +171,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -185,7 +185,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -204,10 +204,10 @@ }, "console": "internalConsole", "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" - }, + } }, { "type": "lldb", @@ -224,10 +224,10 @@ }, "console": "internalConsole", "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" - }, + } }, // bun test [...] { @@ -240,7 +240,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -254,7 +254,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "0", + "BUN_GARBAGE_COLLECTOR_LEVEL": "0" }, "console": "internalConsole" }, @@ -268,7 +268,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "0", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -282,7 +282,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -296,7 +296,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "2", + "BUN_GARBAGE_COLLECTOR_LEVEL": "2" }, "console": "internalConsole" }, @@ -315,7 +315,7 @@ }, "console": "internalConsole", "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" } @@ -335,7 +335,7 @@ }, "console": "internalConsole", "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" } @@ -365,7 +365,7 @@ "env": { "FORCE_COLOR": "1", "BUN_DEBUG_QUIET_LOGS": "1", - "BUN_GARBAGE_COLLECTOR_LEVEL": "0", + "BUN_GARBAGE_COLLECTOR_LEVEL": "0" }, "console": "internalConsole" }, @@ -384,7 +384,7 @@ }, "console": "internalConsole", "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" } @@ -419,7 +419,29 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "2" } - ], + ] + }, + { + "type": "cppvsdbg", + "request": "launch", + "name": "Windows: bun test --only [file]", + "program": "${workspaceFolder}/build/bun-debug.exe", + "args": ["test", "--only", "${file}"], + "cwd": "${workspaceFolder}/test", + "environment": [ + { + "name": "FORCE_COLOR", + "value": "1" + }, + { + "name": "BUN_DEBUG_QUIET_LOGS", + "value": "1" + }, + { + "name": "BUN_GARBAGE_COLLECTOR_LEVEL", + "value": "2" + } + ] }, { "type": "cppvsdbg", @@ -441,7 +463,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "0" } - ], + ] }, { "type": "cppvsdbg", @@ -463,7 +485,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "2" } - ], + ] }, { "type": "cppvsdbg", @@ -491,10 +513,10 @@ } ], "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" - }, + } }, { "type": "cppvsdbg", @@ -522,10 +544,10 @@ } ], "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" - }, + } }, // Windows: bun run [file] { @@ -548,7 +570,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "2" } - ], + ] }, { "type": "cppvsdbg", @@ -570,7 +592,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "0" } - ], + ] }, { "type": "cppvsdbg", @@ -592,7 +614,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "2" } - ], + ] }, { "type": "cppvsdbg", @@ -620,10 +642,10 @@ } ], "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" - }, + } }, { "type": "cppvsdbg", @@ -651,10 +673,10 @@ } ], "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" - }, + } }, // Windows: bun test [...] { @@ -677,7 +699,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "2" } - ], + ] }, { "type": "cppvsdbg", @@ -699,7 +721,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "0" } - ], + ] }, { "type": "cppvsdbg", @@ -721,7 +743,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "2" } - ], + ] }, { "type": "cppvsdbg", @@ -743,7 +765,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "2" } - ], + ] }, { "type": "cppvsdbg", @@ -765,7 +787,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "2" } - ], + ] }, { "type": "cppvsdbg", @@ -793,10 +815,10 @@ } ], "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" - }, + } }, { "type": "cppvsdbg", @@ -824,10 +846,10 @@ } ], "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" - }, + } }, // Windows: bun test [*] { @@ -850,7 +872,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "2" } - ], + ] }, { "type": "cppvsdbg", @@ -872,7 +894,7 @@ "name": "BUN_GARBAGE_COLLECTOR_LEVEL", "value": "0" } - ], + ] }, { "type": "cppvsdbg", @@ -900,10 +922,10 @@ } ], "serverReadyAction": { - "pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/", + "pattern": "https://debug.bun.sh/#localhost:([0-9]+)/", "uriFormat": "https://debug.bun.sh/#ws://localhost:%s/", "action": "openExternally" - }, + } }, { "type": "cppvsdbg", @@ -913,7 +935,7 @@ "args": ["src/runner.node.mjs"], "cwd": "${workspaceFolder}/packages/bun-internal-test", "console": "internalConsole" - }, + } ], "inputs": [ { @@ -925,6 +947,6 @@ "id": "testName", "type": "promptString", "description": "Usage: bun test [...]" - }, + } ] } diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index 563523b308..f6a74a318c 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -992,11 +992,9 @@ pub fn getMain( break :use_resolved_path; } - const fd = bun.sys.openat( - // avoid going thorugh libuv for this one. - bun.toFD(std.fs.cwd().fd), - - &(std.os.toPosixPath(vm.main) catch break :use_resolved_path), + const fd = bun.sys.openatA( + if (comptime Environment.isWindows) bun.invalid_fd else bun.toFD(std.fs.cwd().fd), + vm.main, // Open with the minimum permissions necessary for resolving the file path. if (comptime Environment.isLinux) std.os.O.PATH else std.os.O.RDONLY, diff --git a/src/cli.zig b/src/cli.zig index efd810b402..030f24afe1 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -46,7 +46,7 @@ pub const Cli = struct { var wait_group: sync.WaitGroup = undefined; var log_: logger.Log = undefined; pub fn startTransform(_: std.mem.Allocator, _: Api.TransformOptions, _: *logger.Log) anyerror!void {} - pub fn start(allocator: std.mem.Allocator, _: anytype, _: anytype, comptime MainPanicHandler: type) void { + pub fn start(allocator: std.mem.Allocator, comptime MainPanicHandler: type) void { start_time = std.time.nanoTimestamp(); log_ = logger.Log.init(allocator); diff --git a/src/fd.zig b/src/fd.zig index 02aee3d8a0..dfc8c360d1 100644 --- a/src/fd.zig +++ b/src/fd.zig @@ -99,6 +99,13 @@ pub const FDImpl = packed struct { } } + pub fn fromSystemWithoutAssertion(system_fd: System) FDImpl { + return FDImpl{ + .kind = .system, + .value = .{ .as_system = handleToNumber(system_fd) }, + }; + } + pub fn fromSystem(system_fd: System) FDImpl { if (env.os == .windows) { // the current process fd is max usize @@ -106,10 +113,7 @@ pub const FDImpl = packed struct { std.debug.assert(@intFromPtr(system_fd) <= std.math.maxInt(SystemAsInt)); } - return FDImpl{ - .kind = .system, - .value = .{ .as_system = handleToNumber(system_fd) }, - }; + return fromSystemWithoutAssertion(system_fd); } pub fn fromUV(uv_fd: UV) FDImpl { diff --git a/src/main.zig b/src/main.zig index 624f8f33c7..d6a41e68ea 100644 --- a/src/main.zig +++ b/src/main.zig @@ -39,11 +39,16 @@ pub fn main() void { if (Environment.isWindows) { environ = @ptrCast(std.os.environ.ptr); _environ = @ptrCast(std.os.environ.ptr); - bun.win32.STDOUT_FD = bun.toFD(std.io.getStdOut().handle); - bun.win32.STDERR_FD = bun.toFD(std.io.getStdErr().handle); - bun.win32.STDIN_FD = bun.toFD(std.io.getStdIn().handle); + const peb = std.os.windows.peb(); + const stdout = peb.ProcessParameters.hStdOutput; + const stderr = peb.ProcessParameters.hStdError; + const stdin = peb.ProcessParameters.hStdInput; - bun.buffered_stdin.unbuffered_reader.context.handle = std.io.getStdIn().handle; + bun.win32.STDERR_FD = if (stderr != std.os.windows.INVALID_HANDLE_VALUE) bun.toFD(stderr) else bun.invalid_fd; + bun.win32.STDOUT_FD = if (stdout != std.os.windows.INVALID_HANDLE_VALUE) bun.toFD(stdout) else bun.invalid_fd; + bun.win32.STDIN_FD = if (stdin != std.os.windows.INVALID_HANDLE_VALUE) bun.toFD(stdin) else bun.invalid_fd; + + bun.buffered_stdin.unbuffered_reader.context.handle = stdin; const w = std.os.windows; @@ -52,9 +57,8 @@ pub fn main() void { _ = w.kernel32.SetConsoleOutputCP(CP_UTF8); var mode: w.DWORD = undefined; - const stdoutHandle = w.peb().ProcessParameters.hStdOutput; - if (w.kernel32.GetConsoleMode(stdoutHandle, &mode) != 0) { - _ = SetConsoleMode(stdoutHandle, mode | w.ENABLE_VIRTUAL_TERMINAL_PROCESSING); + if (w.kernel32.GetConsoleMode(stdout, &mode) != 0) { + _ = SetConsoleMode(stdout, mode | w.ENABLE_VIRTUAL_TERMINAL_PROCESSING); } } @@ -79,5 +83,5 @@ pub fn main() void { ); } - bun.CLI.Cli.start(bun.default_allocator, stdout, stderr, MainPanicHandler); + bun.CLI.Cli.start(bun.default_allocator, MainPanicHandler); } diff --git a/test/js/web/websocket/websocket-subprocess.ts b/test/js/web/websocket/websocket-subprocess.ts index fd25b7fd5b..bdf8cf3b84 100644 --- a/test/js/web/websocket/websocket-subprocess.ts +++ b/test/js/web/websocket/websocket-subprocess.ts @@ -11,3 +11,15 @@ ws.onmessage = message => { }, 300); } }; + +ws.onclose = () => { + console.log("Closed!"); +}; + +ws.onerror = e => { + console.error(e); +}; + +ws.onopen = () => { + console.log("Connected!"); +}; diff --git a/test/js/web/websocket/websocket.test.js b/test/js/web/websocket/websocket.test.js index 55cdbeb42e..e4e70bab31 100644 --- a/test/js/web/websocket/websocket.test.js +++ b/test/js/web/websocket/websocket.test.js @@ -1,7 +1,7 @@ import { describe, it, expect } from "bun:test"; import { bunExe, bunEnv, gc } from "harness"; import { readFileSync } from "fs"; -import { join } from "path"; +import { join, resolve } from "path"; import process from "process"; const TEST_WEBSOCKET_HOST = process.env.TEST_WEBSOCKET_HOST || "wss://ws.postman-echo.com/raw"; @@ -566,6 +566,7 @@ describe("websocket in subprocess", () => { }); it("should exit after server stop and 0 messages", async () => { + const { promise, resolve } = Promise.withResolvers(); const server = Bun.serve({ port: 0, fetch(req, server) { @@ -576,7 +577,9 @@ describe("websocket in subprocess", () => { return new Response("http response"); }, websocket: { - open(ws) {}, + open(ws) { + resolve(); + }, message(ws, message) {}, close(ws) {}, }, @@ -584,12 +587,12 @@ describe("websocket in subprocess", () => { const subprocess = Bun.spawn({ cmd: [bunExe(), import.meta.dir + "/websocket-subprocess.ts", `http://${server.hostname}:${server.port}`], - stderr: "pipe", - stdin: "pipe", - stdout: "pipe", + stderr: "inherit", + stdin: "inherit", + stdout: "inherit", env: bunEnv, }); - + await promise; server.stop(true); expect(await subprocess.exited).toBe(0); });