correctly handle --cwd flag (#10187)

* actually change cwd on posix

* add test

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Georgijs
2024-04-11 19:22:29 -07:00
committed by GitHub
parent 545cb546cc
commit ff5ef512c7
2 changed files with 30 additions and 15 deletions

View File

@@ -414,20 +414,10 @@ pub const Arguments = struct {
cwd = brk: {
var outbuf: [bun.MAX_PATH_BYTES]u8 = undefined;
const out = bun.path.joinAbs(try bun.getcwd(&outbuf), .loose, cwd_);
// On POSIX, we don't actually call chdir() on the path
//
// On Windows, we do change the current directory.
// Not all system calls on Windows support passing a dirfd (and libuv entirely doesn't)
// So we have to do it the real way
if (comptime Environment.isWindows) {
var wbuf: bun.WPathBuffer = undefined;
bun.sys.chdir(bun.strings.toWPathNormalized(&wbuf, out)).unwrap() catch |err| {
Output.prettyErrorln("{}\n", .{err});
Global.exit(1);
};
}
bun.sys.chdir(out).unwrap() catch |err| {
Output.prettyErrorln("{}\n", .{err});
Global.exit(1);
};
break :brk try allocator.dupe(u8, out);
};
} else {

View File

@@ -1,7 +1,7 @@
import { file, spawn, spawnSync } from "bun";
import { afterEach, beforeEach, expect, it, describe } from "bun:test";
import { bunEnv, bunExe, bunEnv as env, isWindows } from "harness";
import { mkdtemp, realpath, rm, writeFile, exists } from "fs/promises";
import { mkdtemp, realpath, rm, writeFile, exists, mkdir } from "fs/promises";
import { tmpdir } from "os";
import { join } from "path";
import { readdirSorted } from "./dummy.registry";
@@ -419,3 +419,28 @@ import { prueba } from "pruebadfasdfasdkafasdyuif.js";
// The exit code will not be 1 if it panics.
expect(await exited).toBe(1);
});
it("should show the correct working directory when run with --cwd", async () => {
await mkdir(join(run_dir, "subdir"));
await writeFile(
join(run_dir, "subdir", "test.js"),
`
console.log(process.cwd());
`,
);
const res = Bun.spawn({
cmd: [bunExe(), "run", "--cwd", "subdir", "test.js"],
cwd: run_dir,
stdin: "ignore",
stdout: "pipe",
stderr: "pipe",
env: {
...env,
BUN_INSTALL_CACHE_DIR: join(run_dir, ".cache"),
},
});
// The exit code will not be 1 if it panics.
expect(await res.exited).toBe(0);
expect(await Bun.readableStreamToText(res.stdout)).toMatch(/subdir/);
});