diff --git a/src/cli.zig b/src/cli.zig index 924d497ff0..eaec4e593b 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -1521,14 +1521,9 @@ pub const Command = struct { } break :brk false; }; - if (print_help) { - Command.Tag.printHelp(.CreateCommand, true); - Global.exit(0); - return; - } var template_name_start: usize = 0; - var positionals: [2]string = undefined; + var positionals: [2]string = .{ "", "" }; var positional_i: usize = 0; @@ -1547,6 +1542,16 @@ pub const Command = struct { } } + if (print_help or + // "bun create --" + // "bun create -abc --" + positional_i == 0) + { + Command.Tag.printHelp(.CreateCommand, true); + Global.exit(0); + return; + } + const template_name = positionals[0]; // if template_name is "react" diff --git a/test/cli/install/bun-create.test.ts b/test/cli/install/bun-create.test.ts index 660077db52..97ecb89714 100644 --- a/test/cli/install/bun-create.test.ts +++ b/test/cli/install/bun-create.test.ts @@ -1,5 +1,5 @@ -import { spawn } from "bun"; -import { afterEach, beforeEach, expect, it } from "bun:test"; +import { spawn, spawnSync } from "bun"; +import { afterEach, beforeEach, expect, it, describe } from "bun:test"; import { bunExe, bunEnv as env } from "harness"; import { mkdtemp, realpath, rm, mkdir, stat } from "fs/promises"; import { tmpdir } from "os"; @@ -14,6 +14,28 @@ afterEach(async () => { await rm(x_dir, { force: true, recursive: true }); }); +describe("should not crash", async () => { + const args = [ + [bunExe(), "create", ""], + [bunExe(), "create", "--"], + [bunExe(), "create", "--", ""], + [bunExe(), "create", "--help"], + ]; + for (let cmd of args) { + it(JSON.stringify(cmd.slice(1).join(" ")), () => { + const { exitCode } = spawnSync({ + cmd, + cwd: x_dir, + stdout: "ignore", + stdin: "inherit", + stderr: "inherit", + env, + }); + expect(exitCode).toBe(cmd.length === 3 && cmd.at(-1) === "" ? 1 : 0); + }); + } +}); + it("should create selected template with @ prefix", async () => { const { stderr } = spawn({ cmd: [bunExe(), "create", "@quick-start/some-template"],