authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-04-21 18:06:13+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-04-21 18:06:13+02:00
logb5d8966e05926c31b16cdd18ee324cf3b6381d51
treed35339bac764cbf199484e8bcec23979cacbb63e
parent3252a0553175690ff8a2f0aa8a5697c8344ec59c
parent74b56501b3ac7421a7ae7bda177b7d1b737242dd

Merge pull request 'package fetching fixes and enhancements' (#31992) from fetch-enhancements into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31992

3 files changed, 157 insertions(+), 114 deletions(-)

lib/std/zig.zig+1
...@@ -744,6 +744,7 @@ pub fn parseTargetQueryOrReportFatalError(...@@ -744,6 +744,7 @@ pub fn parseTargetQueryOrReportFatalError(
744pub const EnvVar = enum {744pub const EnvVar = enum {
745 ZIG_GLOBAL_CACHE_DIR,745 ZIG_GLOBAL_CACHE_DIR,
746 ZIG_LOCAL_CACHE_DIR,746 ZIG_LOCAL_CACHE_DIR,
747 ZIG_LOCAL_PKG_DIR,
747 ZIG_LIB_DIR,748 ZIG_LIB_DIR,
748 ZIG_LIBC,749 ZIG_LIBC,
749 ZIG_BUILD_RUNNER,750 ZIG_BUILD_RUNNER,
src/Package/Fetch.zig+81-69
...@@ -56,6 +56,9 @@ location_tok: std.zig.Ast.TokenIndex,...@@ -56,6 +56,9 @@ location_tok: std.zig.Ast.TokenIndex,
56hash_tok: std.zig.Ast.OptionalTokenIndex,56hash_tok: std.zig.Ast.OptionalTokenIndex,
57name_tok: std.zig.Ast.TokenIndex,57name_tok: std.zig.Ast.TokenIndex,
58lazy_status: LazyStatus,58lazy_status: LazyStatus,
59/// Same as `parent_packge_root` except it is unchanged when recursing into
60/// relative file paths (as opposed to URL).
61remote_package_root: Cache.Path,
59parent_package_root: Cache.Path,62parent_package_root: Cache.Path,
60parent_manifest_ast: ?*const std.zig.Ast,63parent_manifest_ast: ?*const std.zig.Ast,
61prog_node: std.Progress.Node,64prog_node: std.Progress.Node,
...@@ -104,6 +107,12 @@ pub const LazyStatus = enum {...@@ -104,6 +107,12 @@ pub const LazyStatus = enum {
104 unavailable,107 unavailable,
105};108};
106109
110pub const LocalStorage = struct {
111 cache_root: Cache.Path,
112 /// Path to "zig-pkg" inside the package in which the user ran `zig build`.
113 pkg_root: Cache.Path,
114};
115
107/// Contains shared state among all `Fetch` tasks.116/// Contains shared state among all `Fetch` tasks.
108pub const JobQueue = struct {117pub const JobQueue = struct {
109 io: Io,118 io: Io,
...@@ -122,9 +131,8 @@ pub const JobQueue = struct {...@@ -122,9 +131,8 @@ pub const JobQueue = struct {
122 /// This tracks `Fetch` tasks as well as recompression tasks.131 /// This tracks `Fetch` tasks as well as recompression tasks.
123 group: Io.Group = .init,132 group: Io.Group = .init,
124 global_cache: Cache.Directory,133 global_cache: Cache.Directory,
125 local_cache: Cache.Path,134 /// If `null`, indicates fetch globally only.
126 /// Path to "zig-pkg" inside the package in which the user ran `zig build`.135 local_storage: ?*const LocalStorage,
127 root_pkg_path: Cache.Path,
128 /// If true then, no fetching occurs, and:136 /// If true then, no fetching occurs, and:
129 /// * The `global_cache` directory is assumed to be the direct parent137 /// * The `global_cache` directory is assumed to be the direct parent
130 /// directory of on-disk packages rather than having the "p/" directory138 /// directory of on-disk packages rather than having the "p/" directory
...@@ -341,7 +349,7 @@ pub const JobQueue = struct {...@@ -341,7 +349,7 @@ pub const JobQueue = struct {
341 );349 );
342 }350 }
343351
344 fn recompress(jq: *JobQueue, package_hash: Package.Hash) Io.Cancelable!void {352 fn recompress(jq: *JobQueue, package_hash: Package.Hash, package_root: Cache.Path) Io.Cancelable!void {
345 const pkg_hash_slice = package_hash.toSlice();353 const pkg_hash_slice = package_hash.toSlice();
346354
347 const prog_node = jq.prog_node.startFmt(0, "recompress {s}", .{pkg_hash_slice});355 const prog_node = jq.prog_node.startFmt(0, "recompress {s}", .{pkg_hash_slice});
...@@ -359,7 +367,7 @@ pub const JobQueue = struct {...@@ -359,7 +367,7 @@ pub const JobQueue = struct {
359 defer arena_instance.deinit();367 defer arena_instance.deinit();
360 const arena = arena_instance.allocator();368 const arena = arena_instance.allocator();
361369
362 recompressFallible(jq, arena, dest_path, pkg_hash_slice, prog_node) catch |err| switch (err) {370 recompressFallible(jq, arena, dest_path, pkg_hash_slice, package_root, prog_node) catch |err| switch (err) {
363 error.Canceled => |e| return e,371 error.Canceled => |e| return e,
364 error.ReadFailed => comptime unreachable,372 error.ReadFailed => comptime unreachable,
365 error.WriteFailed => comptime unreachable,373 error.WriteFailed => comptime unreachable,
...@@ -372,6 +380,7 @@ pub const JobQueue = struct {...@@ -372,6 +380,7 @@ pub const JobQueue = struct {
372 arena: Allocator,380 arena: Allocator,
373 dest_path: Cache.Path,381 dest_path: Cache.Path,
374 pkg_hash_slice: []const u8,382 pkg_hash_slice: []const u8,
383 package_root: Cache.Path,
375 prog_node: std.Progress.Node,384 prog_node: std.Progress.Node,
376 ) !void {385 ) !void {
377 const gpa = jq.http_client.allocator;386 const gpa = jq.http_client.allocator;
...@@ -386,7 +395,7 @@ pub const JobQueue = struct {...@@ -386,7 +395,7 @@ pub const JobQueue = struct {
386 var scanned_files: std.ArrayList(ScannedFile) = .empty;395 var scanned_files: std.ArrayList(ScannedFile) = .empty;
387 defer scanned_files.deinit(gpa);396 defer scanned_files.deinit(gpa);
388397
389 var pkg_dir = try jq.root_pkg_path.openDir(io, pkg_hash_slice, .{ .iterate = true });398 var pkg_dir = try package_root.root_dir.handle.openDir(io, package_root.sub_path, .{ .iterate = true });
390 defer pkg_dir.close(io);399 defer pkg_dir.close(io);
391400
392 {401 {
...@@ -513,7 +522,6 @@ pub fn run(f: *Fetch) RunError!void {...@@ -513,7 +522,6 @@ pub fn run(f: *Fetch) RunError!void {
513 const eb = &f.error_bundle;522 const eb = &f.error_bundle;
514 const arena = f.arena.allocator();523 const arena = f.arena.allocator();
515 const gpa = f.arena.child_allocator;524 const gpa = f.arena.child_allocator;
516 const local_cache_root = job_queue.local_cache;
517525
518 try eb.init(gpa);526 try eb.init(gpa);
519527
...@@ -534,32 +542,19 @@ pub fn run(f: *Fetch) RunError!void {...@@ -534,32 +542,19 @@ pub fn run(f: *Fetch) RunError!void {
534 );542 );
535 // Packages fetched by URL may not use relative paths to escape outside the543 // Packages fetched by URL may not use relative paths to escape outside the
536 // fetched package directory from within the package cache.544 // fetched package directory from within the package cache.
537 if (pkg_root.root_dir.eql(local_cache_root.root_dir)) {545
538 // `parent_package_root.sub_path` contains a path like this:546 // This code path is only reachable recursively and the sub_path
539 // "p/$hash", or547 // will already have been resolved to no longer have extra ".." or
540 // "p/$hash/foo", with possibly more directories after "foo".548 // "." components.
541 // We want to fail unless the resolved relative path has a549 assert(job_queue.local_storage != null);
542 // prefix of "p/$hash/".550 log.debug("checking pkg root \"{s}\" against parent package root \"{s}\"", .{
543 const prefix_len: usize = if (job_queue.read_only) 0 else "p/".len;551 pkg_root.sub_path, f.remote_package_root.sub_path,
544 const parent_sub_path = f.parent_package_root.sub_path;552 });
545 const end = find_end: {553 assert(pkg_root.root_dir.eql(f.remote_package_root.root_dir));
546 if (parent_sub_path.len > prefix_len) {554 if (!std.mem.startsWith(u8, pkg_root.sub_path, f.remote_package_root.sub_path)) return f.fail(
547 // Use `isSep` instead of `indexOfScalarPos` to account for555 f.location_tok,
548 // Windows accepting both `\` and `/` as path separators.556 try eb.printString("dependency path outside project: '{f}'", .{pkg_root}),
549 for (parent_sub_path[prefix_len..], prefix_len..) |c, i| {557 );
550 if (std.fs.path.isSep(c)) break :find_end i;
551 }
552 }
553 break :find_end parent_sub_path.len;
554 };
555 const expected_prefix = parent_sub_path[0..end];
556 if (!std.mem.startsWith(u8, pkg_root.sub_path, expected_prefix)) {
557 return f.fail(
558 f.location_tok,
559 try eb.printString("dependency path outside project: '{f}'", .{pkg_root}),
560 );
561 }
562 }
563 f.package_root = pkg_root;558 f.package_root = pkg_root;
564 try loadManifest(f, pkg_root);559 try loadManifest(f, pkg_root);
565 if (!f.has_build_zig) try checkBuildFileExistence(f);560 if (!f.has_build_zig) try checkBuildFileExistence(f);
...@@ -602,6 +597,7 @@ pub fn run(f: *Fetch) RunError!void {...@@ -602,6 +597,7 @@ pub fn run(f: *Fetch) RunError!void {
602 log.debug("using fork {f} for {s}", .{ fork.path, fork.manifest.name });597 log.debug("using fork {f} for {s}", .{ fork.path, fork.manifest.name });
603 fork.uses += 1;598 fork.uses += 1;
604 f.package_root = fork.path;599 f.package_root = fork.path;
600 f.remote_package_root = f.package_root;
605 f.manifest_ast = fork.manifest_ast;601 f.manifest_ast = fork.manifest_ast;
606 f.manifest = fork.manifest;602 f.manifest = fork.manifest;
607 f.have_manifest = true;603 f.have_manifest = true;
...@@ -610,31 +606,34 @@ pub fn run(f: *Fetch) RunError!void {...@@ -610,31 +606,34 @@ pub fn run(f: *Fetch) RunError!void {
610 return queueJobsForDeps(f);606 return queueJobsForDeps(f);
611 }607 }
612608
613 const package_root = try job_queue.root_pkg_path.join(arena, expected_hash.toSlice());609 if (job_queue.local_storage) |ls| {
614 if (package_root.root_dir.handle.access(io, package_root.sub_path, .{})) |_| {610 const package_root = try ls.pkg_root.join(arena, expected_hash.toSlice());
615 assert(f.lazy_status != .unavailable);611 if (package_root.root_dir.handle.access(io, package_root.sub_path, .{})) |_| {
616 f.package_root = package_root;612 assert(f.lazy_status != .unavailable);
617 try loadManifest(f, f.package_root);613 f.package_root = package_root;
618 try checkBuildFileExistence(f);614 f.remote_package_root = f.package_root;
619 if (!job_queue.recursive) return;615 try loadManifest(f, f.package_root);
620 return queueJobsForDeps(f);616 try checkBuildFileExistence(f);
621 } else |err| switch (err) {617 if (!job_queue.recursive) return;
622 error.FileNotFound => {618 return queueJobsForDeps(f);
623 log.debug("FileNotFound: {f}", .{package_root});619 } else |err| switch (err) {
624 if (job_queue.read_only and f.lazy_status == .eager) return f.fail(620 error.FileNotFound => {
625 f.name_tok,621 log.debug("FileNotFound: {f}", .{package_root});
626 try eb.printString("package not found at '{f}'", .{package_root}),622 if (job_queue.read_only and f.lazy_status == .eager) return f.fail(
627 );623 f.name_tok,
628 },624 try eb.printString("package not found at '{f}'", .{package_root}),
629 error.Canceled => |e| return e,625 );
630 else => |e| {626 },
631 try eb.addRootErrorMessage(.{627 error.Canceled => |e| return e,
632 .msg = try eb.printString("unable to open package cache directory {f}: {t}", .{628 else => |e| {
633 package_root, e,629 try eb.addRootErrorMessage(.{
634 }),630 .msg = try eb.printString("unable to open package cache directory {f}: {t}", .{
635 });631 package_root, e,
636 return error.FetchFailed;632 }),
637 },633 });
634 return error.FetchFailed;
635 },
636 }
638 }637 }
639638
640 // Check global cache before remote fetch.639 // Check global cache before remote fetch.
...@@ -713,7 +712,14 @@ fn runResource(...@@ -713,7 +712,14 @@ fn runResource(
713 break :r x;712 break :r x;
714 };713 };
715 const tmp_dir_sub_path = ".tmp-" ++ std.fmt.hex(rand_int);714 const tmp_dir_sub_path = ".tmp-" ++ std.fmt.hex(rand_int);
716 const tmp_directory_path = try job_queue.root_pkg_path.join(arena, tmp_dir_sub_path);715 const tmp_tmp_dir_sub_path = "tmp/" ++ tmp_dir_sub_path;
716 const tmp_directory_path: Cache.Path = if (job_queue.local_storage) |ls|
717 try ls.pkg_root.join(arena, tmp_dir_sub_path)
718 else
719 .{
720 .root_dir = job_queue.global_cache,
721 .sub_path = tmp_tmp_dir_sub_path,
722 };
717723
718 const package_sub_path = blk: {724 const package_sub_path = blk: {
719 var tmp_directory: Cache.Directory = .{725 var tmp_directory: Cache.Directory = .{
...@@ -772,19 +778,24 @@ fn runResource(...@@ -772,19 +778,24 @@ fn runResource(
772 // zig package directory untouched as it may be in use. This is done even778 // zig package directory untouched as it may be in use. This is done even
773 // if the hash is invalid, in case the package with the different hash is779 // if the hash is invalid, in case the package with the different hash is
774 // used in the future.780 // used in the future.
775 f.package_root = try job_queue.root_pkg_path.join(arena, computed_package_hash.toSlice());781 if (job_queue.local_storage) |ls| {
776 renameTmpIntoCache(io, package_sub_path, f.package_root) catch |err| {782 f.package_root = try ls.pkg_root.join(arena, computed_package_hash.toSlice());
777 try eb.addRootErrorMessage(.{ .msg = try eb.printString(783 renameTmpIntoCache(io, package_sub_path, f.package_root) catch |err| {
778 "unable to rename temporary directory {f} into package cache directory {f}: {t}",784 try eb.addRootErrorMessage(.{ .msg = try eb.printString(
779 .{ package_sub_path, f.package_root, err },785 "unable to rename temporary directory {f} into package cache directory {f}: {t}",
780 ) });786 .{ package_sub_path, f.package_root, err },
781 return error.FetchFailed;787 ) });
782 };788 return error.FetchFailed;
789 };
790 } else {
791 f.package_root = tmp_directory_path;
792 }
793 f.remote_package_root = f.package_root;
783794
784 if (!disable_recompress) {795 if (!disable_recompress) {
785 // Spin off a task to recompress the tarball, with filtered files deleted, into796 // Spin off a task to recompress the tarball, with filtered files deleted, into
786 // the global cache.797 // the global cache.
787 job_queue.group.async(io, JobQueue.recompress, .{ job_queue, computed_package_hash });798 job_queue.group.async(io, JobQueue.recompress, .{ job_queue, computed_package_hash, f.package_root });
788 }799 }
789800
790 // Remove temporary directory root if not already renamed to global cache.801 // Remove temporary directory root if not already renamed to global cache.
...@@ -991,6 +1002,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {...@@ -991,6 +1002,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
991 .all => .eager,1002 .all => .eager,
992 },1003 },
993 .parent_package_root = f.package_root,1004 .parent_package_root = f.package_root,
1005 .remote_package_root = f.remote_package_root,
994 .parent_manifest_ast = &f.manifest_ast,1006 .parent_manifest_ast = &f.manifest_ast,
995 .prog_node = f.prog_node,1007 .prog_node = f.prog_node,
996 .job_queue = f.job_queue,1008 .job_queue = f.job_queue,
...@@ -1185,7 +1197,7 @@ fn initResource(f: *Fetch, uri: std.Uri, resource: *Resource, reader_buffer: []u...@@ -1185,7 +1197,7 @@ fn initResource(f: *Fetch, uri: std.Uri, resource: *Resource, reader_buffer: []u
1185 if (ascii.eqlIgnoreCase(uri.scheme, "file")) {1197 if (ascii.eqlIgnoreCase(uri.scheme, "file")) {
1186 const path = try uri.path.toRawMaybeAlloc(arena);1198 const path = try uri.path.toRawMaybeAlloc(arena);
1187 const file = f.parent_package_root.openFile(io, path, .{}) catch |err| {1199 const file = f.parent_package_root.openFile(io, path, .{}) catch |err| {
1188 return f.fail(f.location_tok, try eb.printString("unable to open '{f}{s}': {t}", .{1200 return f.fail(f.location_tok, try eb.printString("unable to open {f}/{s}: {t}", .{
1189 f.parent_package_root, path, err,1201 f.parent_package_root, path, err,
1190 }));1202 }));
1191 };1203 };
src/main.zig+75-45
...@@ -1359,12 +1359,7 @@ fn buildOutputType(...@@ -1359,12 +1359,7 @@ fn buildOutputType(
1359 } else if (mem.eql(u8, arg, "--zig-lib-dir")) {1359 } else if (mem.eql(u8, arg, "--zig-lib-dir")) {
1360 override_lib_dir = args_iter.nextOrFatal();1360 override_lib_dir = args_iter.nextOrFatal();
1361 } else if (mem.eql(u8, arg, "--debug-log")) {1361 } else if (mem.eql(u8, arg, "--debug-log")) {
1362 if (!build_options.enable_logging) {1362 try addDebugLog(arena, args_iter.nextOrFatal());
1363 warn("Zig was compiled without logging enabled (-Dlog). --debug-log has no effect.", .{});
1364 _ = args_iter.nextOrFatal();
1365 } else {
1366 try log_scopes.append(arena, args_iter.nextOrFatal());
1367 }
1368 } else if (mem.eql(u8, arg, "--listen")) {1363 } else if (mem.eql(u8, arg, "--listen")) {
1369 const next_arg = args_iter.nextOrFatal();1364 const next_arg = args_iter.nextOrFatal();
1370 if (mem.eql(u8, next_arg, "-")) {1365 if (mem.eql(u8, next_arg, "-")) {
...@@ -4958,6 +4953,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,...@@ -4958,6 +4953,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
4958 var override_lib_dir: ?[]const u8 = EnvVar.ZIG_LIB_DIR.get(environ_map);4953 var override_lib_dir: ?[]const u8 = EnvVar.ZIG_LIB_DIR.get(environ_map);
4959 var override_global_cache_dir: ?[]const u8 = EnvVar.ZIG_GLOBAL_CACHE_DIR.get(environ_map);4954 var override_global_cache_dir: ?[]const u8 = EnvVar.ZIG_GLOBAL_CACHE_DIR.get(environ_map);
4960 var override_local_cache_dir: ?[]const u8 = EnvVar.ZIG_LOCAL_CACHE_DIR.get(environ_map);4955 var override_local_cache_dir: ?[]const u8 = EnvVar.ZIG_LOCAL_CACHE_DIR.get(environ_map);
4956 var override_pkg_dir: ?[]const u8 = EnvVar.ZIG_LOCAL_PKG_DIR.get(environ_map);
4961 var override_build_runner: ?[]const u8 = EnvVar.ZIG_BUILD_RUNNER.get(environ_map);4957 var override_build_runner: ?[]const u8 = EnvVar.ZIG_BUILD_RUNNER.get(environ_map);
4962 var child_argv: std.ArrayList([]const u8) = .empty;4958 var child_argv: std.ArrayList([]const u8) = .empty;
4963 var forks: std.ArrayList(Fork) = .empty;4959 var forks: std.ArrayList(Fork) = .empty;
...@@ -5048,6 +5044,11 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,...@@ -5048,6 +5044,11 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
5048 i += 1;5044 i += 1;
5049 override_local_cache_dir = args[i];5045 override_local_cache_dir = args[i];
5050 continue;5046 continue;
5047 } else if (mem.eql(u8, arg, "--pkg-dir")) {
5048 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
5049 i += 1;
5050 override_pkg_dir = args[i];
5051 continue;
5051 } else if (mem.eql(u8, arg, "--global-cache-dir")) {5052 } else if (mem.eql(u8, arg, "--global-cache-dir")) {
5052 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});5053 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
5053 i += 1;5054 i += 1;
...@@ -5092,11 +5093,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,...@@ -5092,11 +5093,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
5092 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});5093 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
5093 try child_argv.appendSlice(arena, args[i .. i + 2]);5094 try child_argv.appendSlice(arena, args[i .. i + 2]);
5094 i += 1;5095 i += 1;
5095 if (!build_options.enable_logging) {5096 try addDebugLog(arena, args[i]);
5096 warn("Zig was compiled without logging enabled (-Dlog). --debug-log has no effect.", .{});
5097 } else {
5098 try log_scopes.append(arena, args[i]);
5099 }
5100 continue;5097 continue;
5101 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {5098 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
5102 if (build_options.enable_debug_extensions) {5099 if (build_options.enable_debug_extensions) {
...@@ -5325,9 +5322,6 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,...@@ -5325,9 +5322,6 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
5325 .parent = root_mod,5322 .parent = root_mod,
5326 });5323 });
53275324
5328 var cleanup_build_dir: ?Io.Dir = null;
5329 defer if (cleanup_build_dir) |*dir| dir.close(io);
5330
5331 if (dev.env.supports(.fetch_command)) {5325 if (dev.env.supports(.fetch_command)) {
5332 const fetch_prog_node = root_prog_node.start("Fetch Packages", 0);5326 const fetch_prog_node = root_prog_node.start("Fetch Packages", 0);
5333 defer fetch_prog_node.end();5327 defer fetch_prog_node.end();
...@@ -5339,33 +5333,29 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,...@@ -5339,33 +5333,29 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
5339 .io = io,5333 .io = io,
5340 .http_client = &http_client,5334 .http_client = &http_client,
5341 .global_cache = dirs.global_cache,5335 .global_cache = dirs.global_cache,
5342 .local_cache = .{ .root_dir = dirs.local_cache, .sub_path = "" },5336 .local_storage = &.{
5343 .root_pkg_path = .{ .root_dir = build_root.directory, .sub_path = "zig-pkg" },5337 .cache_root = .{ .root_dir = dirs.local_cache, .sub_path = "" },
5344 .read_only = false,5338 .pkg_root = if (override_pkg_dir) |p|
5339 .initCwd(p)
5340 else if (system_pkg_dir_path) |p|
5341 .initCwd(p)
5342 else
5343 .{
5344 .root_dir = build_root.directory,
5345 .sub_path = "zig-pkg",
5346 },
5347 },
5345 .recursive = true,5348 .recursive = true,
5346 .debug_hash = false,5349 .debug_hash = false,
5347 .unlazy_set = unlazy_set,5350 .unlazy_set = unlazy_set,
5348 .fork_set = fork_set,5351 .fork_set = fork_set,
5349 .mode = fetch_mode,5352 .mode = fetch_mode,
5350 .prog_node = fetch_prog_node,5353 .prog_node = fetch_prog_node,
5354 .read_only = system_pkg_dir_path != null,
5351 };5355 };
5352 defer job_queue.deinit();5356 defer job_queue.deinit();
53535357
5354 if (system_pkg_dir_path) |p| {5358 if (system_pkg_dir_path == null) {
5355 const system_pkg_path: Path = .{
5356 .root_dir = .{
5357 .path = p,
5358 .handle = Io.Dir.cwd().openDir(io, p, .{}) catch |err| {
5359 fatal("unable to open system package directory '{s}': {t}", .{ p, err });
5360 },
5361 },
5362 .sub_path = "",
5363 };
5364 job_queue.global_cache = system_pkg_path.root_dir;
5365 job_queue.root_pkg_path = system_pkg_path;
5366 job_queue.read_only = true;
5367 cleanup_build_dir = job_queue.global_cache.handle;
5368 } else {
5369 try http_client.initDefaultProxies(arena, environ_map);5359 try http_client.initDefaultProxies(arena, environ_map);
5370 }5360 }
53715361
...@@ -5381,6 +5371,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,...@@ -5381,6 +5371,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
5381 .hash_tok = .none,5371 .hash_tok = .none,
5382 .name_tok = 0,5372 .name_tok = 0,
5383 .lazy_status = .eager,5373 .lazy_status = .eager,
5374 .remote_package_root = phantom_package_root,
5384 .parent_package_root = phantom_package_root,5375 .parent_package_root = phantom_package_root,
5385 .parent_manifest_ast = null,5376 .parent_manifest_ast = null,
5386 .prog_node = fetch_prog_node,5377 .prog_node = fetch_prog_node,
...@@ -5401,6 +5392,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,...@@ -5401,6 +5392,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
54015392
5402 .module = build_mod,5393 .module = build_mod,
5403 };5394 };
5395
5404 job_queue.all_fetches.appendAssumeCapacity(&fetch);5396 job_queue.all_fetches.appendAssumeCapacity(&fetch);
54055397
5406 job_queue.table.putAssumeCapacityNoClobber(5398 job_queue.table.putAssumeCapacityNoClobber(
...@@ -7032,7 +7024,10 @@ const usage_fetch =...@@ -7032,7 +7024,10 @@ const usage_fetch =
7032 \\Options:7024 \\Options:
7033 \\ -h, --help Print this help and exit7025 \\ -h, --help Print this help and exit
7034 \\ --global-cache-dir [path] Override path to global Zig cache directory7026 \\ --global-cache-dir [path] Override path to global Zig cache directory
7027 \\ --cache-dir [path] Override path to local cache directory
7028 \\ --pkg-dir [path] Override path to local package directory
7035 \\ --debug-hash Print verbose hash information to stdout7029 \\ --debug-hash Print verbose hash information to stdout
7030 \\ --debug-log [scope] Enable printing debug/info log messages for scope
7036 \\ --save Add the fetched package to build.zig.zon7031 \\ --save Add the fetched package to build.zig.zon
7037 \\ --save=[name] Add the fetched package to build.zig.zon as name7032 \\ --save=[name] Add the fetched package to build.zig.zon as name
7038 \\ --save-exact Add the fetched package to build.zig.zon, storing the URL verbatim7033 \\ --save-exact Add the fetched package to build.zig.zon, storing the URL verbatim
...@@ -7052,6 +7047,8 @@ fn cmdFetch(...@@ -7052,6 +7047,8 @@ fn cmdFetch(
7052 const color: Color = .auto;7047 const color: Color = .auto;
7053 var opt_path_or_url: ?[]const u8 = null;7048 var opt_path_or_url: ?[]const u8 = null;
7054 var override_global_cache_dir: ?[]const u8 = EnvVar.ZIG_GLOBAL_CACHE_DIR.get(environ_map);7049 var override_global_cache_dir: ?[]const u8 = EnvVar.ZIG_GLOBAL_CACHE_DIR.get(environ_map);
7050 var override_local_cache_dir: ?[]const u8 = EnvVar.ZIG_LOCAL_CACHE_DIR.get(environ_map);
7051 var override_pkg_dir: ?[]const u8 = EnvVar.ZIG_LOCAL_PKG_DIR.get(environ_map);
7055 var debug_hash: bool = false;7052 var debug_hash: bool = false;
7056 var save: union(enum) {7053 var save: union(enum) {
7057 no,7054 no,
...@@ -7068,11 +7065,23 @@ fn cmdFetch(...@@ -7068,11 +7065,23 @@ fn cmdFetch(
7068 try Io.File.stdout().writeStreamingAll(io, usage_fetch);7065 try Io.File.stdout().writeStreamingAll(io, usage_fetch);
7069 return cleanExit(io);7066 return cleanExit(io);
7070 } else if (mem.eql(u8, arg, "--global-cache-dir")) {7067 } else if (mem.eql(u8, arg, "--global-cache-dir")) {
7071 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});7068 if (i + 1 >= args.len) fatal("expected argument after: {s}", .{arg});
7072 i += 1;7069 i += 1;
7073 override_global_cache_dir = args[i];7070 override_global_cache_dir = args[i];
7071 } else if (mem.eql(u8, arg, "--cache-dir")) {
7072 if (i + 1 >= args.len) fatal("expected argument after: {s}", .{arg});
7073 i += 1;
7074 override_local_cache_dir = args[i];
7075 } else if (mem.eql(u8, arg, "--pkg-dir")) {
7076 if (i + 1 >= args.len) fatal("expected argument after: {s}", .{arg});
7077 i += 1;
7078 override_pkg_dir = args[i];
7074 } else if (mem.eql(u8, arg, "--debug-hash")) {7079 } else if (mem.eql(u8, arg, "--debug-hash")) {
7075 debug_hash = true;7080 debug_hash = true;
7081 } else if (mem.eql(u8, arg, "--debug-log")) {
7082 if (i + 1 >= args.len) fatal("expected argument after: {s}", .{arg});
7083 i += 1;
7084 try addDebugLog(arena, args[i]);
7076 } else if (mem.eql(u8, arg, "--save")) {7085 } else if (mem.eql(u8, arg, "--save")) {
7077 save = .{ .yes = null };7086 save = .{ .yes = null };
7078 } else if (mem.cutPrefix(u8, arg, "--save=")) |rest| {7087 } else if (mem.cutPrefix(u8, arg, "--save=")) |rest| {
...@@ -7113,27 +7122,39 @@ fn cmdFetch(...@@ -7113,27 +7122,39 @@ fn cmdFetch(
7113 };7122 };
7114 defer global_cache_directory.handle.close(io);7123 defer global_cache_directory.handle.close(io);
71157124
7125 var local_storage: Package.Fetch.LocalStorage = undefined;
7126 var build_root: BuildRoot = undefined;
7127 var build_root_initialized = false;
7128 defer if (build_root_initialized) build_root.deinit(io);
7129
7116 const cwd_path = try introspect.getResolvedCwd(io, arena);7130 const cwd_path = try introspect.getResolvedCwd(io, arena);
71177131
7118 var build_root = try findBuildRoot(arena, io, .{7132 const local_storage_ptr = switch (save) {
7119 .cwd_path = cwd_path,7133 .no => null,
7120 });7134 .yes, .exact => ls: {
7121 defer build_root.deinit(io);7135 build_root = try findBuildRoot(arena, io, .{ .cwd_path = cwd_path });
7136 build_root_initialized = true;
7137
7138 local_storage = .{
7139 .cache_root = if (override_local_cache_dir) |p| .initCwd(p) else .{
7140 .root_dir = build_root.directory,
7141 .sub_path = ".zig-cache",
7142 },
7143 .pkg_root = if (override_pkg_dir) |p| .initCwd(p) else .{
7144 .root_dir = build_root.directory,
7145 .sub_path = "zig-pkg",
7146 },
7147 };
71227148
7123 const local_cache_path: Path = .{7149 break :ls &local_storage;
7124 .root_dir = build_root.directory,7150 },
7125 .sub_path = ".zig-cache",
7126 };7151 };
71277152
7128 var job_queue: Package.Fetch.JobQueue = .{7153 var job_queue: Package.Fetch.JobQueue = .{
7129 .io = io,7154 .io = io,
7130 .http_client = &http_client,7155 .http_client = &http_client,
7131 .global_cache = global_cache_directory,7156 .global_cache = global_cache_directory,
7132 .local_cache = local_cache_path,7157 .local_storage = local_storage_ptr,
7133 .root_pkg_path = .{
7134 .root_dir = build_root.directory,
7135 .sub_path = "zig-pkg",
7136 },
7137 .recursive = false,7158 .recursive = false,
7138 .read_only = false,7159 .read_only = false,
7139 .debug_hash = debug_hash,7160 .debug_hash = debug_hash,
...@@ -7149,6 +7170,7 @@ fn cmdFetch(...@@ -7149,6 +7170,7 @@ fn cmdFetch(
7149 .hash_tok = .none,7170 .hash_tok = .none,
7150 .name_tok = 0,7171 .name_tok = 0,
7151 .lazy_status = .eager,7172 .lazy_status = .eager,
7173 .remote_package_root = undefined,
7152 .parent_package_root = undefined,7174 .parent_package_root = undefined,
7153 .parent_manifest_ast = null,7175 .parent_manifest_ast = null,
7154 .prog_node = root_prog_node,7176 .prog_node = root_prog_node,
...@@ -7793,3 +7815,11 @@ fn randInt(io: Io, comptime T: type) T {...@@ -7793,3 +7815,11 @@ fn randInt(io: Io, comptime T: type) T {
7793 io.random(@ptrCast(&x));7815 io.random(@ptrCast(&x));
7794 return x;7816 return x;
7795}7817}
7818
7819fn addDebugLog(arena: Allocator, scope_name: []const u8) error{OutOfMemory}!void {
7820 if (!build_options.enable_logging) {
7821 warn("Zig was compiled without logging enabled (-Dlog). --debug-log has no effect.", .{});
7822 } else {
7823 try log_scopes.append(arena, scope_name);
7824 }
7825}