Skip to content

Commit c821f88

Browse files
committed
package manager: allow overriding deps with local cache
1 parent 04e08ea commit c821f88

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
@@ -335,6 +335,24 @@ pub fn main() !void {
335335
if (steps_menu)
336336
return steps(builder, stdout_writer);
337337

338+
inline for (dependencies.root_deps) |dep| {
339+
if (!@hasDecl(dependencies.packages, dep[1])) continue;
340+
const pkg = @field(dependencies.packages, dep[1]);
341+
if (!@hasDecl(pkg, "local_override")) continue;
342+
const allocator = single_threaded_arena.allocator();
343+
const from = try std.process.getCwdAlloc(allocator);
344+
const relative_path = try std.fs.path.relative(allocator, from, pkg.build_root);
345+
if (pkg.local_override) {
346+
if (builder.graph.system_package_mode) {
347+
std.log.warn("ignoring local override of '{s}' in system mode, using '{s}'", .{
348+
dep[0], pkg.build_root,
349+
});
350+
} else {
351+
std.log.warn("overriding dependency '{s}' with '{s}'", .{ dep[0], relative_path });
352+
}
353+
}
354+
}
355+
338356
var run: Run = .{
339357
.max_rss = max_rss,
340358
.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,
@@ -2276,6 +2313,7 @@ const TestFetchBuilder = struct {
22762313
.allow_missing_paths_field = false,
22772314

22782315
.package_root = undefined,
2316+
.local_override = false,
22792317
.error_bundle = undefined,
22802318
.manifest = null,
22812319
.manifest_ast = undefined,

src/main.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4990,6 +4990,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
49904990
.http_client = &http_client,
49914991
.thread_pool = &thread_pool,
49924992
.global_cache = global_cache_directory,
4993+
.local_cache = local_cache_directory,
49934994
.read_only = false,
49944995
.recursive = true,
49954996
.debug_hash = false,
@@ -5032,6 +5033,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50325033
.use_latest_commit = false,
50335034

50345035
.package_root = undefined,
5036+
.local_override = false,
50355037
.error_bundle = undefined,
50365038
.manifest = null,
50375039
.manifest_ast = undefined,
@@ -6897,6 +6899,7 @@ fn cmdFetch(
68976899
.http_client = &http_client,
68986900
.thread_pool = &thread_pool,
68996901
.global_cache = global_cache_directory,
6902+
.local_cache = null,
69006903
.recursive = false,
69016904
.read_only = false,
69026905
.debug_hash = debug_hash,
@@ -6920,6 +6923,7 @@ fn cmdFetch(
69206923
.use_latest_commit = true,
69216924

69226925
.package_root = undefined,
6926+
.local_override = false,
69236927
.error_bundle = undefined,
69246928
.manifest = null,
69256929
.manifest_ast = undefined,

0 commit comments

Comments
 (0)