From bb13798d9802d26770d8467a200022a2b91f7afd Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Tue, 21 May 2024 17:25:40 -0700 Subject: [PATCH] fix(install): workspace version added to package.json (#11241) --- src/install/install.zig | 10 +++-- test/cli/install/bun-workspaces.test.ts | 55 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/install/install.zig b/src/install/install.zig index 5bd67da7b2822..3f5334fd624a2 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -7088,14 +7088,16 @@ pub const PackageManager = struct { }, } } - break :brk null; + + break :brk try allocator.dupe(u8, request.version.literal.slice(request.version_buf)); }, .uninitialized => switch (request.version.tag) { .uninitialized => try allocator.dupe(u8, latest), - else => null, + else => try allocator.dupe(u8, request.version.literal.slice(request.version_buf)), }, - else => null, - } orelse try allocator.dupe(u8, request.version.literal.slice(request.version_buf)); + .workspace => try allocator.dupe(u8, "workspace:*"), + else => try allocator.dupe(u8, request.version.literal.slice(request.version_buf)), + }; } } } diff --git a/test/cli/install/bun-workspaces.test.ts b/test/cli/install/bun-workspaces.test.ts index 9fabb8e23cc7e..51eea163f0188 100644 --- a/test/cli/install/bun-workspaces.test.ts +++ b/test/cli/install/bun-workspaces.test.ts @@ -2,6 +2,7 @@ import { spawnSync } from "bun"; import { bunExe, bunEnv as env, runBunInstall, tmpdirSync, toMatchNodeModulesAt } from "harness"; import { join } from "path"; import { writeFileSync, mkdirSync, rmSync } from "fs"; +import { writeFile, mkdir } from "fs/promises"; import { beforeEach, test, expect } from "bun:test"; import { install_test_helpers } from "bun:internal-for-testing"; const { parseLockfile } = install_test_helpers; @@ -147,3 +148,57 @@ test("dependency on same name as workspace and dist-tag", async () => { expect(lockfile).toMatchNodeModulesAt(packageDir); expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual(["", "3 packages installed"]); }); + +test("adding workspace in workspace edits package.json with correct version (workspace:*)", async () => { + await writeFile( + join(packageDir, "package.json"), + JSON.stringify({ + name: "foo", + workspaces: ["packages/*", "apps/*"], + }), + ); + + await mkdir(join(packageDir, "packages", "pkg1"), { recursive: true }); + await writeFile( + join(packageDir, "packages", "pkg1", "package.json"), + JSON.stringify({ + name: "pkg1", + version: "1.0.0", + }), + ); + + await mkdir(join(packageDir, "apps", "pkg2"), { recursive: true }); + await writeFile( + join(packageDir, "apps", "pkg2", "package.json"), + JSON.stringify({ + name: "pkg2", + version: "1.0.0", + }), + ); + + const { stdout, exited } = Bun.spawn({ + cmd: [bunExe(), "add", "pkg2@workspace:*"], + cwd: join(packageDir, "packages", "pkg1"), + stdout: "pipe", + stderr: "inherit", + env, + }); + const out = await Bun.readableStreamToText(stdout); + + expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([ + "", + "installed pkg2@workspace:apps/pkg2", + "", + "2 packages installed", + ]); + + expect(await exited).toBe(0); + + expect(await Bun.file(join(packageDir, "packages", "pkg1", "package.json")).json()).toEqual({ + name: "pkg1", + version: "1.0.0", + dependencies: { + pkg2: "workspace:*", + }, + }); +});