Skip to content

Commit 9e43d8f

Browse files
committed
package manager: allow overriding deps with local cache
1 parent 0a70455 commit 9e43d8f

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
@@ -369,6 +369,24 @@ pub fn main() !void {
369369
if (steps_menu)
370370
return steps(builder, stdout_writer);
371371

372+
inline for (dependencies.root_deps) |dep| {
373+
if (!@hasDecl(dependencies.packages, dep[1])) continue;
374+
const pkg = @field(dependencies.packages, dep[1]);
375+
if (!@hasDecl(pkg, "local_override")) continue;
376+
const allocator = single_threaded_arena.allocator();
377+
const from = try std.process.getCwdAlloc(allocator);
378+
const relative_path = try std.fs.path.relative(allocator, from, pkg.build_root);
379+
if (pkg.local_override) {
380+
if (builder.graph.system_package_mode) {
381+
std.log.warn("ignoring local override of '{s}' in system mode, using '{s}'", .{
382+
dep[0], pkg.build_root,
383+
});
384+
} else {
385+
std.log.warn("overriding dependency '{s}' with '{s}'", .{ dep[0], relative_path });
386+
}
387+
}
388+
}
389+
372390
var run: Run = .{
373391
.max_rss = max_rss,
374392
.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,
@@ -2277,6 +2314,7 @@ const TestFetchBuilder = struct {
22772314
.allow_missing_paths_field = false,
22782315

22792316
.package_root = undefined,
2317+
.local_override = false,
22802318
.error_bundle = undefined,
22812319
.manifest = null,
22822320
.manifest_ast = undefined,

src/main.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5065,6 +5065,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50655065
.http_client = &http_client,
50665066
.thread_pool = &thread_pool,
50675067
.global_cache = global_cache_directory,
5068+
.local_cache = local_cache_directory,
50685069
.read_only = false,
50695070
.recursive = true,
50705071
.debug_hash = false,
@@ -5107,6 +5108,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
51075108
.use_latest_commit = false,
51085109

51095110
.package_root = undefined,
5111+
.local_override = false,
51105112
.error_bundle = undefined,
51115113
.manifest = null,
51125114
.manifest_ast = undefined,
@@ -6999,6 +7001,7 @@ fn cmdFetch(
69997001
.http_client = &http_client,
70007002
.thread_pool = &thread_pool,
70017003
.global_cache = global_cache_directory,
7004+
.local_cache = null,
70027005
.recursive = false,
70037006
.read_only = false,
70047007
.debug_hash = debug_hash,
@@ -7022,6 +7025,7 @@ fn cmdFetch(
70227025
.use_latest_commit = true,
70237026

70247027
.package_root = undefined,
7028+
.local_override = false,
70257029
.error_bundle = undefined,
70267030
.manifest = null,
70277031
.manifest_ast = undefined,

0 commit comments

Comments
 (0)