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

Copy overrides from package.json during migration #7285

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions src/install/migration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ pub fn migrateNPMLockfile(this: *Lockfile, allocator: Allocator, log: *logger.Lo
}
}

if (pkg.get("name")) |pkg_name| {
hborchardt marked this conversation as resolved.
Show resolved Hide resolved
const pkg_name_str = pkg_name.asString(this.allocator) orelse unreachable;
builder.count(pkg_name_str);
}

if (pkg.get("bin")) |bin| {
if (bin.data != .e_object) return error.InvalidNPMLockfile;
switch (bin.data.e_object.properties.len) {
Expand Down Expand Up @@ -952,6 +957,45 @@ pub fn migrateNPMLockfile(this: *Lockfile, allocator: Allocator, log: *logger.Lo
this.buffers.resolutions.items.len = (@intFromPtr(resolutions_buf.ptr) - @intFromPtr(this.buffers.resolutions.items.ptr)) / @sizeOf(Install.PackageID);
this.buffers.dependencies.items.len = this.buffers.resolutions.items.len;

// consult the corresponding package.json file for overrides, if possible
const overrides_copied = copied: {
const package_json_dir_path = path[0..(path.len - "package-lock.json".len)];
const package_json_path_buf = try this.allocator.alloc(u8, path.len - "package-lock.json".len + "package.json".len);
defer this.allocator.free(package_json_path_buf);
@memcpy(package_json_path_buf[0..package_json_dir_path.len], package_json_dir_path);
package_json_path_buf[package_json_dir_path.len..package_json_path_buf.len][0.."package.json".len].* = "package.json".*;

var json_file = bun.openFile(package_json_path_buf, .{ .mode = .read_only }) catch break :copied false;
hborchardt marked this conversation as resolved.
Show resolved Hide resolved
defer json_file.close();
const json_stat_size = try json_file.getEndPos();
const json_buf = try this.allocator.alloc(u8, json_stat_size + 64);
defer this.allocator.free(json_buf);
const json_len = try json_file.preadAll(json_buf, 0);
const json_source = logger.Source.initPathString(package_json_path_buf, json_buf[0..json_len]);

var lockfile: Lockfile = undefined;
try lockfile.initEmpty(this.allocator);
defer lockfile.deinit();
var maybe_root = Lockfile.Package{};
try maybe_root.parseMain(
&lockfile,
this.allocator,
log,
json_source,
Install.Features.main,
);

var sb = this.stringBuilder();
lockfile.overrides.count(&lockfile, &sb);
try sb.allocate();
this.overrides = try lockfile.overrides.clone(&lockfile, this, &sb);
break :copied true;
};

if (overrides_copied) {
debug("copied {d} overrides from package.json", .{this.overrides.map.count()});
}

// In allow_assert, we prefill this buffer with uninitialized values that we can detect later
// It is our fault if we hit an error here, making it safe to disable in release.
if (Environment.allow_assert) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!package-lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[install]
registry = "https://registry.npmjs.org/"

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

10 changes: 10 additions & 0 deletions test/cli/install/migration/lockfile-with-overrides/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "bun",
"version": "0.0.0",
"dependencies": {
"rimraf": "^4.3.1"
},
"overrides": {
"glob": "npm:empty-npm-package@1.0.0"
}
}
18 changes: 18 additions & 0 deletions test/cli/install/migration/migrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,21 @@ test("migrate from npm lockfile that is missing `resolved` properties", async ()
expect(await Bun.file(join(testDir, "node_modules/lodash/package.json")).json()).toHaveProperty("version", "4.17.21");
expect(exitCode).toBe(0);
});

test("migrate from npm lockfile with overrides", async () => {
const testDir = join(tmpdir(), "migrate-" + Math.random().toString(36).slice(2));

fs.cpSync(join(import.meta.dir, "lockfile-with-overrides"), testDir, { recursive: true });

const { exitCode: exitCodeMigrate } = Bun.spawnSync([bunExe(), "pm", "migrate"], {
env: bunEnv,
cwd: testDir,
});
expect(exitCodeMigrate).toBe(0);

const { exitCode: exitCodeInstall } = Bun.spawnSync([bunExe(), "install", "--frozen-lockfile"], {
env: bunEnv,
cwd: testDir,
});
expect(exitCodeInstall).toBe(0);
});