Skip to content

Commit

Permalink
Merge branch 'main' into don/feat/object-create-pojo
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac authored Dec 13, 2024
2 parents f374390 + 8b3b144 commit e008425
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 8 deletions.
42 changes: 35 additions & 7 deletions src/install/bun.lock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ pub const Stringifier = struct {
const resolution_buf = lockfile.buffers.resolutions.items;
const pkgs = lockfile.packages.slice();
const pkg_dep_lists: []DependencySlice = pkgs.items(.dependencies);
const pkg_resolution: []Resolution = pkgs.items(.resolution);
const pkg_resolutions: []Resolution = pkgs.items(.resolution);
const pkg_names: []String = pkgs.items(.name);
const pkg_name_hashes: []PackageNameHash = pkgs.items(.name_hash);
const pkg_metas: []BinaryLockfile.Package.Meta = pkgs.items(.meta);
Expand Down Expand Up @@ -463,15 +463,43 @@ pub const Stringifier = struct {
lockfile.workspace_versions,
&optional_peers_buf,
);
for (0..pkgs.len) |pkg_id| {
const res = pkg_resolution[pkg_id];

var workspace_sort_buf: std.ArrayListUnmanaged(PackageID) = .{};
defer workspace_sort_buf.deinit(allocator);

for (0..pkgs.len) |_pkg_id| {
const pkg_id: PackageID = @intCast(_pkg_id);
const res = pkg_resolutions[pkg_id];
if (res.tag != .workspace) continue;
try writer.writeAll(",\n");
try workspace_sort_buf.append(allocator, pkg_id);
}

const Sorter = struct {
string_buf: string,
res_buf: []const Resolution,

pub fn isLessThan(this: @This(), l: PackageID, r: PackageID) bool {
const l_res = this.res_buf[l];
const r_res = this.res_buf[r];
return l_res.value.workspace.order(&r_res.value.workspace, this.string_buf, this.string_buf) == .lt;
}
};

std.sort.pdq(
PackageID,
workspace_sort_buf.items,
Sorter{ .string_buf = buf, .res_buf = pkg_resolutions },
Sorter.isLessThan,
);

for (workspace_sort_buf.items) |workspace_pkg_id| {
const res = pkg_resolutions[workspace_pkg_id];
try writer.writeAll("\n");
try writeIndent(writer, indent);
try writeWorkspaceDeps(
writer,
indent,
@intCast(pkg_id),
@intCast(workspace_pkg_id),
res.value.workspace,
pkg_names,
pkg_name_hashes,
Expand All @@ -497,7 +525,7 @@ pub const Stringifier = struct {

const pkg_name = pkg_names[pkg_id];
const pkg_name_hash = pkg_name_hashes[pkg_id];
const res = pkg_resolution[pkg_id];
const res = pkg_resolutions[pkg_id];
const dep = deps_buf[dep_id];

if (lockfile.patched_dependencies.count() > 0) {
Expand Down Expand Up @@ -648,7 +676,7 @@ pub const Stringifier = struct {
const pkg_id = resolution_buf[dep_id];
if (pkg_id == invalid_package_id) continue;

const res = pkg_resolution[pkg_id];
const res = pkg_resolutions[pkg_id];
switch (res.tag) {
.root, .npm, .folder, .local_tarball, .github, .git, .symlink, .workspace, .remote_tarball => {},
.uninitialized => continue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,72 @@ exports[`auto-install symlinks (and junctions) are created correctly in the inst
}
"
`;

exports[`text lockfile workspace sorting 1`] = `
"{
"lockfileVersion": 0,
"workspaces": {
"": {
"dependencies": {
"no-deps": "1.0.0",
},
},
"packages/b": {
"name": "b",
"dependencies": {
"no-deps": "1.0.0",
},
},
"packages/c": {
"name": "c",
"dependencies": {
"no-deps": "1.0.0",
},
},
},
"packages": {
"b": ["b@workspace:packages/b", { "dependencies": { "no-deps": "1.0.0" } }],
"c": ["c@workspace:packages/c", { "dependencies": { "no-deps": "1.0.0" } }],
"no-deps": ["[email protected]", "http://localhost:1234/no-deps/-/no-deps-1.0.0.tgz", {}, "sha512-v4w12JRjUGvfHDUP8vFDwu0gUWu04j0cv9hLb1Abf9VdaXu4XcrddYFTMVBVvmldKViGWH7jrb6xPJRF0wq6gw=="],
}
}
"
`;

exports[`text lockfile workspace sorting 2`] = `
"{
"lockfileVersion": 0,
"workspaces": {
"": {
"dependencies": {
"no-deps": "1.0.0",
},
},
"packages/a": {
"name": "a",
"dependencies": {
"no-deps": "1.0.0",
},
},
"packages/b": {
"name": "b",
"dependencies": {
"no-deps": "1.0.0",
},
},
"packages/c": {
"name": "c",
"dependencies": {
"no-deps": "1.0.0",
},
},
},
"packages": {
"a": ["a@workspace:packages/a", { "dependencies": { "no-deps": "1.0.0" } }],
"b": ["b@workspace:packages/b", { "dependencies": { "no-deps": "1.0.0" } }],
"c": ["c@workspace:packages/c", { "dependencies": { "no-deps": "1.0.0" } }],
"no-deps": ["[email protected]", "http://localhost:1234/no-deps/-/no-deps-1.0.0.tgz", {}, "sha512-v4w12JRjUGvfHDUP8vFDwu0gUWu04j0cv9hLb1Abf9VdaXu4XcrddYFTMVBVvmldKViGWH7jrb6xPJRF0wq6gw=="],
}
}
"
`;
81 changes: 80 additions & 1 deletion test/cli/install/registry/bun-install-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ describe("whoami", async () => {
expect(err).not.toContain("error:");
expect(await exited).toBe(0);
});
test("two .npmrc", async () => {
test("two .npmrc", async () => {
const token = await generateRegistryUser("whoami-two-npmrc", "whoami-two-npmrc");
const packageNpmrc = `registry=http://localhost:${port}/`;
const homeNpmrc = `//localhost:${port}/:_authToken=${token}`;
Expand Down Expand Up @@ -1700,6 +1700,85 @@ describe("package.json indentation", async () => {
});
});

describe("text lockfile", () => {
test("workspace sorting", async () => {
await Promise.all([
write(
join(packageDir, "package.json"),
JSON.stringify({
name: "foo",
workspaces: ["packages/*"],
dependencies: {
"no-deps": "1.0.0",
},
}),
),
write(
join(packageDir, "packages", "b", "package.json"),
JSON.stringify({
name: "b",
dependencies: {
"no-deps": "1.0.0",
},
}),
),
write(
join(packageDir, "packages", "c", "package.json"),
JSON.stringify({
name: "c",
dependencies: {
"no-deps": "1.0.0",
},
}),
),
]);

let { stdout, stderr, exited } = spawn({
cmd: [bunExe(), "install", "--save-text-lockfile"],
cwd: packageDir,
stdout: "pipe",
stderr: "pipe",
env,
});

let out = await Bun.readableStreamToText(stdout);
let err = await Bun.readableStreamToText(stderr);
console.log({ out, err });
expect(await exited).toBe(0);

expect(
(await Bun.file(join(packageDir, "bun.lock")).text()).replaceAll(/localhost:\d+/g, "localhost:1234"),
).toMatchSnapshot();

// now add workspace 'a'
await write(
join(packageDir, "packages", "a", "package.json"),
JSON.stringify({
name: "a",
dependencies: {
"no-deps": "1.0.0",
},
}),
);

({ stdout, stderr, exited } = spawn({
cmd: [bunExe(), "install"],
cwd: packageDir,
stdout: "pipe",
stderr: "pipe",
env,
}));

out = await Bun.readableStreamToText(stdout);
err = await Bun.readableStreamToText(stderr);
console.log({ out, err });
expect(await exited).toBe(0);

const lockfile = await Bun.file(join(packageDir, "bun.lock")).text();
expect(lockfile.replaceAll(/localhost:\d+/g, "localhost:1234")).toMatchSnapshot();
});
});

describe("optionalDependencies", () => {
for (const optional of [true, false]) {
test(`exit code is ${optional ? 0 : 1} when ${optional ? "optional" : ""} dependency tarball is missing`, async () => {
Expand Down

0 comments on commit e008425

Please sign in to comment.