diff --git a/scripts/build-binary.mjs b/scripts/build-binary.mjs index 9e715456..94aae2c4 100644 --- a/scripts/build-binary.mjs +++ b/scripts/build-binary.mjs @@ -74,9 +74,24 @@ try { process.exit(1); } +// By default a compiled executable loads `.env`, `.env.local` and `bunfig.toml` +// from its working directory at startup. That directory is the user's project: +// its dotenv values would reach Claude and every Bash command as real +// environment variables (overriding the project's own `.env.test`), and a +// `bunfig.toml` that fails to parse would stop the agent from starting. const bun = spawnSync( "bun", - ["build", "src/binary-entry.ts", "--compile", "--target", target, "--outfile", outfile], + [ + "build", + "src/binary-entry.ts", + "--compile", + "--no-compile-autoload-dotenv", + "--no-compile-autoload-bunfig", + "--target", + target, + "--outfile", + outfile, + ], { cwd: repoRoot, stdio: "inherit", shell: process.platform === "win32" }, ); if (bun.error) { diff --git a/scripts/smoke-binary.mjs b/scripts/smoke-binary.mjs index f2e27445..2f518049 100644 --- a/scripts/smoke-binary.mjs +++ b/scripts/smoke-binary.mjs @@ -58,8 +58,9 @@ function check(ok, label, detail) { // ---------------------------------------------------------------- basic -function runSync(args, [cmd, ...cmdArgs] = [command, ...commandArgs]) { +function runSync(args, [cmd, ...cmdArgs] = [command, ...commandArgs], cwd) { const result = spawnSync(cmd, [...cmdArgs, ...args], { + cwd, encoding: "utf8", timeout: 60_000, }); @@ -101,6 +102,26 @@ if (positionals.length === 0) { "without claude next to it, says the CLI is missing", missing.stdout || missing.error, ); + + // Started in a project directory, the executable must not read the + // project's dotenv files or bunfig.toml. Node stands in for `claude`, to + // print the environment the CLI is spawned with. + copyFileSync(process.execPath, join(scratch, `claude${exe}`)); + const project = join(scratch, "project"); + mkdirSync(project); + writeFileSync(join(project, ".env"), "ACP_SMOKE_DOTENV=leaked\n"); + writeFileSync(join(project, ".env.local"), "ACP_SMOKE_DOTENV_LOCAL=leaked\n"); + writeFileSync(join(project, "bunfig.toml"), "= not toml\n"); + const probe = runSync( + ["--cli", "-p", "[process.env.ACP_SMOKE_DOTENV, process.env.ACP_SMOKE_DOTENV_LOCAL].join()"], + [alone], + project, + ); + check( + probe.stdout === ",", + "ignores .env, .env.local and bunfig.toml in the working directory", + probe.stdout || probe.error, + ); } finally { rmSync(scratch, { recursive: true, force: true }); }