diff --git a/src/install/install.zig b/src/install/install.zig index be17cda808..88821ffbdc 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -2592,22 +2592,30 @@ pub const PackageManager = struct { }; defer node_gyp_file.close(); - var bytes: string = switch (Environment.os) { - else => "#!/usr/bin/env node\nrequire(\"child_process\").spawnSync(\"bun\",[\"x\",\"node-gyp\",...process.argv.slice(2)],{stdio:\"inherit\"})", - .windows => "@node -e \"require('child_process').spawnSync('bun',['x','node-gyp',...process.argv.slice(2)],{stdio:'inherit'})\"", + const shebang = switch (Environment.os) { + .windows => + \\0 + \\#!/usr/bin/env node + \\ + , + }; + const content = + \\const child_process = require("child_process"); + \\child_process.spawnSync("bun", ["x", "node-gyp", ...process.argv.slice(2)], { stdio: "inherit" }); + \\ + ; + + node_gyp_file.writeAll(shebang ++ content) catch |err| { + Output.prettyErrorln("error: {s} writing to " ++ file_name ++ " file", .{@errorName(err)}); + Global.crash(); }; - var index: usize = 0; - while (index < bytes.len) { - switch (bun.sys.write(bun.toFD(node_gyp_file.handle), bytes[index..])) { - .result => |written| { - index += written; - }, - .err => |err| { - Output.prettyErrorln("error: {s} writing to " ++ file_name ++ " file", .{@tagName(err.getErrno())}); - Global.crash(); - }, - } - } // Add our node-gyp tempdir to the path const existing_path = this.env.get("PATH") orelse ""; diff --git a/test/cli/install/registry/bun-install-registry.test.ts b/test/cli/install/registry/bun-install-registry.test.ts index 4c3259c629..fd76e8ba87 100644 --- a/test/cli/install/registry/bun-install-registry.test.ts +++ b/test/cli/install/registry/bun-install-registry.test.ts @@ -4447,12 +4447,10 @@ test("it should be able to find binary in node_modules/.bin from parent director await cp(join(packageDir, "bunfig.toml"), join(packageDir, "morePackageDir", "bunfig.toml")); - await await writeFile( + await writeShebangScript( join(packageDir, "node_modules", ".bin", "missing-bin"), - `#!/usr/bin/env node -require("fs").writeFileSync("missing-bin.txt", "missing-bin@WHAT"); -`, - { mode: 0o777 }, + "node", + `require("fs").writeFileSync("missing-bin.txt", "missing-bin@WHAT");`, ); const { stdout, stderr, exited } = spawn({ diff --git a/test/harness.ts b/test/harness.ts index f04957bbde..5553a99bc8 100644 --- a/test/harness.ts +++ b/test/harness.ts @@ -1,7 +1,7 @@ import { gc as bunGC, unsafe, which } from "bun"; import { describe, test, expect, afterAll, beforeAll } from "bun:test"; -import { readlink, readFile } from "fs/promises"; -import { isAbsolute } from "path"; +import { readlink, readFile, writeFile } from "fs/promises"; +import { isAbsolute, sep } from "path"; import { openSync, closeSync } from "node:fs"; export const isMacOS = process.platform === "darwin"; @@ -557,3 +557,21 @@ export function toTOMLString(opts: object) { } return ret; } + +const shebang_posix = (program: string) => `#!/usr/bin/env ${program} +`; + +const shebang_windows = (program: string) => `0