Skip to content

Commit b0976d9

Browse files
committed
package manager: allow overriding deps with local cache
1 parent 833d4c9 commit b0976d9

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

lib/compiler/build_runner.zig

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

387+
inline for (dependencies.root_deps) |dep| {
388+
if (!@hasDecl(dependencies.packages, dep[1])) continue;
389+
const pkg = @field(dependencies.packages, dep[1]);
390+
if (!@hasDecl(pkg, "local_override")) continue;
391+
const allocator = single_threaded_arena.allocator();
392+
const from = try std.process.getCwdAlloc(allocator);
393+
const relative_path = try std.fs.path.relative(allocator, from, pkg.build_root);
394+
if (pkg.local_override) {
395+
if (builder.graph.system_package_mode) {
396+
std.log.warn("ignoring local override of '{s}' in system mode, using '{s}'", .{
397+
dep[0], pkg.build_root,
398+
});
399+
} else {
400+
std.log.warn("overriding dependency '{s}' with '{s}'", .{ dep[0], relative_path });
401+
}
402+
}
403+
}
404+
387405
var run: Run = .{
388406
.max_rss = max_rss,
389407
.max_rss_is_default = false,

src/Package/Fetch.zig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use_latest_commit: bool,
5555
/// This will either be relative to `global_cache`, or to the build root of
5656
/// the root package.
5757
package_root: Cache.Path,
58+
local_override: bool,
5859
error_bundle: ErrorBundle.Wip,
5960
manifest: ?Manifest,
6061
manifest_ast: std.zig.Ast,
@@ -99,6 +100,7 @@ pub const JobQueue = struct {
99100
thread_pool: *ThreadPool,
100101
wait_group: WaitGroup = .{},
101102
global_cache: Cache.Directory,
103+
local_cache: ?Cache.Directory,
102104
/// If true then, no fetching occurs, and:
103105
/// * The `global_cache` directory is assumed to be the direct parent
104106
/// directory of on-disk packages rather than having the "p/" directory
@@ -210,6 +212,11 @@ pub const JobQueue = struct {
210212
}
211213
}
212214

215+
try buf.writer().print(
216+
\\ pub const local_override = {};
217+
\\
218+
, .{fetch.local_override});
219+
213220
try buf.writer().print(
214221
\\ pub const build_root = "{q}";
215222
\\
@@ -398,6 +405,35 @@ pub fn run(f: *Fetch) RunError!void {
398405
const prefixed_pkg_sub_path = prefixed_pkg_sub_path_buffer[0 .. 2 + hash_slice.len];
399406
const prefix_len: usize = if (f.job_queue.read_only) "p/".len else 0;
400407
const pkg_sub_path = prefixed_pkg_sub_path[prefix_len..];
408+
409+
if (f.job_queue.local_cache) |local_cache| local: {
410+
const local_cache_path = if (f.job_queue.read_only) prefixed_pkg_sub_path else pkg_sub_path;
411+
if (local_cache.handle.access(local_cache_path, .{})) |_| {
412+
f.local_override = true;
413+
if (f.job_queue.read_only) break :local;
414+
415+
assert(f.lazy_status != .unavailable);
416+
f.package_root = .{
417+
.root_dir = local_cache,
418+
.sub_path = try arena.dupe(u8, local_cache_path),
419+
};
420+
try loadManifest(f, f.package_root);
421+
try checkBuildFileExistence(f);
422+
if (!f.job_queue.recursive) return;
423+
return queueJobsForDeps(f);
424+
} else |err| switch (err) {
425+
error.FileNotFound => {},
426+
else => |e| {
427+
try eb.addRootErrorMessage(.{
428+
.msg = try eb.printString("unable to open local package cache directory '{}{s}': {s}", .{
429+
local_cache, local_cache_path, @errorName(e),
430+
}),
431+
});
432+
return error.FetchFailed;
433+
},
434+
}
435+
}
436+
401437
if (cache_root.handle.access(pkg_sub_path, .{})) |_| {
402438
assert(f.lazy_status != .unavailable);
403439
f.package_root = .{
@@ -777,6 +813,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
777813
.use_latest_commit = false,
778814

779815
.package_root = undefined,
816+
.local_override = false,
780817
.error_bundle = undefined,
781818
.manifest = null,
782819
.manifest_ast = undefined,
@@ -2332,6 +2369,7 @@ const TestFetchBuilder = struct {
23322369
.http_client = &self.http_client,
23332370
.thread_pool = &self.thread_pool,
23342371
.global_cache = self.global_cache_directory,
2372+
.local_cache = null,
23352373
.recursive = false,
23362374
.read_only = false,
23372375
.debug_hash = false,
@@ -2357,6 +2395,7 @@ const TestFetchBuilder = struct {
23572395
.use_latest_commit = true,
23582396

23592397
.package_root = undefined,
2398+
.local_override = false,
23602399
.error_bundle = undefined,
23612400
.manifest = null,
23622401
.manifest_ast = undefined,

src/main.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5313,6 +5313,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
53135313
.http_client = &http_client,
53145314
.thread_pool = &thread_pool,
53155315
.global_cache = global_cache_directory,
5316+
.local_cache = local_cache_directory,
53165317
.read_only = false,
53175318
.recursive = true,
53185319
.debug_hash = false,
@@ -5358,6 +5359,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
53585359
.use_latest_commit = false,
53595360

53605361
.package_root = undefined,
5362+
.local_override = false,
53615363
.error_bundle = undefined,
53625364
.manifest = null,
53635365
.manifest_ast = undefined,
@@ -7247,6 +7249,7 @@ fn cmdFetch(
72477249
.http_client = &http_client,
72487250
.thread_pool = &thread_pool,
72497251
.global_cache = global_cache_directory,
7252+
.local_cache = null,
72507253
.recursive = false,
72517254
.read_only = false,
72527255
.debug_hash = debug_hash,
@@ -7273,6 +7276,7 @@ fn cmdFetch(
72737276
.use_latest_commit = true,
72747277

72757278
.package_root = undefined,
7279+
.local_override = false,
72767280
.error_bundle = undefined,
72777281
.manifest = null,
72787282
.manifest_ast = undefined,

0 commit comments

Comments
 (0)