fix(create): crash when running postinstall task with --no-install (#25616)

## Summary
- Fix segmentation fault in `bun create` when using `--no-install` with
a template that has a `bun-create.postinstall` task starting with "bun "
- The bug was caused by unconditionally slicing `argv[2..]` which
created an empty array when `npm_client` was null
- Added check for `npm_client != null` before slicing

## Reproduction
```bash
# Create template with bun-create.postinstall
mkdir -p ~/.bun-create/test-template
echo '{"name":"test","bun-create":{"postinstall":"bun install"}}' > ~/.bun-create/test-template/package.json

# This would crash before the fix
bun create test-template /tmp/my-app --no-install
```

## Test plan
- [x] Verified the reproduction case crashes before the fix
- [x] Verified the reproduction case works after the fix

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Dylan Conway
2025-12-19 23:17:51 -08:00
committed by GitHub
parent 99b0a16c33
commit fa983247b2
2 changed files with 29 additions and 1 deletions

View File

@@ -80,7 +80,7 @@ fn execTask(allocator: std.mem.Allocator, task_: string, cwd: string, _: string,
} }
} }
if (strings.startsWith(task, "bun ")) { if (npm_client != null and strings.startsWith(task, "bun ")) {
argv = argv[2..]; argv = argv[2..];
} }

View File

@@ -144,3 +144,31 @@ for (const repo of ["https://github.com/dylan-conway/create-test", "github.com/d
expect(await exited).toBe(0); expect(await exited).toBe(0);
}, 20_000); }, 20_000);
} }
it("should not crash with --no-install and bun-create.postinstall starting with 'bun '", async () => {
const bunCreateDir = join(x_dir, "bun-create");
const testTemplate = "postinstall-test";
await Bun.write(
join(bunCreateDir, testTemplate, "package.json"),
JSON.stringify({
name: "test",
"bun-create": {
postinstall: "bun install",
},
}),
);
const { exited, stderr, stdout } = spawn({
cmd: [bunExe(), "create", testTemplate, join(x_dir, "dest"), "--no-install"],
cwd: x_dir,
stdout: "pipe",
stdin: "ignore",
stderr: "pipe",
env: { ...env, BUN_CREATE_DIR: bunCreateDir },
});
const [err, _out, exitCode] = await Promise.all([stderr.text(), stdout.text(), exited]);
expect(err).not.toContain("error:");
expect(exitCode).toBe(0);
});