From 2caa5dc8f2c2a6373eee71da2c77bfcf804eaeec Mon Sep 17 00:00:00 2001 From: pfg Date: Thu, 2 Oct 2025 14:11:29 -0700 Subject: [PATCH] Passthrough anyerror when using handleOom (#23176) ### What does this PR do? Previously, handleOom(anyerror!T) would return T and panic for OutOfMemory for any error. fixes it to return anyerror!T for this case. ### How did you verify your code works? CI --------- Co-authored-by: taylor.fish --- src/ast/parseTypescript.zig | 2 +- src/ast/visitStmt.zig | 10 +++++----- src/bun.js/webcore/Response.zig | 12 ++++++------ src/bun.js/webcore/S3Client.zig | 16 ++++++++-------- src/bundler/bundle_v2.zig | 8 ++++++-- src/cli/test_command.zig | 2 +- src/css/printer.zig | 2 +- src/handle_oom.zig | 2 +- 8 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/ast/parseTypescript.zig b/src/ast/parseTypescript.zig index 35432904e3..26d6aad257 100644 --- a/src/ast/parseTypescript.zig +++ b/src/ast/parseTypescript.zig @@ -210,7 +210,7 @@ pub fn ParseTypescript( p.popScope(); if (!opts.is_typescript_declare) { - name.ref = bun.handleOom(p.declareSymbol(.ts_namespace, name_loc, name_text)); + name.ref = try p.declareSymbol(.ts_namespace, name_loc, name_text); try p.ref_to_ts_namespace_member.put(p.allocator, name.ref.?, ns_member_data); } diff --git a/src/ast/visitStmt.zig b/src/ast/visitStmt.zig index 9a6e7b879a..f56b933b00 100644 --- a/src/ast/visitStmt.zig +++ b/src/ast/visitStmt.zig @@ -1315,10 +1315,10 @@ pub fn VisitStmt( try p.top_level_enums.append(p.allocator, data.name.ref.?); } - bun.handleOom(p.recordDeclaredSymbol(data.name.ref.?)); - bun.handleOom(p.pushScopeForVisitPass(.entry, stmt.loc)); + try p.recordDeclaredSymbol(data.name.ref.?); + try p.pushScopeForVisitPass(.entry, stmt.loc); defer p.popScope(); - bun.handleOom(p.recordDeclaredSymbol(data.arg)); + try p.recordDeclaredSymbol(data.arg); const allocator = p.allocator; // Scan ahead for any variables inside this namespace. This must be done @@ -1327,7 +1327,7 @@ pub fn VisitStmt( // We need to convert the uses into property accesses on the namespace. for (data.values) |value| { if (value.ref.isValid()) { - bun.handleOom(p.is_exported_inside_namespace.put(allocator, value.ref, data.arg)); + try p.is_exported_inside_namespace.put(allocator, value.ref, data.arg); } } @@ -1336,7 +1336,7 @@ pub fn VisitStmt( // without initializers are initialized to undefined. var next_numeric_value: ?f64 = 0.0; - var value_exprs = bun.handleOom(ListManaged(Expr).initCapacity(allocator, data.values.len)); + var value_exprs = try ListManaged(Expr).initCapacity(allocator, data.values.len); var all_values_are_pure = true; diff --git a/src/bun.js/webcore/Response.zig b/src/bun.js/webcore/Response.zig index c2532c80a9..fa0a6b3c06 100644 --- a/src/bun.js/webcore/Response.zig +++ b/src/bun.js/webcore/Response.zig @@ -146,38 +146,38 @@ pub fn writeFormat(this: *Response, comptime Formatter: type, formatter: *Format try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime Output.prettyFmt("ok: ", enable_ansi_colors)); try formatter.printAs(.Boolean, Writer, writer, jsc.JSValue.jsBoolean(this.isOK()), .BooleanObject, enable_ansi_colors); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime Output.prettyFmt("url: \"", enable_ansi_colors)); try writer.print(comptime Output.prettyFmt("{}", enable_ansi_colors), .{this.url}); try writer.writeAll("\""); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime Output.prettyFmt("status: ", enable_ansi_colors)); try formatter.printAs(.Double, Writer, writer, jsc.JSValue.jsNumber(this.init.status_code), .NumberObject, enable_ansi_colors); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime Output.prettyFmt("statusText: ", enable_ansi_colors)); try writer.print(comptime Output.prettyFmt("\"{}\"", enable_ansi_colors), .{this.init.status_text}); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime Output.prettyFmt("headers: ", enable_ansi_colors)); try formatter.printAs(.Private, Writer, writer, try this.getHeaders(formatter.globalThis), .DOMWrapper, enable_ansi_colors); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime Output.prettyFmt("redirected: ", enable_ansi_colors)); try formatter.printAs(.Boolean, Writer, writer, jsc.JSValue.jsBoolean(this.redirected), .BooleanObject, enable_ansi_colors); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); formatter.resetLine(); diff --git a/src/bun.js/webcore/S3Client.zig b/src/bun.js/webcore/S3Client.zig index 414caa1d1a..69cb2a8f6d 100644 --- a/src/bun.js/webcore/S3Client.zig +++ b/src/bun.js/webcore/S3Client.zig @@ -12,21 +12,21 @@ pub fn writeFormatCredentials(credentials: *S3Credentials, options: bun.S3.Multi try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime bun.Output.prettyFmt("endpoint: \"", enable_ansi_colors)); try writer.print(comptime bun.Output.prettyFmt("{s}\"", enable_ansi_colors), .{endpoint}); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); const region = if (credentials.region.len > 0) credentials.region else S3Credentials.guessRegion(credentials.endpoint); try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime bun.Output.prettyFmt("region: \"", enable_ansi_colors)); try writer.print(comptime bun.Output.prettyFmt("{s}\"", enable_ansi_colors), .{region}); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); // PS: We don't want to print the credentials if they are empty just signal that they are there without revealing them if (credentials.accessKeyId.len > 0) { try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime bun.Output.prettyFmt("accessKeyId: \"[REDACTED]\"", enable_ansi_colors)); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); } @@ -34,7 +34,7 @@ pub fn writeFormatCredentials(credentials: *S3Credentials, options: bun.S3.Multi if (credentials.secretAccessKey.len > 0) { try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime bun.Output.prettyFmt("secretAccessKey: \"[REDACTED]\"", enable_ansi_colors)); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); } @@ -42,7 +42,7 @@ pub fn writeFormatCredentials(credentials: *S3Credentials, options: bun.S3.Multi if (credentials.sessionToken.len > 0) { try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime bun.Output.prettyFmt("sessionToken: \"[REDACTED]\"", enable_ansi_colors)); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); } @@ -51,7 +51,7 @@ pub fn writeFormatCredentials(credentials: *S3Credentials, options: bun.S3.Multi try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime bun.Output.prettyFmt("acl: ", enable_ansi_colors)); try writer.print(comptime bun.Output.prettyFmt("{s}\"", enable_ansi_colors), .{acl_value.toString()}); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); } @@ -59,14 +59,14 @@ pub fn writeFormatCredentials(credentials: *S3Credentials, options: bun.S3.Multi try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime bun.Output.prettyFmt("partSize: ", enable_ansi_colors)); try formatter.printAs(.Double, Writer, writer, jsc.JSValue.jsNumber(options.partSize), .NumberObject, enable_ansi_colors); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); try formatter.writeIndent(Writer, writer); try writer.writeAll(comptime bun.Output.prettyFmt("queueSize: ", enable_ansi_colors)); try formatter.printAs(.Double, Writer, writer, jsc.JSValue.jsNumber(options.queueSize), .NumberObject, enable_ansi_colors); - bun.handleOom(formatter.printComma(Writer, writer, enable_ansi_colors)); + try formatter.printComma(Writer, writer, enable_ansi_colors); try writer.writeAll("\n"); try formatter.writeIndent(Writer, writer); diff --git a/src/bundler/bundle_v2.zig b/src/bundler/bundle_v2.zig index 430884450b..0e7aa8f698 100644 --- a/src/bundler/bundle_v2.zig +++ b/src/bundler/bundle_v2.zig @@ -178,7 +178,9 @@ pub const BundleV2 = struct { fn ensureClientTranspiler(this: *BundleV2) void { if (this.client_transpiler == null) { - _ = bun.handleOom(this.initializeClientTranspiler()); + _ = this.initializeClientTranspiler() catch |e| { + std.debug.panic("Failed to initialize client transpiler: {s}", .{@errorName(e)}); + }; } } @@ -233,7 +235,9 @@ pub const BundleV2 = struct { pub inline fn transpilerForTarget(noalias this: *BundleV2, target: options.Target) *Transpiler { if (!this.transpiler.options.server_components and this.linker.dev_server == null) { if (target == .browser and this.transpiler.options.target.isServerSide()) { - return this.client_transpiler orelse bun.handleOom(this.initializeClientTranspiler()); + return this.client_transpiler orelse this.initializeClientTranspiler() catch |e| { + std.debug.panic("Failed to initialize client transpiler: {s}", .{@errorName(e)}); + }; } return this.transpiler; diff --git a/src/cli/test_command.zig b/src/cli/test_command.zig index 8c0daab27c..9626a14b65 100644 --- a/src/cli/test_command.zig +++ b/src/cli/test_command.zig @@ -861,7 +861,7 @@ pub const CommandLineReporter = struct { } const output_writer = Output.errorWriter(); // unbuffered. buffered is errorWriterBuffered() / Output.flush() - bun.handleOom(output_writer.writeAll(output_buf.items[initial_length..])); + output_writer.writeAll(output_buf.items[initial_length..]) catch {}; var this: *CommandLineReporter = buntest.reporter orelse return; // command line reporter is missing! uh oh! diff --git a/src/css/printer.zig b/src/css/printer.zig index 44f643f9d9..f79346eca3 100644 --- a/src/css/printer.zig +++ b/src/css/printer.zig @@ -352,7 +352,7 @@ pub fn Printer(comptime Writer: type) type { pub fn writeFmt(this: *This, comptime fmt: []const u8, args: anytype) PrintErr!void { // assuming the writer comes from an ArrayList const start: usize = getWrittenAmt(this.dest); - bun.handleOom(this.dest.print(fmt, args)); + this.dest.print(fmt, args) catch return this.addFmtError(); const written = getWrittenAmt(this.dest) - start; this.col += @intCast(written); } diff --git a/src/handle_oom.zig b/src/handle_oom.zig index ac166cd03b..62cb8019b4 100644 --- a/src/handle_oom.zig +++ b/src/handle_oom.zig @@ -5,7 +5,7 @@ fn isOomOnlyError(comptime ErrorUnionOrSet: type) bool { .error_set => ErrorUnionOrSet, else => @compileError("argument must be an error union or error set"), }; - for (@typeInfo(ErrorSet).error_set orelse &.{}) |err| { + for (@typeInfo(ErrorSet).error_set orelse return false) |err| { if (!std.mem.eql(u8, err.name, "OutOfMemory")) return false; } return true;