aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2023-08-31 03:35:28 +0300
committerGravatar GitHub <noreply@github.com> 2023-08-30 17:35:28 -0700
commit037463fc48a96cfdacb647ba44e021abd5e4769e (patch)
tree2c8f39f90e7ecb9c013981f26a494033a24b27c0
parent04215e2f3a8c11d9e2b5f336eee40e6f7ebf11c9 (diff)
downloadbun-037463fc48a96cfdacb647ba44e021abd5e4769e.tar.gz
bun-037463fc48a96cfdacb647ba44e021abd5e4769e.tar.zst
bun-037463fc48a96cfdacb647ba44e021abd5e4769e.zip
[install] fix stale root life-cycle script in lockfile (#4411)
fixes #4319
-rw-r--r--src/install/install.zig25
-rw-r--r--test/cli/install/bun-install.test.ts170
2 files changed, 187 insertions, 8 deletions
diff --git a/src/install/install.zig b/src/install/install.zig
index ee23fa427..768113d35 100644
--- a/src/install/install.zig
+++ b/src/install/install.zig
@@ -7524,13 +7524,6 @@ pub const PackageManager = struct {
package_json_source,
Features.main,
);
- if (!root.scripts.filled) {
- maybe_root.scripts.enqueue(
- manager.lockfile,
- lockfile.buffers.string_bytes.items,
- strings.withoutTrailingSlash(Fs.FileSystem.instance.top_level_dir),
- );
- }
var mapping = try manager.lockfile.allocator.alloc(PackageID, maybe_root.dependencies.len);
@memset(mapping, invalid_package_id);
@@ -7565,6 +7558,8 @@ pub const PackageManager = struct {
new_dep.count(lockfile.buffers.string_bytes.items, *Lockfile.StringBuilder, builder);
}
+ maybe_root.scripts.count(lockfile.buffers.string_bytes.items, *Lockfile.StringBuilder, builder);
+
const off = @as(u32, @truncate(manager.lockfile.buffers.dependencies.items.len));
const len = @as(u32, @truncate(new_dependencies.len));
var packages = manager.lockfile.packages.slice();
@@ -7598,6 +7593,12 @@ pub const PackageManager = struct {
}
}
+ manager.lockfile.packages.items(.scripts)[0] = maybe_root.scripts.clone(
+ lockfile.buffers.string_bytes.items,
+ *Lockfile.StringBuilder,
+ builder,
+ );
+
builder.clamp();
// Split this into two passes because the below may allocate memory or invalidate pointers
@@ -7619,6 +7620,16 @@ pub const PackageManager = struct {
}
}
}
+
+ if (manager.summary.update > 0) root.scripts = .{};
+ }
+
+ if (!root.scripts.filled) {
+ maybe_root.scripts.enqueue(
+ manager.lockfile,
+ lockfile.buffers.string_bytes.items,
+ strings.withoutTrailingSlash(Fs.FileSystem.instance.top_level_dir),
+ );
}
}
},
diff --git a/test/cli/install/bun-install.test.ts b/test/cli/install/bun-install.test.ts
index a530cac38..a98c22e3b 100644
--- a/test/cli/install/bun-install.test.ts
+++ b/test/cli/install/bun-install.test.ts
@@ -857,7 +857,141 @@ it("should handle life-cycle scripts during re-installation", async () => {
await access(join(package_dir, "bun.lockb"));
});
-it("should use updated life-cycle scripts during re-installation", async () => {
+it("should use updated life-cycle scripts in root during re-installation", async () => {
+ await writeFile(
+ join(package_dir, "package.json"),
+ JSON.stringify({
+ name: "Foo",
+ scripts: {
+ install: [bunExe(), "foo.js"].join(" "),
+ },
+ workspaces: ["bar"],
+ }),
+ );
+ await writeFile(join(package_dir, "foo.js"), 'console.log("[scripts:run] Foo");');
+ await mkdir(join(package_dir, "bar"));
+ await writeFile(
+ join(package_dir, "bar", "package.json"),
+ JSON.stringify({
+ name: "Bar",
+ scripts: {
+ preinstall: [bunExe(), "bar.js"].join(" "),
+ },
+ }),
+ );
+ await writeFile(join(package_dir, "bar", "bar.js"), 'console.log("[scripts:run] Bar");');
+ const {
+ stdout: stdout1,
+ stderr: stderr1,
+ exited: exited1,
+ } = spawn({
+ cmd: [bunExe(), "install"],
+ cwd: package_dir,
+ stdout: null,
+ stdin: "pipe",
+ stderr: "pipe",
+ env,
+ });
+ expect(stderr1).toBeDefined();
+ const err1 = await new Response(stderr1).text();
+ expect(err1).not.toContain("error:");
+ expect(err1).toContain("Saved lockfile");
+ expect(stdout1).toBeDefined();
+ const out1 = await new Response(stdout1).text();
+ expect(out1.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([
+ "[scripts:run] Bar",
+ " + Bar@workspace:bar",
+ "[scripts:run] Foo",
+ "",
+ " 1 packages installed",
+ ]);
+ expect(await exited1).toBe(0);
+ expect(requested).toBe(0);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".cache", "Bar"]);
+ expect(await readlink(join(package_dir, "node_modules", "Bar"))).toBe(join("..", "bar"));
+ await access(join(package_dir, "bun.lockb"));
+ // Perform `bun install` with outdated lockfile
+ await rm(join(package_dir, "node_modules"), { force: true, recursive: true });
+ await writeFile(
+ join(package_dir, "package.json"),
+ JSON.stringify({
+ name: "Foo",
+ scripts: {
+ install: [bunExe(), "moo.js"].join(" "),
+ postinstall: [bunExe(), "foo.js"].join(" "),
+ },
+ workspaces: ["bar"],
+ }),
+ );
+ await writeFile(join(package_dir, "moo.js"), 'console.log("[scripts:run] Moo");');
+ const {
+ stdout: stdout2,
+ stderr: stderr2,
+ exited: exited2,
+ } = spawn({
+ cmd: [bunExe(), "install"],
+ cwd: package_dir,
+ stdout: null,
+ stdin: "pipe",
+ stderr: "pipe",
+ env,
+ });
+ expect(stderr2).toBeDefined();
+ const err2 = await new Response(stderr2).text();
+ expect(err2).not.toContain("error:");
+ expect(err2).toContain("Saved lockfile");
+ expect(stdout2).toBeDefined();
+ const out2 = await new Response(stdout2).text();
+ expect(out2.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([
+ "[scripts:run] Bar",
+ " + Bar@workspace:bar",
+ "[scripts:run] Moo",
+ "[scripts:run] Foo",
+ "",
+ " 1 packages installed",
+ ]);
+ expect(await exited2).toBe(0);
+ expect(requested).toBe(0);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".cache", "Bar"]);
+ expect(await readlink(join(package_dir, "node_modules", "Bar"))).toBe(join("..", "bar"));
+ await access(join(package_dir, "bun.lockb"));
+ // Perform `bun install --production` with lockfile from before
+ const bun_lockb = await file(join(package_dir, "bun.lockb")).arrayBuffer();
+ await rm(join(package_dir, "node_modules"), { force: true, recursive: true });
+ const {
+ stdout: stdout3,
+ stderr: stderr3,
+ exited: exited3,
+ } = spawn({
+ cmd: [bunExe(), "install", "--production"],
+ cwd: package_dir,
+ stdout: null,
+ stdin: "pipe",
+ stderr: "pipe",
+ env,
+ });
+ expect(stderr3).toBeDefined();
+ const err3 = await new Response(stderr3).text();
+ expect(err3).not.toContain("error:");
+ expect(err3).not.toContain("Saved lockfile");
+ expect(stdout3).toBeDefined();
+ const out3 = await new Response(stdout3).text();
+ expect(out3.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([
+ "[scripts:run] Bar",
+ " + Bar@workspace:bar",
+ "[scripts:run] Moo",
+ "[scripts:run] Foo",
+ "",
+ " 1 packages installed",
+ ]);
+ expect(await exited3).toBe(0);
+ expect(requested).toBe(0);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual(["Bar"]);
+ expect(await readlink(join(package_dir, "node_modules", "Bar"))).toBe(join("..", "bar"));
+ expect(await file(join(package_dir, "bun.lockb")).arrayBuffer()).toEqual(bun_lockb);
+});
+
+it("should use updated life-cycle scripts in dependency during re-installation", async () => {
await writeFile(
join(package_dir, "package.json"),
JSON.stringify({
@@ -954,6 +1088,40 @@ it("should use updated life-cycle scripts during re-installation", async () => {
expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".cache", "Bar"]);
expect(await readlink(join(package_dir, "node_modules", "Bar"))).toBe(join("..", "bar"));
await access(join(package_dir, "bun.lockb"));
+ // Perform `bun install --production` with lockfile from before
+ const bun_lockb = await file(join(package_dir, "bun.lockb")).arrayBuffer();
+ await rm(join(package_dir, "node_modules"), { force: true, recursive: true });
+ const {
+ stdout: stdout3,
+ stderr: stderr3,
+ exited: exited3,
+ } = spawn({
+ cmd: [bunExe(), "install", "--production"],
+ cwd: package_dir,
+ stdout: null,
+ stdin: "pipe",
+ stderr: "pipe",
+ env,
+ });
+ expect(stderr3).toBeDefined();
+ const err3 = await new Response(stderr3).text();
+ expect(err3).not.toContain("error:");
+ expect(err3).not.toContain("Saved lockfile");
+ expect(stdout3).toBeDefined();
+ const out3 = await new Response(stdout3).text();
+ expect(out3.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([
+ "[scripts:run] Baz",
+ " + Bar@workspace:bar",
+ "[scripts:run] Foo",
+ "[scripts:run] Bar",
+ "",
+ " 1 packages installed",
+ ]);
+ expect(await exited3).toBe(0);
+ expect(requested).toBe(0);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual(["Bar"]);
+ expect(await readlink(join(package_dir, "node_modules", "Bar"))).toBe(join("..", "bar"));
+ expect(await file(join(package_dir, "bun.lockb")).arrayBuffer()).toEqual(bun_lockb);
});
it("should ignore workspaces within workspaces", async () => {