fix(install): call GetFinalPathNameByHandle on cwd for postinstall scripts (#12497)

This commit is contained in:
Dylan Conway
2024-07-10 21:09:14 -07:00
committed by GitHub
parent 25f7ef7338
commit 58483426cd
4 changed files with 48 additions and 6 deletions

View File

@@ -1513,8 +1513,10 @@ pub fn spawnProcessWindows(
const allocator = stack_allocator.get();
const loop = options.windows.loop.platformEventLoop().uv_loop;
const cwd = try allocator.dupeZ(u8, options.cwd);
defer allocator.free(cwd);
var cwd_buf: bun.PathBuffer = undefined;
@memcpy(cwd_buf[0..options.cwd.len], options.cwd);
cwd_buf[options.cwd.len] = 0;
const cwd = cwd_buf[0..options.cwd.len :0];
uv_process_options.cwd = cwd.ptr;

View File

@@ -106,7 +106,7 @@ pub const LifecycleScriptSubprocess = struct {
const manager = this.manager;
const original_script = this.scripts.items[next_script_index].?;
const cwd = bun.path.z(this.scripts.cwd, &cwd_z_buf);
const cwd = this.scripts.cwd;
const env = manager.env;
this.stdout.setParent(this);
this.stderr.setParent(this);

View File

@@ -2767,7 +2767,7 @@ pub const Package = extern struct {
items: [Lockfile.Scripts.names.len]?Lockfile.Scripts.Entry,
first_index: u8,
total: u8,
cwd: string,
cwd: stringZ,
package_name: string,
pub fn printScripts(
@@ -2955,7 +2955,7 @@ pub const Package = extern struct {
this: *const Package.Scripts,
lockfile: *Lockfile,
lockfile_buf: []const u8,
cwd: string,
cwd_: string,
package_name: string,
resolution_tag: Resolution.Tag,
add_node_gyp_rebuild_script: bool,
@@ -2963,11 +2963,26 @@ pub const Package = extern struct {
const allocator = lockfile.allocator;
const first_index, const total, const scripts = getScriptEntries(this, lockfile, lockfile_buf, resolution_tag, add_node_gyp_rebuild_script);
if (first_index != -1) {
var cwd_buf: if (Environment.isWindows) bun.PathBuffer else void = undefined;
const cwd = if (comptime !Environment.isWindows)
cwd_
else brk: {
@memcpy(cwd_buf[0..cwd_.len], cwd_);
cwd_buf[cwd_.len] = 0;
const cwd_handle = bun.openDirNoRenamingOrDeletingWindows(bun.invalid_fd, cwd_buf[0..cwd_.len :0]) catch break :brk cwd_;
var buf: bun.WPathBuffer = undefined;
const new_cwd = bun.windows.GetFinalPathNameByHandle(cwd_handle.fd, .{}, &buf) catch break :brk cwd_;
break :brk strings.convertUTF16toUTF8InBuffer(&cwd_buf, new_cwd) catch break :brk cwd_;
};
return .{
.items = scripts,
.first_index = @intCast(first_index),
.total = total,
.cwd = allocator.dupe(u8, cwd) catch bun.outOfMemory(),
.cwd = allocator.dupeZ(u8, cwd) catch bun.outOfMemory(),
.package_name = package_name,
};
}

View File

@@ -484,3 +484,28 @@ for (const glob of [true, false]) {
});
});
}
test("cwd in workspace script is not the symlink path on windows", async () => {
await Promise.all([
write(
join(packageDir, "package.json"),
JSON.stringify({
name: "foo",
workspaces: ["pkg1"],
}),
),
write(
join(packageDir, "pkg1", "package.json"),
JSON.stringify({
name: "pkg1",
scripts: {
postinstall: 'bun -e \'require("fs").writeFileSync("cwd", process.cwd())\'',
},
}),
),
]);
await runBunInstall(env, packageDir);
expect(await file(join(packageDir, "node_modules", "pkg1", "cwd")).text()).toBe(join(packageDir, "pkg1"));
});