Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
probably-neb committed Dec 3, 2024
1 parent 236e591 commit 48c1d69
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions src/install/lockfile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4336,6 +4336,8 @@ pub const Package = extern struct {
arena: std.heap.ArenaAllocator,
exclusions: Exclusions,

const we_debug = Output.scoped(.WorkspaceExclusions, false);

const Exclusions = bun.StringArrayHashMap(u32);

pub fn init(allocator: std.mem.Allocator, root_path: []const u8) WorkspaceExclusions {
Expand All @@ -4356,7 +4358,7 @@ pub const Package = extern struct {
self.exclusions.deinit();
}

pub fn append(self: *WorkspaceExclusions, log: *logger.Log, source: *const logger.Source, loc: logger.Loc, glob: []const u8, index: u32) bun.OOM!void {
pub fn insert(self: *WorkspaceExclusions, log: *logger.Log, source: *const logger.Source, loc: logger.Loc, glob: []const u8, index: u32) bun.OOM!void {
if (comptime Environment.allow_assert) {
assert(isExclusion(glob));
}
Expand All @@ -4375,7 +4377,7 @@ pub const Package = extern struct {
break :brk glob[negation_count..];
};

bun.Output.debug("found exclusion: {s}\n", .{glob});
we_debug("found exclusion: {s}\n", .{glob});

defer _ = self.arena.reset(.retain_capacity);

Expand All @@ -4395,13 +4397,12 @@ pub const Package = extern struct {

entry.value_ptr.* = index;

bun.Output.debug("path: {s} - saved as exclusion (early)\n", .{key_path});
we_debug("path: {s} - saved as exclusion (early)\n", .{key_path});
return;
}


const glob_pattern = brk: {
const parts = [_][]const u8{non_negated_glob, "package.json"};
const parts = [_][]const u8{ non_negated_glob, "package.json" };
break :brk arena_alloc.dupe(u8, bun.path.join(parts, .auto)) catch bun.outOfMemory();
};

Expand Down Expand Up @@ -4455,39 +4456,48 @@ pub const Package = extern struct {
// invalid path in exclusion is a reasonable error to allow
// continue instead of return in case some paths described by glob are valid unlike this one
continue;
}
},
};
const excluded_package_json_dir: []const u8 = Path.dirname(excluded_package_json_path, .auto);

const abs_excluded_workspace_dir_path = self.prepareRelPathForHash(filepath_buf, excluded_package_json_dir);
const key_path = self.prepareRelPathForHash(filepath_buf, excluded_package_json_dir);

// PERF: try relative to cwd path for smaller key to hash
const entry = try self.exclusions.getOrPut(abs_excluded_workspace_dir_path);
const entry = try self.exclusions.getOrPut(key_path);

if (!entry.found_existing) {
entry.key_ptr.* = try self.exclusions.allocator.dupe(u8, abs_excluded_workspace_dir_path);
entry.key_ptr.* = try self.exclusions.allocator.dupe(u8, key_path);
}

bun.Output.debug("path: {s} - saved as exclusion\n", .{abs_excluded_workspace_dir_path});
we_debug("path: {s} - saved as exclusion\n", .{key_path});

entry.value_ptr.* = index;
}
}

fn prepareRelPathForHash(self: WorkspaceExclusions, path_buf: []u8, rel_path: []const u8) []const u8 {
var abs_path = Path.joinAbsStringBuf(
const abs_path = Path.joinAbsStringBuf(
self.abs_root_path,
path_buf,
&.{rel_path},
.auto,
);

return self.prepareAbsPathForHash(abs_path);
}

fn prepareAbsPathForHash(self: WorkspaceExclusions, abs_path: []const u8) []const u8 {
// strip trailing sep
if (abs_path.len > 0 and abs_path[abs_path.len - 1] == std.fs.path.sep) {
abs_path.len -= 1;
}
const abs_path_no_trailing_sep = if (abs_path.len > 0 and abs_path[abs_path.len - 1] == std.fs.path.sep)
abs_path[0 .. abs_path.len - 1]
else
abs_path;

const res_path = abs_path;
// make relative
assert(abs_path_no_trailing_sep.len >= self.abs_root_path.len);
var res_path = abs_path_no_trailing_sep[self.abs_root_path.len..];
if (res_path.len > 0 and res_path[0] == std.fs.path.sep) {
res_path = res_path[1..];
}

return res_path;
}
Expand All @@ -4497,25 +4507,25 @@ pub const Package = extern struct {

if (comptime Environment.allow_assert) {
assert(std.fs.path.isAbsolute(abs_path));
assert(!std.mem.endsWith(u8, abs_path, if (comptime Environment.isWindows) "\\" else "/"));
assert(!std.mem.endsWith(u8, abs_path, "package.json"));
assert(std.mem.startsWith(u8, abs_path, self.abs_root_path));
// NOTE: expects path to be normalized as well
}

const key = abs_path;
const key = self.prepareAbsPathForHash(abs_path);

const entry = self.exclusions.get(key);
// if path matches an excluded path and the excluded path was
// entered after the path in the package.json/workspaces array
// then it should be excluded
const is_excluded = if (entry) |entry_index| entry_index > index else false;

bun.Output.debug("path: {s} - {s}excluded\n", .{ key, if (is_excluded) "" else "NOT " });
we_debug("path: {s} - {s}excluded\n", .{ key, if (is_excluded) "" else "NOT " });

return is_excluded;
}

pub fn isExclusion(glob: []const u8) bool {
// TODO: count leading !
var negation_count: u32 = 0;
while (negation_count < glob.len and glob[negation_count] == '!') : (negation_count += 1) {}
return negation_count % 2 == 1;
Expand Down Expand Up @@ -4588,10 +4598,11 @@ pub const Package = extern struct {
// ?hard to predict branch? while iterating input_paths and
// checking if .len == 0
const input_paths = allocator.alloc(StrWithIndex, arr_slice.len) catch bun.outOfMemory();
defer allocator.free(input_paths);

for (arr_slice, 0..) |item, i| {
const index: u32 = @intCast(i); // WARN: max number of workspace paths is max(u32)
// TODO: when does this get deallocated?
// TODO: when does this get deallocated if it's not an exclusion?
const input_path = try item.asStringZ(allocator) orelse {
log.addErrorFmt(source, item.loc, allocator,
\\Workspaces expects an array of strings, like:
Expand All @@ -4608,22 +4619,19 @@ pub const Package = extern struct {
}

if (WorkspaceExclusions.isExclusion(input_path)) {
workspace_exclusions.append(log, source, loc, input_path, index) catch bun.outOfMemory();
workspace_exclusions.insert(log, source, loc, input_path, index) catch bun.outOfMemory();
input_paths[i].str.len = 0; // signal no path
allocator.free(input_path);
continue;
}

// TODO: remove
bun.Output.debug("not exclusion: {s}", .{input_path});

if (bun.glob.detectGlobSyntax(input_path)) {
workspace_globs.append(.{.str = input_path, .index = index}) catch bun.outOfMemory();
workspace_globs.append(.{ .str = input_path, .index = index }) catch bun.outOfMemory();
input_paths[i].str.len = 0; // signal no path
continue;
}

input_paths[i] = .{.str = input_path, .index = index};
input_paths[i] = .{ .str = input_path, .index = index };
}

for (input_paths, arr_slice) |input_path, item| {
Expand Down

0 comments on commit 48c1d69

Please sign in to comment.