Skip to content
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
24 changes: 19 additions & 5 deletions src/jsc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1592,11 +1592,25 @@ impl FromJsEnum for bun_sys::SignalCode {
s.deref();
match hit {
Some(code) => Ok(code),
// Zig builds the `'SIGHUP', 'SIGINT' or ...` list at comptime; at
// 31 variants the runtime port keeps the message terse.
None => Err(global.throw_invalid_arguments(format_args!(
"{property_name} must be one of the SignalCode names"
))),
None => {
// Mirror Zig's comptime `toEnumFromMap` list
// (`'SIGHUP', 'SIGINT', … or 'SIGSYS'`), built from the
// canonical signal X-macro so names are never re-spelled.
let names = &bun_core::SIGNAL_NAMES[1..];
let mut one_of = std::string::String::from("'");
for (i, entry) in names.iter().enumerate() {
one_of.push_str(entry);
one_of.push('\'');
if i < names.len() - 2 {
one_of.push_str(", '");
} else if i == names.len() - 2 {
one_of.push_str(" or '");
}
}
Err(global.throw_invalid_arguments(format_args!(
"{property_name} must be one of {one_of}"
)))
}
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/js/bun/spawn/spawn-kill-signal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,37 @@ describe("subprocess.kill", () => {
expect(proc.signalCode).toBe("SIGTERM");
});
}

test("invalid signal name lists the valid signal names", async () => {
const proc = Bun.spawn({
cmd: [shellExe(), "-c", "sleep 1000"],
stdio: ["inherit", "inherit", "inherit"],
});

let err: any;
try {
proc.kill("SIGGOD");
} catch (e) {
err = e;
}

expect(err).toBeInstanceOf(TypeError);
expect(err.code).toBe("ERR_INVALID_ARG_TYPE");
// The message must enumerate the real signal names, not a static
// "the SignalCode names" placeholder (regressed in the Rust port).
expect(err.message).toContain("'SIGHUP'");
expect(err.message).toContain("'SIGTERM'");
expect(err.message).toContain("'SIGKILL'");
expect(err.message).toContain("or 'SIGSYS'");
expect(err.message).not.toContain("the SignalCode names");

const { promise, resolve, reject } = Promise.withResolvers();
proc.exited.then(resolve, reject);
proc.kill();
await promise;

expect(proc.exitCode).toBe(null);
expect(proc.signalCode).toBe("SIGTERM");
});
});
});
Loading