diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index 94821e2209..044bd7b1d4 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -830,7 +830,7 @@ pub const RunCommand = struct { this_transpiler.runEnvLoader(true) catch {}; } - this_transpiler.env.map.putDefault("npm_config_local_prefix", this_transpiler.fs.top_level_dir) catch unreachable; + this_transpiler.env.map.put("INIT_CWD", this_transpiler.fs.top_level_dir) catch unreachable; // we have no way of knowing what version they're expecting without running the node executable // running the node executable is too slow @@ -872,6 +872,10 @@ pub const RunCommand = struct { this_transpiler.env.map.putAssumeCapacity(key, v); } } + + this_transpiler.env.map.put("npm_config_local_prefix", package_json.source.path.name.dir) catch unreachable; + } else { + this_transpiler.env.map.put("npm_config_local_prefix", this_transpiler.fs.top_level_dir) catch unreachable; } return root_dir_info; @@ -1381,7 +1385,7 @@ pub const RunCommand = struct { var this_transpiler: transpiler.Transpiler = undefined; const root_dir_info = try configureEnvForRun(ctx, &this_transpiler, null, log_errors, false); try configurePathForRun(ctx, root_dir_info, &this_transpiler, &ORIGINAL_PATH, root_dir_info.abs_path, force_using_bun); - this_transpiler.env.map.put("npm_command", "run-script") catch unreachable; + this_transpiler.env.map.put("npm_command", "run") catch unreachable; if (!ctx.debug.loaded_bunfig) { bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", ctx, .RunCommand) catch {}; diff --git a/test/cli/install/bun-run.test.ts b/test/cli/install/bun-run.test.ts index d9cfbc96fa..3d457b01ff 100644 --- a/test/cli/install/bun-run.test.ts +++ b/test/cli/install/bun-run.test.ts @@ -12,7 +12,7 @@ import { tempDirWithFiles, tmpdirSync, } from "harness"; -import { join } from "path"; +import { join, sep } from "node:path"; let run_dir: string; @@ -505,7 +505,63 @@ it("$npm_command is accurate", async () => { }); expect(await p.exited).toBe(0); expect(await new Response(p.stderr).text()).toBe(`$ echo $npm_command\n`); - expect(await new Response(p.stdout).text()).toBe(`run-script\n`); + expect(await new Response(p.stdout).text()).toBe(`run\n`); +}); + +it("$INIT_CWD is accurate", async () => { + await writeFile( + join(run_dir, "package.json"), + `{ + "scripts": { + "sample": "echo $INIT_CWD", + "sample2": "cd subdir && INIT_CWD=OVERRIDE ${bunExe()} run sample", + }, + } + `, + ); + + const p = spawn({ + cmd: [bunExe(), "run", "sample"], + cwd: run_dir, + stdio: ["ignore", "pipe", "pipe"], + env: bunEnv, + }); + expect(await p.exited).toBe(0); + expect(await new Response(p.stderr).text()).toBe(`$ echo $INIT_CWD\n`); + expect(await new Response(p.stdout).text()).toBe(`${run_dir}\n`); + + await mkdir(join(run_dir, "subdir")); + const p2 = spawn({ + cmd: [bunExe(), "run", "sample2"], + cwd: run_dir, + stdio: ["ignore", "pipe", "pipe"], + env: bunEnv, + }); + expect(await p2.exited).toBe(0); + expect(await new Response(p2.stdout).text()).toBe(`${run_dir}${sep}subdir\n`); +}); + +it("npm_config_local_prefix is accurate", async () => { + await writeFile( + join(run_dir, "package.json"), + `{ + "scripts": { + "sample": "echo $npm_config_local_prefix", + }, + } + `, + ); + await mkdir(join(run_dir, "subdir")); + + const p = spawn({ + cmd: [bunExe(), "run", "sample"], + cwd: join(run_dir, "subdir"), + stdio: ["ignore", "pipe", "pipe"], + env: bunEnv, + }); + expect(await p.exited).toBe(0); + expect(await new Response(p.stderr).text()).toBe(`$ echo $npm_config_local_prefix\n`); + expect(await new Response(p.stdout).text()).toBe(`${run_dir}\n`); }); it("$npm_lifecycle_event is accurate", async () => {