Skip to content

Commit 48f664f

Browse files
committed
package manager: allow overriding deps with local cache
1 parent a916bc7 commit 48f664f

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

lib/compiler/build_runner.zig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,24 @@ pub fn main() !void {
373373
if (steps_menu)
374374
return steps(builder, stdout_writer);
375375

376+
inline for (dependencies.root_deps) |dep| {
377+
if (!@hasDecl(dependencies.packages, dep[1])) continue;
378+
const pkg = @field(dependencies.packages, dep[1]);
379+
if (!@hasDecl(pkg, "local_override")) continue;
380+
const allocator = single_threaded_arena.allocator();
381+
const from = try std.process.getCwdAlloc(allocator);
382+
const relative_path = try std.fs.path.relative(allocator, from, pkg.build_root);
383+
if (pkg.local_override) {
384+
if (builder.graph.system_package_mode) {
385+
std.log.warn("ignoring local override of '{s}' in system mode, using '{s}'", .{
386+
dep[0], pkg.build_root,
387+
});
388+
} else {
389+
std.log.warn("overriding dependency '{s}' with '{s}'", .{ dep[0], relative_path });
390+
}
391+
}
392+
}
393+
376394
var run: Run = .{
377395
.max_rss = max_rss,
378396
.max_rss_is_default = false,

src/Package/Fetch.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use_latest_commit: bool,
5353
/// This will either be relative to `global_cache`, or to the build root of
5454
/// the root package.
5555
package_root: Cache.Path,
56+
local_override: bool,
5657
error_bundle: ErrorBundle.Wip,
5758
manifest: ?Manifest,
5859
manifest_ast: std.zig.Ast,
@@ -97,6 +98,7 @@ pub const JobQueue = struct {
9798
thread_pool: *ThreadPool,
9899
wait_group: WaitGroup = .{},
99100
global_cache: Cache.Directory,
101+
local_cache: ?Cache.Directory,
100102
/// If true then, no fetching occurs, and:
101103
/// * The `global_cache` directory is assumed to be the direct parent
102104
/// directory of on-disk packages rather than having the "p/" directory
@@ -198,6 +200,11 @@ pub const JobQueue = struct {
198200
}
199201
}
200202

203+
try buf.writer().print(
204+
\\ pub const local_override = {};
205+
\\
206+
, .{fetch.local_override});
207+
201208
try buf.writer().print(
202209
\\ pub const build_root = "{q}";
203210
\\
@@ -372,6 +379,35 @@ pub fn run(f: *Fetch) RunError!void {
372379
const prefixed_pkg_sub_path = "p" ++ s ++ expected_hash;
373380
const prefix_len: usize = if (f.job_queue.read_only) "p/".len else 0;
374381
const pkg_sub_path = prefixed_pkg_sub_path[prefix_len..];
382+
383+
if (f.job_queue.local_cache) |local_cache| local: {
384+
const local_cache_path = if (f.job_queue.read_only) prefixed_pkg_sub_path else pkg_sub_path;
385+
if (local_cache.handle.access(local_cache_path, .{})) |_| {
386+
f.local_override = true;
387+
if (f.job_queue.read_only) break :local;
388+
389+
assert(f.lazy_status != .unavailable);
390+
f.package_root = .{
391+
.root_dir = local_cache,
392+
.sub_path = try arena.dupe(u8, local_cache_path),
393+
};
394+
try loadManifest(f, f.package_root);
395+
try checkBuildFileExistence(f);
396+
if (!f.job_queue.recursive) return;
397+
return queueJobsForDeps(f);
398+
} else |err| switch (err) {
399+
error.FileNotFound => {},
400+
else => |e| {
401+
try eb.addRootErrorMessage(.{
402+
.msg = try eb.printString("unable to open local package cache directory '{}{s}': {s}", .{
403+
local_cache, local_cache_path, @errorName(e),
404+
}),
405+
});
406+
return error.FetchFailed;
407+
},
408+
}
409+
}
410+
375411
if (cache_root.handle.access(pkg_sub_path, .{})) |_| {
376412
assert(f.lazy_status != .unavailable);
377413
f.package_root = .{
@@ -708,6 +744,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
708744
.use_latest_commit = false,
709745

710746
.package_root = undefined,
747+
.local_override = false,
711748
.error_bundle = undefined,
712749
.manifest = null,
713750
.manifest_ast = undefined,
@@ -2279,6 +2316,7 @@ const TestFetchBuilder = struct {
22792316
.allow_missing_paths_field = false,
22802317

22812318
.package_root = undefined,
2319+
.local_override = false,
22822320
.error_bundle = undefined,
22832321
.manifest = null,
22842322
.manifest_ast = undefined,

src/main.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5043,6 +5043,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50435043
.http_client = &http_client,
50445044
.thread_pool = &thread_pool,
50455045
.global_cache = global_cache_directory,
5046+
.local_cache = local_cache_directory,
50465047
.read_only = false,
50475048
.recursive = true,
50485049
.debug_hash = false,
@@ -5085,6 +5086,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50855086
.use_latest_commit = false,
50865087

50875088
.package_root = undefined,
5089+
.local_override = false,
50885090
.error_bundle = undefined,
50895091
.manifest = null,
50905092
.manifest_ast = undefined,
@@ -6902,6 +6904,7 @@ fn cmdFetch(
69026904
.http_client = &http_client,
69036905
.thread_pool = &thread_pool,
69046906
.global_cache = global_cache_directory,
6907+
.local_cache = null,
69056908
.recursive = false,
69066909
.read_only = false,
69076910
.debug_hash = debug_hash,
@@ -6925,6 +6928,7 @@ fn cmdFetch(
69256928
.use_latest_commit = true,
69266929

69276930
.package_root = undefined,
6931+
.local_override = false,
69286932
.error_bundle = undefined,
69296933
.manifest = null,
69306934
.manifest_ast = undefined,

0 commit comments

Comments
 (0)