From 6c60711117e5ec1c830958ee2710f0d47bfc3023 Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Wed, 6 Mar 2024 19:11:09 -0800 Subject: [PATCH 1/2] preinstall and install --- src/install/install.zig | 4 +- src/install/lockfile.zig | 39 ++++++++++--------- .../registry/bun-install-registry.test.ts | 6 +-- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/install/install.zig b/src/install/install.zig index 79f87ab9b276e..84ac58f690c90 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -8367,7 +8367,7 @@ pub const PackageManager = struct { if (scripts.hasAny()) { const add_node_gyp_rebuild_script = if (this.lockfile.hasTrustedDependency(folder_name) and scripts.install.isEmpty() and - scripts.postinstall.isEmpty()) + scripts.preinstall.isEmpty()) brk: { const binding_dot_gyp_path = Path.joinAbsStringZ( this.node_modules_folder_path.items, @@ -9450,7 +9450,7 @@ pub const PackageManager = struct { .auto, ); if (root.scripts.hasAny()) { - const add_node_gyp_rebuild_script = root.scripts.install.isEmpty() and root.scripts.postinstall.isEmpty() and Syscall.exists(binding_dot_gyp_path); + const add_node_gyp_rebuild_script = root.scripts.install.isEmpty() and root.scripts.preinstall.isEmpty() and Syscall.exists(binding_dot_gyp_path); manager.root_lifecycle_scripts = root.scripts.enqueue( manager.lockfile, diff --git a/src/install/lockfile.zig b/src/install/lockfile.zig index a4b212897c9e9..7eb06cf80f086 100644 --- a/src/install/lockfile.zig +++ b/src/install/lockfile.zig @@ -2405,8 +2405,25 @@ pub const Package = extern struct { var counter: u8 = 0; if (add_node_gyp_rebuild_script) { - // missing install and postinstall, only need to check preinstall - if (!this.preinstall.isEmpty()) { + { + script_index += 1; + const entry: Lockfile.Scripts.Entry = .{ + .cwd = cwd orelse brk: { + cwd = lockfile.allocator.dupe(u8, _cwd) catch unreachable; + break :brk cwd.?; + }, + .script = lockfile.allocator.dupe(u8, "node-gyp rebuild") catch unreachable, + .package_name = package_name, + }; + if (first_script_index == -1) first_script_index = @intCast(script_index); + scripts[script_index] = entry; + script_index += 1; + lockfile.scripts.install.append(lockfile.allocator, entry) catch unreachable; + counter += 1; + } + + // missing install and preinstall, only need to check postinstall + if (!this.postinstall.isEmpty()) { const entry: Lockfile.Scripts.Entry = .{ .cwd = cwd orelse brk: { cwd = lockfile.allocator.dupe(u8, _cwd) catch unreachable; @@ -2417,24 +2434,10 @@ pub const Package = extern struct { }; if (first_script_index == -1) first_script_index = @intCast(script_index); scripts[script_index] = entry; - lockfile.scripts.preinstall.append(lockfile.allocator, entry) catch unreachable; + lockfile.scripts.postinstall.append(lockfile.allocator, entry) catch unreachable; counter += 1; } script_index += 1; - - const entry: Lockfile.Scripts.Entry = .{ - .cwd = cwd orelse brk: { - cwd = lockfile.allocator.dupe(u8, _cwd) catch unreachable; - break :brk cwd.?; - }, - .script = lockfile.allocator.dupe(u8, "node-gyp rebuild") catch unreachable, - .package_name = package_name, - }; - if (first_script_index == -1) first_script_index = @intCast(script_index); - scripts[script_index] = entry; - script_index += 2; - lockfile.scripts.install.append(lockfile.allocator, entry) catch unreachable; - counter += 1; } else { const install_scripts = .{ "preinstall", @@ -2608,7 +2611,7 @@ pub const Package = extern struct { const add_node_gyp_rebuild_script = if (lockfile.hasTrustedDependency(folder_name) and this.install.isEmpty() and - this.postinstall.isEmpty()) + this.preinstall.isEmpty()) brk: { const binding_dot_gyp_path = Path.joinAbsStringZ( cwd, diff --git a/test/cli/install/registry/bun-install-registry.test.ts b/test/cli/install/registry/bun-install-registry.test.ts index c55ae499d1426..6051ab4c68fd4 100644 --- a/test/cli/install/registry/bun-install-registry.test.ts +++ b/test/cli/install/registry/bun-install-registry.test.ts @@ -2586,7 +2586,7 @@ for (const forceWaiterThread of [false, true]) { expect(await exists(join(packageDir, "build.node"))).toBeTrue(); }); - test("auto node-gyp scripts work when scripts exists other than `install` and `postinstall`", async () => { + test("auto node-gyp scripts work when scripts exists other than `install` and `preinstall`", async () => { await writeFile( join(packageDir, "package.json"), JSON.stringify({ @@ -2596,7 +2596,7 @@ for (const forceWaiterThread of [false, true]) { "node-gyp": "1.5.0", }, scripts: { - preinstall: "exit 0", + postinstall: "exit 0", prepare: "exit 0", postprepare: "exit 0", }, @@ -2629,7 +2629,7 @@ for (const forceWaiterThread of [false, true]) { expect(await exists(join(packageDir, "build.node"))).toBeTrue(); }); - for (const script of ["install", "postinstall"]) { + for (const script of ["install", "preinstall"]) { test(`does not add auto node-gyp script when ${script} script exists`, async () => { const packageJSON: any = { name: "foo", From ffc0f0a95cf4582f55239428ed037d7aad3b1e06 Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Wed, 6 Mar 2024 19:32:56 -0800 Subject: [PATCH 2/2] comment --- src/install/lockfile.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/install/lockfile.zig b/src/install/lockfile.zig index 7eb06cf80f086..1043dcd778a7f 100644 --- a/src/install/lockfile.zig +++ b/src/install/lockfile.zig @@ -2341,6 +2341,14 @@ pub const Package = extern struct { meta: Meta = .{}, bin: Bin = .{}, + + /// If any of these scripts run, they will run in order: + /// 1. preinstall + /// 2. install + /// 3. postinstall + /// 4. preprepare + /// 5. prepare + /// 6. postprepare scripts: Package.Scripts = .{}, pub const Scripts = extern struct {