Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(install): automatically migrate package-lock.json to bun.lockb #6352

Merged
merged 43 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
5847b41
work so far
paperclover Oct 3, 2023
77d7ae9
Merge remote-tracking branch 'origin/main' into dave/npm-migrate
paperclover Oct 3, 2023
311662a
stuff
paperclover Oct 4, 2023
639c131
a
paperclover Oct 4, 2023
94fc3bd
basics work
paperclover Oct 4, 2023
f91be35
Merge remote-tracking branch 'origin/main' into dave/npm-migrate
paperclover Oct 4, 2023
155887e
stuff
paperclover Oct 4, 2023
42426df
yoo
paperclover Oct 5, 2023
5a8e7dd
Merge remote-tracking branch 'origin/main' into dave/npm-migrate
paperclover Oct 5, 2023
b4be6ac
build lockfile
paperclover Oct 6, 2023
9fb00e0
correct
paperclover Oct 6, 2023
7ccbfde
Merge remote-tracking branch 'origin/main' into dave/npm-migrate
paperclover Oct 7, 2023
e91e977
f
paperclover Oct 7, 2023
f176be0
Merge remote-tracking branch 'origin/main' into dave/npm-migrate
paperclover Oct 7, 2023
4fc1585
Merge remote-tracking branch 'origin/main' into dave/npm-migrate
paperclover Oct 9, 2023
aea5c2b
a
paperclover Oct 9, 2023
0e1edcc
install fixture havent tested
paperclover Oct 10, 2023
90ca75d
i made it worse
paperclover Oct 10, 2023
0a4c386
lol
paperclover Oct 10, 2023
0d62e46
be more reasonable
paperclover Oct 10, 2023
a191b3f
make the test easier to pass because bun install doesn't handle obscu…
paperclover Oct 10, 2023
f041109
a
paperclover Oct 10, 2023
c7efa8f
works now
paperclover Oct 10, 2023
06b4834
ok
paperclover Oct 10, 2023
337616b
a
paperclover Oct 10, 2023
f6ab751
a
paperclover Oct 10, 2023
dcbe6cc
cool
paperclover Oct 10, 2023
6e42b48
nah
paperclover Oct 10, 2023
9cb75e9
Merge remote-tracking branch 'origin/main' into dave/npm-migrate
paperclover Oct 10, 2023
f4f3826
fix stuff
paperclover Oct 11, 2023
8498f26
l
paperclover Oct 11, 2023
aab0b9e
a
paperclover Oct 11, 2023
4bbc448
idfk
paperclover Oct 11, 2023
d91e628
LAME
paperclover Oct 11, 2023
e85621c
prettier errors
paperclover Oct 11, 2023
eaeb71e
Merge remote-tracking branch 'origin/main' into dave/npm-migrate
paperclover Oct 11, 2023
042f1d7
does this fix tests?
paperclover Oct 11, 2023
f97f363
Add more safety checks to Integrity
Jarred-Sumner Oct 11, 2023
49278a8
Add another check
Jarred-Sumner Oct 11, 2023
34b1264
More careful lifetime handling
Jarred-Sumner Oct 11, 2023
f0d27dc
Fix linux debugger issue
Jarred-Sumner Oct 11, 2023
1b01abc
a
paperclover Oct 11, 2023
543a3a1
tmp dir and snapshot test
paperclover Oct 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix stuff
  • Loading branch information
paperclover committed Oct 11, 2023
commit f4f38266d32c9cfd819a3187709fa316c9833218
5 changes: 3 additions & 2 deletions .vscode/launch.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified bun.lockb
Binary file not shown.
14 changes: 13 additions & 1 deletion src/install/dependency.zig
Original file line number Diff line number Diff line change
Expand Up @@ -964,13 +964,17 @@ pub fn parseWithTag(
pub const Behavior = packed struct(u8) {
pub const uninitialized: Behavior = .{};

// these padding fields are to have compatibility
// with older versions of lockfile v2
_unused_1: u1 = 0,

normal: bool = false,
optional: bool = false,
dev: bool = false,
peer: bool = false,
workspace: bool = false,

_padding: u3 = 0, //padding
_unused_2: u2 = 0,

pub const normal = Behavior{ .normal = true };
pub const optional = Behavior{ .optional = true };
Expand Down Expand Up @@ -1116,4 +1120,12 @@ pub const Behavior = packed struct(u8) {
},
}
}

comptime {
std.debug.assert(@as(u8, @bitCast(Behavior.normal)) == (1 << 1));
std.debug.assert(@as(u8, @bitCast(Behavior.optional)) == (1 << 2));
std.debug.assert(@as(u8, @bitCast(Behavior.dev)) == (1 << 3));
std.debug.assert(@as(u8, @bitCast(Behavior.peer)) == (1 << 4));
std.debug.assert(@as(u8, @bitCast(Behavior.workspace)) == (1 << 5));
}
};
10 changes: 9 additions & 1 deletion src/install/lockfile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,14 @@ pub const Package = extern struct {
}
defer to_i += 1;

debug("compare {s} and {s}", .{
from_dep.name.slice(from_lockfile.buffers.string_bytes.items),
to_deps[to_i].name.slice(to_lockfile.buffers.string_bytes.items),
});
debug("-> {} and {}", .{
from_dep.*,
to_deps[to_i],
});
if (to_deps[to_i].eql(from_dep, to_lockfile.buffers.string_bytes.items, from_lockfile.buffers.string_bytes.items)) {
if (update_requests) |updates| {
if (updates.len == 0 or brk: {
Expand Down Expand Up @@ -4470,7 +4478,7 @@ pub fn hasMetaHashChanged(this: *Lockfile, print_name_version_string: bool) !boo
this.meta_hash = try this.generateMetaHash(print_name_version_string);
return !strings.eqlLong(&previous_meta_hash, &this.meta_hash, false);
}
fn generateMetaHash(this: *Lockfile, print_name_version_string: bool) !MetaHash {
pub fn generateMetaHash(this: *Lockfile, print_name_version_string: bool) !MetaHash {
if (this.packages.len <= 1)
return zero_hash;

Expand Down
21 changes: 9 additions & 12 deletions src/install/migration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ pub fn migrateNPMLockfile(this: *Lockfile, allocator: Allocator, log: *logger.Lo
.meta = .{
.id = package_id,

.origin = if (is_workspace) .local else .npm,
.origin = if (package_id == 0) .local else .npm,

.arch = if (pkg.get("cpu")) |cpu_array| arch: {
if (cpu_array.data != .e_array) return error.InvalidNPMLockfile;
Expand Down Expand Up @@ -582,14 +582,16 @@ pub fn migrateNPMLockfile(this: *Lockfile, allocator: Allocator, log: *logger.Lo
for (wksp.keys(), wksp.values()) |key, value| {
const entry1 = id_map.get(key) orelse return error.InvalidNPMLockfile;
const name_hash = stringHash(value.name);
const wksp_name = builder.append(String, value.name);
const wksp_path = builder.append(String, key);
dependencies_buf[0] = Dependency{
.name = builder.append(String, value.name),
.name = wksp_name,
.name_hash = name_hash,
.version = .{
.tag = .workspace,
.literal = builder.append(String, key),
.literal = wksp_path,
.value = .{
.workspace = builder.append(String, key),
.workspace = wksp_path,
},
},
.behavior = .{
Expand Down Expand Up @@ -681,14 +683,7 @@ pub fn migrateNPMLockfile(this: *Lockfile, allocator: Allocator, log: *logger.Lo
dependencies_buf[0] = Dependency{
.name = dep_name,
.name_hash = name_hash,
.version = if (is_workspace)
.{
.tag = .workspace,
.literal = resolutions[id].value.workspace,
.value = .{ .workspace = resolutions[id].value.workspace },
}
else
version,
.version = version,
.behavior = .{
.normal = dep_key == .dependencies,
.optional = dep_key == .optionalDependencies,
Expand Down Expand Up @@ -933,6 +928,8 @@ pub fn migrateNPMLockfile(this: *Lockfile, allocator: Allocator, log: *logger.Lo
try this.verifyData();
}

this.meta_hash = try this.generateMetaHash(false);

return LoadFromDiskResult{ .ok = this };
}

Expand Down
2 changes: 1 addition & 1 deletion test/cli/install/bun-pm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ it("should remove all cache", async () => {
});

it("bun pm migrate", async () => {
const test_dir = join(import.meta.dir, "migrate-2");
const test_dir = join(import.meta.dir, "migration/contoso-test");
rmSync(join(test_dir, "bun.lockb"), { recursive: true, force: true });

const { stdout, stderr, exitCode } = Bun.spawnSync({
Expand Down
2 changes: 0 additions & 2 deletions test/cli/install/migrate-3/.gitignore

This file was deleted.

31 changes: 0 additions & 31 deletions test/cli/install/migrate-3/migrate-and-add.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/cli/install/migrate-3/package.json

This file was deleted.

2 changes: 0 additions & 2 deletions test/cli/install/migrate-4/.gitignore

This file was deleted.

18 changes: 0 additions & 18 deletions test/cli/install/migrate-4/migrate-and-add-workspace.test.ts

This file was deleted.

37 changes: 0 additions & 37 deletions test/cli/install/migrate-4/package-lock.json

This file was deleted.

6 changes: 0 additions & 6 deletions test/cli/install/migrate-4/packages/a/original.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "a",
"version": "1.0.0",
"dependencies": {
"b": "1.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import fs from "fs";
import path from "path";
import { test, expect, describe, beforeAll } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { tmpdir } from "os";
import { join } from "path";

const cwd = import.meta.dir;
let cwd = join(tmpdir(), "complex-workspace-test" + Math.random().toString(36).slice(2, 8));

function validate(packageName: string, version: string, realPackageName?: string) {
test(`${packageName} is ${realPackageName ? `${realPackageName}@${version}` : version}`, () => {
Expand Down Expand Up @@ -31,24 +33,27 @@ function mustNotExist(filePath: string) {
});
}

beforeAll(() => {
fs.rmSync("bun.lockb", { recursive: true, force: true });
fs.rmSync("node_modules", { recursive: true, force: true });
fs.rmSync("packages/body-parser/node_modules", { recursive: true, force: true });
fs.rmSync("packages/lol-package/node_modules", { recursive: true, force: true });
fs.rmSync("packages/second/node_modules", { recursive: true, force: true });
fs.rmSync("packages/with-postinstall/node_modules", { recursive: true, force: true });
fs.rmSync("packages/with-postinstall/postinstall.txt", { recursive: true, force: true });
test('the install succeeds', () => {
fs.cpSync(path.join(import.meta.dir, "complex-workspace"), cwd, { recursive: true });

Bun.spawnSync([bunExe(), "reset.ts"], {
env: bunEnv,
cwd,
});

Bun.spawnSync([bunExe(), "install"], {
env: bunEnv,
cwd,
});

console.log(cwd);
});

// bun-types
validate("node_modules/bun-types", "1.0.0");
mustExist("node_modules/bun-types/isfake.txt");
validate("node_modules/bun-types/node_modules/bun-types", "1.0.0");
// NOTE: ???
// validate("node_modules/bun-types/node_modules/bun-types", "1.0.0");
mustNotExist("node_modules/bun-types/node_modules/bun-types/isfake.txt");

// svelte
Expand Down Expand Up @@ -78,14 +83,15 @@ validate("node_modules/body-parser", "200.0.0");
// NOTE: bun hoists this dependency higher than npm
// npm places this in node_modules/not-body-parser
validate("packages/second/node_modules/not-body-parser", "200.0.0", "body-parser");
validate("packages/second/node_modules/connect", "200.0.0", "body-parser");
// NOTE: bun install doesnt properly handle npm aliased dependencies
// validate("packages/second/node_modules/connect", "200.0.0", "body-parser");
validate("packages/second/node_modules/body-parser", "3.21.2", "express");
// NOTE: bun does not hoist this properly, but it is extremely unlikely to be a real use case
// validate("packages/second/node_modules/body-parser/node_modules/body-parser", "1.13.3", "body-parser");

// connect
mustNotExist("node_modules/connect");
validate("packages/second/node_modules/body-parser/node_modules/connect", "2.30.2", "connect");
// mustNotExist("node_modules/connect");
// validate("packages/second/node_modules/body-parser/node_modules/connect", "2.30.2", "connect");

// sharp
validate("node_modules/sharp", "0.32.6");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "bun-types",
"version": "1.0.0",
"dependencies": {
"bun-types": "npm:bun-types@^1.0.0"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bun-types": "file:bun-types",
"hello": "file:hello-0.3.2.tgz",
"install-test": "bitbucket:dylan-conway/public-install-test",
"install-test1": "github:dylan-conway/install-test",
"install-test1": "git+ssh://git@github.com/dylan-conway/install-test.git#596234dab30564f37adae1e5c4d7123bcffce537",
"public-install-test": "gitlab:dylan-conway/public-install-test",
"svelte": "4.1.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"hello": "0.3.2",
"lol": "*",
"not-body-parser": "*",
"svelte": "4.1.0",
"connect": "200.0.0"
"svelte": "4.1.0"
}
}
1 change: 1 addition & 0 deletions test/cli/install/migration/complex-workspace/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this case has a very fun and hard to understand dependency graph. bun pm migrate is expected to migrate it without changing what packages will install. the hoisting doesnt match exactly what npm does, but it is equally valid.
Binary file not shown.
Loading