Skip to content

Commit

Permalink
remove bin reading
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway committed Dec 15, 2024
1 parent fa25709 commit 86aa496
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 338 deletions.
2 changes: 1 addition & 1 deletion src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,7 @@ pub const Formatter = struct {

writer.print(
comptime Output.prettyFmt("<r><green>{s}<r><d>:<r> ", enable_ansi_colors),
.{bun.fmt.formatJSONString(key.slice())},
.{bun.fmt.formatJSONStringLatin1(key.slice())},
);
}
} else if (Environment.isDebug and is_private_symbol) {
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/javascript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3000,7 +3000,7 @@ pub const VirtualMachine = struct {
"{s} resolving preload {}",
.{
@errorName(e),
bun.fmt.formatJSONString(preload),
bun.fmt.formatJSONStringLatin1(preload),
},
) catch unreachable;
return e;
Expand All @@ -3012,7 +3012,7 @@ pub const VirtualMachine = struct {
this.allocator,
"preload not found {}",
.{
bun.fmt.formatJSONString(preload),
bun.fmt.formatJSONStringLatin1(preload),
},
) catch unreachable;
return error.ModuleNotFound;
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/test/pretty_format.zig
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ pub const JestPrettyFormat = struct {

writer.print(
comptime Output.prettyFmt("<r><green>{s}<r><d>:<r> ", enable_ansi_colors),
.{bun.fmt.formatJSONString(key.slice())},
.{bun.fmt.formatJSONStringLatin1(key.slice())},
);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/init_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ pub const InitCommand = struct {
" \"'",
fields.entry_point,
)) {
Output.prettyln(" <r><cyan>bun run {any}<r>", .{bun.fmt.formatJSONString(fields.entry_point)});
Output.prettyln(" <r><cyan>bun run {any}<r>", .{bun.fmt.formatJSONStringLatin1(fields.entry_point)});
} else {
Output.prettyln(" <r><cyan>bun run {s}<r>", .{fields.entry_point});
}
Expand Down
2 changes: 1 addition & 1 deletion src/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ const JSONFormatterUTF8 = struct {
};

/// Expects latin1
pub fn formatJSONString(text: []const u8) JSONFormatter {
pub fn formatJSONStringLatin1(text: []const u8) JSONFormatter {
return .{ .input = text };
}

Expand Down
13 changes: 1 addition & 12 deletions src/install/bin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@ const Lockfile = Install.Lockfile;
/// - map where keys are names of the binaries and values are file paths to the binaries
pub const Bin = extern struct {
tag: Tag = Tag.none,
unset: u8 = 0,
_padding_tag: [2]u8 = .{0} ** 2,
_padding_tag: [3]u8 = .{0} ** 3,

// Largest member must be zero initialized
value: Value = Value{ .map = ExternalStringList{} },

pub fn isUnset(this: *const Bin) bool {
return this.unset != 0;
}

pub fn count(this: *const Bin, buf: []const u8, extern_strings: []const ExternalString, comptime StringBuilder: type, builder: StringBuilder) u32 {
switch (this.tag) {
.file => builder.count(this.value.file.slice(buf)),
Expand All @@ -64,21 +59,18 @@ pub const Bin = extern struct {
.none => {
return Bin{
.tag = .none,
.unset = this.unset,
.value = Value.init(.{ .none = {} }),
};
},
.file => {
return Bin{
.tag = .file,
.unset = this.unset,
.value = Value.init(.{ .file = builder.append(String, this.value.file.slice(buf)) }),
};
},
.named_file => {
return Bin{
.tag = .named_file,
.unset = this.unset,
.value = Value.init(
.{
.named_file = [2]String{
Expand All @@ -92,7 +84,6 @@ pub const Bin = extern struct {
.dir => {
return Bin{
.tag = .dir,
.unset = this.unset,
.value = Value.init(.{ .dir = builder.append(String, this.value.dir.slice(buf)) }),
};
},
Expand All @@ -103,7 +94,6 @@ pub const Bin = extern struct {

return Bin{
.tag = .map,
.unset = this.unset,
.value = Value.init(.{ .map = ExternalStringList.init(all_extern_strings, extern_strings_slice) }),
};
},
Expand All @@ -118,7 +108,6 @@ pub const Bin = extern struct {

const cloned: Bin = .{
.tag = this.tag,
.unset = this.unset,

.value = switch (this.tag) {
.none => Value.init(.{ .none = {} }),
Expand Down
118 changes: 58 additions & 60 deletions src/install/bun.lock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const Negatable = Npm.Negatable;
const DependencyID = Install.DependencyID;
const invalid_dependency_id = Install.invalid_dependency_id;
const DependencyIDSlice = BinaryLockfile.DependencyIDSlice;
const Bin = Install.Bin;
const ExternalString = Semver.ExternalString;

/// A property key in the `packages` field of the lockfile
pub const PkgPath = struct {
Expand Down Expand Up @@ -421,6 +423,7 @@ pub const Stringifier = struct {
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);
const pkg_bins = pkgs.items(.bin);

var temp_buf: std.ArrayListUnmanaged(u8) = .{};
defer temp_buf.deinit(allocator);
Expand Down Expand Up @@ -744,8 +747,9 @@ pub const Stringifier = struct {
dep_name,
});

const pkg_name = pkg_names[pkg_id].slice(buf);
const pkg_name = pkg_names[pkg_id];
const pkg_meta = pkg_metas[pkg_id];
const pkg_bin = pkg_bins[pkg_id];
const pkg_deps_list = pkg_dep_lists[pkg_id];

pkg_deps_sort_buf.clearRetainingCapacity();
Expand Down Expand Up @@ -774,53 +778,53 @@ pub const Stringifier = struct {
switch (res.tag) {
.root => {
try writer.print("[\"{}@root:\"]", .{
bun.fmt.formatJSONStringUTF8(pkg_name, .{ .quote = false }),
pkg_name.fmtJson(buf, .{ .quote = false }),
// we don't read the root package version into the binary lockfile
});
},
.folder => {
try writer.print("[\"{s}@file:{}\", ", .{
pkg_name,
bun.fmt.formatJSONStringUTF8(res.value.folder.slice(buf), .{ .quote = false }),
try writer.print("[\"{}@file:{}\", ", .{
pkg_name.fmtJson(buf, .{ .quote = false }),
res.value.folder.fmtJson(buf, .{ .quote = false }),
});

try writePackageDepsAndMeta(writer, dep_id, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, buf, &optional_peers_buf, &lockfile.buffers.extern_strings);
try writePackageInfoObject(writer, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, &pkg_bin, buf, &optional_peers_buf, lockfile.buffers.extern_strings.items);

try writer.writeByte(']');
},
.local_tarball => {
try writer.print("[\"{s}@{}\", ", .{
pkg_name,
bun.fmt.formatJSONStringUTF8(res.value.local_tarball.slice(buf), .{ .quote = false }),
try writer.print("[\"{}@{}\", ", .{
pkg_name.fmtJson(buf, .{ .quote = false }),
res.value.local_tarball.fmtJson(buf, .{ .quote = false }),
});

try writePackageDepsAndMeta(writer, dep_id, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, buf, &optional_peers_buf, &lockfile.buffers.extern_strings);
try writePackageInfoObject(writer, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, &pkg_bin, buf, &optional_peers_buf, lockfile.buffers.extern_strings.items);

try writer.writeByte(']');
},
.remote_tarball => {
try writer.print("[\"{s}@{}\", ", .{
pkg_name,
bun.fmt.formatJSONStringUTF8(res.value.remote_tarball.slice(buf), .{ .quote = false }),
try writer.print("[\"{}@{}\", ", .{
pkg_name.fmtJson(buf, .{ .quote = false }),
res.value.remote_tarball.fmtJson(buf, .{ .quote = false }),
});

try writePackageDepsAndMeta(writer, dep_id, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, buf, &optional_peers_buf, &lockfile.buffers.extern_strings);
try writePackageInfoObject(writer, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, &pkg_bin, buf, &optional_peers_buf, lockfile.buffers.extern_strings.items);

try writer.writeByte(']');
},
.symlink => {
try writer.print("[\"{s}@link:{}\", ", .{
pkg_name,
bun.fmt.formatJSONStringUTF8(res.value.symlink.slice(buf), .{ .quote = false }),
try writer.print("[\"{}@link:{}\", ", .{
pkg_name.fmtJson(buf, .{ .quote = false }),
res.value.symlink.fmtJson(buf, .{ .quote = false }),
});

try writePackageDepsAndMeta(writer, dep_id, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, buf, &optional_peers_buf, &lockfile.buffers.extern_strings);
try writePackageInfoObject(writer, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, &pkg_bin, buf, &optional_peers_buf, lockfile.buffers.extern_strings.items);

try writer.writeByte(']');
},
.npm => {
try writer.print("[\"{s}@{}\", ", .{
pkg_name,
try writer.print("[\"{}@{}\", ", .{
pkg_name.fmtJson(buf, .{ .quote = false }),
res.value.npm.version.fmt(buf),
});

Expand All @@ -832,35 +836,33 @@ pub const Stringifier = struct {
res.value.npm.url.slice(buf),
});

try writePackageDepsAndMeta(writer, dep_id, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, buf, &optional_peers_buf, &lockfile.buffers.extern_strings);
try writePackageInfoObject(writer, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, &pkg_bin, buf, &optional_peers_buf, lockfile.buffers.extern_strings.items);

try writer.print(", \"{}\"]", .{
pkg_meta.integrity,
});
},
.workspace => {
const workspace_path = res.value.workspace.slice(buf);

try writer.print("[\"{s}@workspace:{}\", ", .{
pkg_name,
bun.fmt.formatJSONStringUTF8(workspace_path, .{ .quote = false }),
try writer.print("[\"{}@workspace:{}\", ", .{
pkg_name.fmtJson(buf, .{ .quote = false }),
res.value.workspace.fmtJson(buf, .{ .quote = false }),
});

try writePackageDepsAndMeta(writer, dep_id, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, buf, &optional_peers_buf, &lockfile.buffers.extern_strings);
try writePackageInfoObject(writer, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, &pkg_bin, buf, &optional_peers_buf, lockfile.buffers.extern_strings.items);

try writer.writeByte(']');
},
inline .git, .github => |tag| {
const repo: Repository = @field(res.value, @tagName(tag));
try writer.print("[\"{s}@{}\", ", .{
pkg_name,
try writer.print("[\"{}@{}\", ", .{
pkg_name.fmtJson(buf, .{ .quote = false }),
repo.fmt(if (comptime tag == .git) "git+" else "github:", buf),
});

try writePackageDepsAndMeta(writer, dep_id, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, buf, &optional_peers_buf, &lockfile.buffers.extern_strings);
try writePackageInfoObject(writer, deps_buf, pkg_deps_sort_buf.items, &pkg_meta, &pkg_bin, buf, &optional_peers_buf, lockfile.buffers.extern_strings.items);

try writer.print(", {}]", .{
bun.fmt.formatJSONStringUTF8(repo.resolved.slice(buf), .{ .quote = true }),
repo.resolved.fmtJson(buf, .{}),
});
},
else => unreachable,
Expand All @@ -881,18 +883,17 @@ pub const Stringifier = struct {
return writer_buf.list.items;
}

/// Writes a single line object.
/// Writes a single line object. Contains dependencies, os, cpu, libc (soon), and bin
/// { "devDependencies": { "one": "1.1.1", "two": "2.2.2" }, "os": "none" }
fn writePackageDepsAndMeta(
fn writePackageInfoObject(
writer: anytype,
_: DependencyID,
deps_buf: []const Dependency,
pkg_dep_ids: []const DependencyID,
meta: *const Meta,
bin: *const Install.Bin,
buf: string,
optional_peers_buf: *std.ArrayList(String),
extern_strings: *const Install.Lockfile.ExternalStringBuffer,
extern_strings: []const ExternalString,
) OOM!void {
defer optional_peers_buf.clearRetainingCapacity();

Expand Down Expand Up @@ -997,9 +998,9 @@ pub const Stringifier = struct {
any = true;
}
try writer.print(
\\ "bin": "{s}"
\\ "bin": {}
, .{
bin.value.file.slice(buf),
bin.value.file.fmtJson(buf, .{}),
});
},
.named_file => {
Expand All @@ -1012,10 +1013,10 @@ pub const Stringifier = struct {
\\ "bin": {
);
try writer.print(
\\ "{s}": "{s}"
\\ {}: {}
, .{
bin.value.named_file[0].slice(buf),
bin.value.named_file[1].slice(buf),
bin.value.named_file[0].fmtJson(buf, .{}),
bin.value.named_file[1].fmtJson(buf, .{}),
});
try writer.writeByte('}');
},
Expand All @@ -1026,9 +1027,9 @@ pub const Stringifier = struct {
any = true;
}
try writer.print(
\\ "bin": "{s}"
\\ "binDir": {}
, .{
bin.value.dir.slice(buf),
bin.value.dir.fmtJson(buf, .{}),
});
},
.map => {
Expand All @@ -1049,10 +1050,10 @@ pub const Stringifier = struct {
}
first = false;
try writer.print(
\\ "{s}": "{s}"
\\ {}: {}
, .{
list[i].slice(buf),
list[i + 1].slice(buf),
list[i].value.fmtJson(buf, .{}),
list[i + 1].value.fmtJson(buf, .{}),
});
}
try writer.writeByte('}');
Expand Down Expand Up @@ -1591,33 +1592,35 @@ pub fn parseIntoBinaryLockfile(
return error.InvalidPackageInfo;
}

const deps_os_cpu_libc_obj_bin = pkg_info.at(i);
const deps_os_cpu_libc_bin_obj = pkg_info.at(i);
i += 1;
if (!deps_os_cpu_libc_obj_bin.isObject()) {
try log.addError(source, deps_os_cpu_libc_obj_bin.loc, "Expected an object");
if (!deps_os_cpu_libc_bin_obj.isObject()) {
try log.addError(source, deps_os_cpu_libc_bin_obj.loc, "Expected an object");
return error.InvalidPackageInfo;
}

const off, const len = try parseAppendDependencies(lockfile, allocator, deps_os_cpu_libc_obj_bin, &string_buf, log, source, &optional_peers_buf);
const off, const len = try parseAppendDependencies(lockfile, allocator, deps_os_cpu_libc_bin_obj, &string_buf, log, source, &optional_peers_buf);

pkg.dependencies = .{ .off = off, .len = len };
pkg.resolutions = .{ .off = off, .len = len };

if (deps_os_cpu_libc_bin_obj.get("bin")) |bin| {
pkg.bin = try Bin.parseAppend(allocator, bin, &string_buf, &lockfile.buffers.extern_strings);
} else if (deps_os_cpu_libc_bin_obj.get("binDir")) |bin_dir| {
pkg.bin = try Bin.parseAppendFromDirectories(allocator, bin_dir, &string_buf);
}

if (res.tag != .workspace) {
if (deps_os_cpu_libc_obj_bin.get("os")) |os| {
if (deps_os_cpu_libc_bin_obj.get("os")) |os| {
pkg.meta.os = try Negatable(Npm.OperatingSystem).fromJson(allocator, os);
}
if (deps_os_cpu_libc_obj_bin.get("cpu")) |arch| {
if (deps_os_cpu_libc_bin_obj.get("cpu")) |arch| {
pkg.meta.arch = try Negatable(Npm.Architecture).fromJson(allocator, arch);
}
// TODO(dylan-conway)
// if (os_cpu_libc_obj.get("libc")) |libc| {
// pkg.meta.libc = Negatable(Npm.Libc).fromJson(allocator, libc);
// }

if (deps_os_cpu_libc_obj_bin.get("bin")) |bin| {
pkg.bin = Install.Bin.parseAppend(allocator, bin, &string_buf, &lockfile.buffers.extern_strings) catch bun.outOfMemory();
}
}
},
else => {},
Expand Down Expand Up @@ -1662,11 +1665,6 @@ pub fn parseIntoBinaryLockfile(
pkg.name = name;
pkg.name_hash = name_hash;
pkg.resolution = res;

// set later
pkg.bin = .{
.unset = 1,
};
pkg.scripts = .{};

const pkg_id = try lockfile.appendPackageDedupe(&pkg, string_buf.bytes.items);
Expand Down
Loading

0 comments on commit 86aa496

Please sign in to comment.