Skip to content
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

fix(shell): remove unecessary allocations when printing errors #17898

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 6 additions & 11 deletions src/shell/interpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3671,8 +3671,7 @@ pub const Interpreter = struct {
const err = this.state.expanding_redirect.expansion.state.err;
defer err.deinit(bun.default_allocator);
this.state.expanding_redirect.expansion.deinit();
const buf = err.fmt();
this.writeFailingError("{s}", .{buf});
this.writeFailingError("{}\n", .{err});
return;
}
this.next();
Expand Down Expand Up @@ -4089,8 +4088,7 @@ pub const Interpreter = struct {
const err = this.state.expanding_args.expansion.state.err;
defer err.deinit(bun.default_allocator);
this.state.expanding_args.expansion.deinit();
const buf = err.fmt();
this.writeFailingError("{s}", .{buf});
this.writeFailingError("{}\n", .{err});
return;
}
child.deinit();
Expand Down Expand Up @@ -4691,8 +4689,7 @@ pub const Interpreter = struct {
const err = this.state.expanding_assigns.state.err;
defer err.deinit(bun.default_allocator);
this.state.expanding_assigns.deinit();
const buf = err.fmt();
this.writeFailingError("{s}", .{buf});
this.writeFailingError("{}\n", .{err});
return;
}

Expand All @@ -4715,8 +4712,7 @@ pub const Interpreter = struct {
else => @panic("Invalid state"),
};
defer err.deinit(bun.default_allocator);
const buf = err.fmt();
this.writeFailingError("{s}", .{buf});
this.writeFailingError("{}\n", .{err});
return;
}
// Handling this case from the shell spec:
Expand Down Expand Up @@ -4936,12 +4932,11 @@ pub const Interpreter = struct {
.buffered_closed = buffered_closed,
} };
const subproc = switch (Subprocess.spawnAsync(this.base.eventLoop(), &shellio, spawn_args, &this.exec.subproc.child)) {
// FIXME: There's a race condition where this could change variants before spawnAsync returns.
.result => this.exec.subproc.child,
.err => |*e| {
this.exec = .none;
const msg = e.fmt();
defer bun.default_allocator.free(msg);
this.writeFailingError("{s}", .{msg});
this.writeFailingError("{}\n", .{e});
return;
},
};
Expand Down
31 changes: 8 additions & 23 deletions src/shell/shell.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,13 @@ pub const ShellErr = union(enum) {
};
}

pub fn fmt(this: @This()) []const u8 {
switch (this) {
.sys => {
const err = this.sys;
const str = std.fmt.allocPrint(bun.default_allocator, "bun: {s}: {}\n", .{ err.message, err.path }) catch bun.outOfMemory();
return str;
},
.custom => {
return std.fmt.allocPrint(bun.default_allocator, "bun: {s}\n", .{this.custom}) catch bun.outOfMemory();
},
.invalid_arguments => {
const str = std.fmt.allocPrint(bun.default_allocator, "bun: invalid arguments: {s}\n", .{this.invalid_arguments.val}) catch bun.outOfMemory();
return str;
},
.todo => {
const str = std.fmt.allocPrint(bun.default_allocator, "bun: TODO: {s}\n", .{this.invalid_arguments.val}) catch bun.outOfMemory();
return str;
},
}
pub fn format(this: *const ShellErr, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
return switch (this.*) {
.sys => |e| writer.print("bun: {s}: {}", .{ e.message, e.path }),
.custom => |msg| writer.print("bun: {s}", .{msg}),
.invalid_arguments => |args| writer.print("bun: invalid arguments: {s}", .{args.val}),
.todo => |msg| writer.print("bun: TODO: {s}", .{msg}),
};
}

pub fn throwJS(this: *const @This(), globalThis: *JSC.JSGlobalObject) bun.JSError {
Expand Down Expand Up @@ -111,21 +99,18 @@ pub const ShellErr = union(enum) {
switch (this) {
.sys => |err| {
bun.Output.prettyErrorln("<r><red>error<r>: Failed due to error: <b>bunsh: {s}: {}<r>", .{ err.message, err.path });
bun.Global.exit(1);
},
.custom => |custom| {
bun.Output.prettyErrorln("<r><red>error<r>: Failed due to error: <b>{s}<r>", .{custom});
bun.Global.exit(1);
},
.invalid_arguments => |invalid_arguments| {
bun.Output.prettyErrorln("<r><red>error<r>: Failed due to error: <b>bunsh: invalid arguments: {s}<r>", .{invalid_arguments.val});
bun.Global.exit(1);
},
.todo => |todo| {
bun.Output.prettyErrorln("<r><red>error<r>: Failed due to error: <b>TODO: {s}<r>", .{todo});
bun.Global.exit(1);
},
}
bun.Global.exit(1);
}

pub fn deinit(this: @This(), allocator: Allocator) void {
Expand Down