Skip to content

Commit

Permalink
forward IPC to child process if running package script (oven-sh#13934)
Browse files Browse the repository at this point in the history
Co-authored-by: Jarred Sumner <[email protected]>
  • Loading branch information
snoglobe and Jarred-Sumner committed Sep 14, 2024
1 parent 3c2e798 commit b9a5e44
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/bun.js/api/bun/process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ pub const PosixSpawnOptions = struct {
stdin: Stdio = .ignore,
stdout: Stdio = .ignore,
stderr: Stdio = .ignore,
ipc: ?bun.FileDescriptor = null,
extra_fds: []const Stdio = &.{},
cwd: []const u8 = "",
detached: bool = false,
Expand Down Expand Up @@ -1031,6 +1032,7 @@ pub const WindowsSpawnOptions = struct {
stdin: Stdio = .ignore,
stdout: Stdio = .ignore,
stderr: Stdio = .ignore,
ipc: ?bun.FileDescriptor = null,
extra_fds: []const Stdio = &.{},
cwd: []const u8 = "",
detached: bool = false,
Expand Down Expand Up @@ -1077,6 +1079,7 @@ pub const PosixSpawnResult = struct {
stdin: ?bun.FileDescriptor = null,
stdout: ?bun.FileDescriptor = null,
stderr: ?bun.FileDescriptor = null,
ipc: ?bun.FileDescriptor = null,
extra_pipes: std.ArrayList(bun.FileDescriptor) = std.ArrayList(bun.FileDescriptor).init(bun.default_allocator),

memfds: [3]bool = .{ false, false, false },
Expand Down Expand Up @@ -1260,6 +1263,11 @@ pub fn spawnProcessPosix(
attr.set(@intCast(flags)) catch {};
attr.resetSignals() catch {};

if (options.ipc) |ipc| {
try actions.inherit(ipc);
spawned.ipc = ipc;
}

const stdio_options: [3]PosixSpawnOptions.Stdio = .{ options.stdin, options.stdout, options.stderr };
const stdios: [3]*?bun.FileDescriptor = .{ &spawned.stdin, &spawned.stdout, &spawned.stderr };

Expand Down Expand Up @@ -1765,6 +1773,7 @@ pub const sync = struct {
stdin: Stdio = .ignore,
stdout: Stdio = .inherit,
stderr: Stdio = .inherit,
ipc: ?bun.FileDescriptor = null,
cwd: []const u8 = "",
detached: bool = false,

Expand Down Expand Up @@ -1799,6 +1808,8 @@ pub const sync = struct {
.stdin = this.stdin.toStdio(),
.stdout = this.stdout.toStdio(),
.stderr = this.stderr.toStdio(),
.ipc = this.ipc,

.cwd = this.cwd,
.detached = this.detached,
.use_execve_on_macos = this.use_execve_on_macos,
Expand Down
7 changes: 7 additions & 0 deletions src/cli/run_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ pub const RunCommand = struct {
combined_script,
};

const ipc_fd = if (!Environment.isWindows) blk: {
const node_ipc_fd = bun.getenvZ("NODE_CHANNEL_FD") orelse break :blk null;
const fd = std.fmt.parseInt(u32, node_ipc_fd, 10) catch break :blk null;
break :blk bun.toFD(@as(i32, @intCast(fd)));
} else null; // TODO: implement on Windows

const spawn_result = switch ((bun.spawnSync(&.{
.argv = &argv,
.argv0 = shell_bin.ptr,
Expand All @@ -353,6 +359,7 @@ pub const RunCommand = struct {
.stderr = .inherit,
.stdout = .inherit,
.stdin = .inherit,
.ipc = ipc_fd,

.windows = if (Environment.isWindows) .{
.loop = JSC.EventLoopHandle.init(JSC.MiniEventLoop.initGlobal(env)),
Expand Down
28 changes: 28 additions & 0 deletions test/js/bun/spawn/bun-ipc-inherit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { spawn, spawnSync, env } from "bun";
import fs from "node:fs/promises";
import { describe, expect, it } from "bun:test";
import { bunExe, isWindows } from "harness";
import path from "path";

it.todoIf(isWindows)("spawning a bun package script should inherit the ipc fd", async () => {
await fs.writeFile(
path.join(process.cwd(), "package.json"),
JSON.stringify({
scripts: {
test: `${bunExe()} -e 'process.send("hello")'`,
},
}),
);

let testMessage;

const child = spawn([bunExe(), "run", "test"], {
ipc: message => {
testMessage = message;
},
stdio: ["inherit", "inherit", "inherit"],
});

await child.exited;
expect(testMessage).toBe("hello");
});

0 comments on commit b9a5e44

Please sign in to comment.