authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-06 09:41:28+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-06 09:41:28+01:00
logd84a638e8b6ffeb95dfafef59e6305bd0e139d4e
tree15d475879933c9ccce5493222cd0b0b2ca261dba
parent076f7e5bd5389e159865d99e0e86edc905cffc42
parentd8171e8a2ee56e76bcd91f187d5ca5664b87bc83

Merge pull request 'fetch packages into project-local directory' (#31121) from project-local-deps into master

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

11 files changed, 360 insertions(+), 342 deletions(-)

lib/std/Io.zig+6
......@@ -1031,6 +1031,9 @@ pub const Group = struct {
10311031 /// Once this function is called, there are resources associated with the
10321032 /// group. To release those resources, `Group.await` or `Group.cancel` must
10331033 /// eventually be called.
1034 ///
1035 /// If `error.Canceled` is returned from any operation this task performs,
1036 /// it is asserted that `function` returns `error.Canceled`.
10341037 pub fn async(g: *Group, io: Io, function: anytype, args: std.meta.ArgsTuple(@TypeOf(function))) void {
10351038 const Args = @TypeOf(args);
10361039 const TypeErased = struct {
......@@ -1050,6 +1053,9 @@ pub const Group = struct {
10501053 /// Once this function is called, there are resources associated with the
10511054 /// group. To release those resources, `Group.await` or `Group.cancel` must
10521055 /// eventually be called.
1056 ///
1057 /// If `error.Canceled` is returned from any operation this task performs,
1058 /// it is asserted that `function` returns `error.Canceled`.
10531059 pub fn concurrent(g: *Group, io: Io, function: anytype, args: std.meta.ArgsTuple(@TypeOf(function))) ConcurrentError!void {
10541060 const Args = @TypeOf(args);
10551061 const TypeErased = struct {
lib/std/Io/Threaded.zig+35-45
......@@ -3191,29 +3191,24 @@ fn dirCreateDirPosix(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, perm
31913191 try syscall.checkCancel();
31923192 continue;
31933193 },
3194 else => |e| {
3195 syscall.finish();
3196 switch (e) {
3197 .ACCES => return error.AccessDenied,
3198 .BADF => |err| return errnoBug(err), // File descriptor used after closed.
3199 .PERM => return error.PermissionDenied,
3200 .DQUOT => return error.DiskQuota,
3201 .EXIST => return error.PathAlreadyExists,
3202 .FAULT => |err| return errnoBug(err),
3203 .LOOP => return error.SymLinkLoop,
3204 .MLINK => return error.LinkQuotaExceeded,
3205 .NAMETOOLONG => return error.NameTooLong,
3206 .NOENT => return error.FileNotFound,
3207 .NOMEM => return error.SystemResources,
3208 .NOSPC => return error.NoSpaceLeft,
3209 .NOTDIR => return error.NotDir,
3210 .ROFS => return error.ReadOnlyFileSystem,
3211 // dragonfly: when dir_fd is unlinked from filesystem
3212 .NOTCONN => return error.FileNotFound,
3213 .ILSEQ => return error.BadPathName,
3214 else => |err| return posix.unexpectedErrno(err),
3215 }
3216 },
3194 .ACCES => return syscall.fail(error.AccessDenied),
3195 .PERM => return syscall.fail(error.PermissionDenied),
3196 .DQUOT => return syscall.fail(error.DiskQuota),
3197 .EXIST => return syscall.fail(error.PathAlreadyExists),
3198 .LOOP => return syscall.fail(error.SymLinkLoop),
3199 .MLINK => return syscall.fail(error.LinkQuotaExceeded),
3200 .NAMETOOLONG => return syscall.fail(error.NameTooLong),
3201 .NOENT => return syscall.fail(error.FileNotFound),
3202 .NOMEM => return syscall.fail(error.SystemResources),
3203 .NOSPC => return syscall.fail(error.NoSpaceLeft),
3204 .NOTDIR => return syscall.fail(error.NotDir),
3205 .ROFS => return syscall.fail(error.ReadOnlyFileSystem),
3206 // dragonfly: when dir_fd is unlinked from filesystem
3207 .NOTCONN => return syscall.fail(error.FileNotFound),
3208 .ILSEQ => return syscall.fail(error.BadPathName),
3209 .BADF => |err| return syscall.errnoBug(err), // File descriptor used after closed.
3210 .FAULT => |err| return syscall.errnoBug(err),
3211 else => |err| return syscall.unexpectedErrno(err),
32173212 }
32183213 }
32193214}
......@@ -5261,28 +5256,23 @@ fn dirOpenDirPosix(
52615256 try syscall.checkCancel();
52625257 continue;
52635258 },
5264 else => |e| {
5265 syscall.finish();
5266 switch (e) {
5267 .FAULT => |err| return errnoBug(err),
5268 .INVAL => return error.BadPathName,
5269 .BADF => |err| return errnoBug(err), // File descriptor used after closed.
5270 .ACCES => return error.AccessDenied,
5271 .LOOP => return error.SymLinkLoop,
5272 .MFILE => return error.ProcessFdQuotaExceeded,
5273 .NAMETOOLONG => return error.NameTooLong,
5274 .NFILE => return error.SystemFdQuotaExceeded,
5275 .NODEV => return error.NoDevice,
5276 .NOENT => return error.FileNotFound,
5277 .NOMEM => return error.SystemResources,
5278 .NOTDIR => return error.NotDir,
5279 .PERM => return error.PermissionDenied,
5280 .BUSY => |err| return errnoBug(err), // O_EXCL not passed
5281 .NXIO => return error.NoDevice,
5282 .ILSEQ => return error.BadPathName,
5283 else => |err| return posix.unexpectedErrno(err),
5284 }
5285 },
5259 .INVAL => return syscall.fail(error.BadPathName),
5260 .ACCES => return syscall.fail(error.AccessDenied),
5261 .LOOP => return syscall.fail(error.SymLinkLoop),
5262 .MFILE => return syscall.fail(error.ProcessFdQuotaExceeded),
5263 .NAMETOOLONG => return syscall.fail(error.NameTooLong),
5264 .NFILE => return syscall.fail(error.SystemFdQuotaExceeded),
5265 .NODEV => return syscall.fail(error.NoDevice),
5266 .NOENT => return syscall.fail(error.FileNotFound),
5267 .NOMEM => return syscall.fail(error.SystemResources),
5268 .NOTDIR => return syscall.fail(error.NotDir),
5269 .PERM => return syscall.fail(error.PermissionDenied),
5270 .NXIO => return syscall.fail(error.NoDevice),
5271 .ILSEQ => return syscall.fail(error.BadPathName),
5272 .FAULT => |err| return syscall.errnoBug(err),
5273 .BADF => |err| return syscall.errnoBug(err), // File descriptor used after closed.
5274 .BUSY => |err| return syscall.errnoBug(err), // O_EXCL not passed
5275 else => |err| return syscall.unexpectedErrno(err),
52865276 }
52875277 }
52885278}
lib/std/Progress.zig+6
......@@ -325,6 +325,12 @@ pub const Node = struct {
325325 return init(@enumFromInt(free_index), parent, name, estimated_total_items);
326326 }
327327
328 pub fn startFmt(node: Node, estimated_total_items: usize, comptime format: []const u8, args: anytype) Node {
329 var buffer: [max_name_len]u8 = undefined;
330 const name = std.fmt.bufPrint(&buffer, format, args) catch &buffer;
331 return Node.start(node, name, estimated_total_items);
332 }
333
328334 /// This is the same as calling `start` and then `end` on the returned `Node`. Thread-safe.
329335 pub fn completeOne(n: Node) void {
330336 const index = n.index.unwrap() orelse return;
lib/std/compress/flate/Compress.zig+1-1
......@@ -267,7 +267,7 @@ pub const Options = struct {
267267 pub const best = level_9;
268268};
269269
270/// It is asserted `buffer` is least `flate.max_history_len` bytes.
270/// It is asserted `buffer` is least `flate.max_window_len` bytes.
271271/// It is asserted `output` has a capacity of at least 8 bytes.
272272pub fn init(
273273 output: *Writer,
lib/std/zig.zig-1
......@@ -737,7 +737,6 @@ pub const EnvVar = enum {
737737 ZIG_BUILD_MULTILINE_ERRORS,
738738 ZIG_VERBOSE_LINK,
739739 ZIG_VERBOSE_CC,
740 ZIG_BTRFS_WORKAROUND,
741740 ZIG_DEBUG_CMD,
742741 ZIG_IS_DETECTING_LIBC_PATHS,
743742 ZIG_IS_TRYING_TO_NOT_CALL_ITSELF,
src/Package/Fetch.zig+265-242
......@@ -1,28 +1,34 @@
11//! Represents one independent job whose responsibility is to:
22//!
3//! 1. Check the global zig package cache to see if the hash already exists.
3//! 1. Check the local zig package directory to see if the hash already exists.
44//! If so, load, parse, and validate the build.zig.zon file therein, and
5//! goto step 8. Likewise if the location is a relative path, treat this
5//! goto step 9. Likewise if the location is a relative path, treat this
66//! the same as a cache hit. Otherwise, proceed.
7//! 2. Fetch and unpack a URL into a temporary directory.
8//! 3. Load, parse, and validate the build.zig.zon file therein. It is allowed
7//! 2. Check the global package cache for a compressed tarball matching the
8//! hash. If it is found, unpack the contents into a temporary directory inside
9//! project local zig cache. Rename this directory into the local zig package
10//! directory and goto step 9, skipping step 10.
11//! 3. Fetch and unpack a URL into a temporary directory.
12//! 4. Load, parse, and validate the build.zig.zon file therein. It is allowed
913//! for the file to be missing, in which case this fetched package is considered
1014//! to be a "naked" package.
11//! 4. Apply inclusion rules of the build.zig.zon to the temporary directory by
15//! 5. Apply inclusion rules of the build.zig.zon to the temporary directory by
1216//! deleting excluded files. If any files had errors for files that were
1317//! ultimately excluded, those errors should be ignored, such as failure to
1418//! create symlinks that weren't supposed to be included anyway.
15//! 5. Compute the package hash based on the remaining files in the temporary
19//! 6. Compute the package hash based on the remaining files in the temporary
1620//! directory.
17//! 6. Rename the temporary directory into the global zig package cache
18//! directory. If the hash already exists, delete the temporary directory and
19//! leave the zig package cache directory untouched as it may be in use by the
20//! system. This is done even if the hash is invalid, in case the package with
21//! the different hash is used in the future.
22//! 7. Validate the computed hash against the expected hash. If invalid,
21//! 7. Rename the temporary directory into the local zig package directory. If
22//! the hash already exists, delete the temporary directory and leave the zig
23//! package directory untouched as it may be in use. This is done even if
24//! the hash is invalid, in case the package with the different hash is used
25//! in the future.
26//! 8. Validate the computed hash against the expected hash. If invalid,
2327//! this job is done.
24//! 8. Spawn a new fetch job for each dependency in the manifest file. Use
28//! 9. Spawn a new fetch job for each dependency in the manifest file. Use
2529//! a mutex and a hash map so that redundant jobs do not get queued up.
30//! 10.Compress the package directory and store it into the global package
31//! cache.
2632//!
2733//! All of this must be done with only referring to the state inside this struct
2834//! because this work will be done in a dedicated thread.
......@@ -34,6 +40,7 @@ const native_os = builtin.os.tag;
3440const std = @import("std");
3541const Io = std.Io;
3642const fs = std.fs;
43const log = std.log.scoped(.fetch);
3744const assert = std.debug.assert;
3845const ascii = std.ascii;
3946const Allocator = std.mem.Allocator;
......@@ -60,16 +67,13 @@ omit_missing_hash_error: bool,
6067/// which specifies inclusion rules. This is intended to be true for the first
6168/// fetch task and false for the recursive dependencies.
6269allow_missing_paths_field: bool,
63allow_missing_fingerprint: bool,
64allow_name_string: bool,
6570/// If true and URL points to a Git repository, will use the latest commit.
6671use_latest_commit: bool,
6772
6873// Above this are fields provided as inputs to `run`.
6974// Below this are fields populated by `run`.
7075
71/// This will either be relative to `global_cache`, or to the build root of
72/// the root package.
76/// Relative to the build root of the root package.
7377package_root: Cache.Path,
7478error_bundle: ErrorBundle.Wip,
7579manifest: ?Manifest,
......@@ -111,10 +115,15 @@ pub const JobQueue = struct {
111115 /// field contains references to all of them.
112116 /// Protected by `mutex`.
113117 all_fetches: std.ArrayList(*Fetch) = .empty,
118 prog_node: std.Progress.Node,
114119
115120 http_client: *std.http.Client,
121 /// This tracks `Fetch` tasks as well as recompression tasks.
116122 group: Io.Group = .init,
117123 global_cache: Cache.Directory,
124 local_cache: Cache.Path,
125 /// Path to "zig-pkg" inside the package in which the user ran `zig build`.
126 root_pkg_path: Cache.Path,
118127 /// If true then, no fetching occurs, and:
119128 /// * The `global_cache` directory is assumed to be the direct parent
120129 /// directory of on-disk packages rather than having the "p/" directory
......@@ -129,7 +138,6 @@ pub const JobQueue = struct {
129138 /// two hashes of the same package do not match.
130139 /// If this is true, `recursive` must be false.
131140 debug_hash: bool,
132 work_around_btrfs_bug: bool,
133141 mode: Mode,
134142 /// Set of hashes that will be additionally fetched even if they are marked
135143 /// as lazy.
......@@ -294,8 +302,121 @@ pub const JobQueue = struct {
294302 \\
295303 );
296304 }
305
306 fn recompress(jq: *JobQueue, package_hash: Package.Hash) Io.Cancelable!void {
307 const pkg_hash_slice = package_hash.toSlice();
308
309 const prog_node = jq.prog_node.startFmt(0, "recompress {s}", .{pkg_hash_slice});
310 defer prog_node.end();
311
312 var dest_sub_path_buf: ["p/".len + Package.Hash.max_len + ".tar.gz".len]u8 = undefined;
313 const dest_path: Cache.Path = .{
314 .root_dir = jq.global_cache,
315 .sub_path = std.fmt.bufPrint(&dest_sub_path_buf, "p/{s}.tar.gz", .{pkg_hash_slice}) catch unreachable,
316 };
317
318 const gpa = jq.http_client.allocator;
319
320 var arena_instance = std.heap.ArenaAllocator.init(gpa);
321 defer arena_instance.deinit();
322 const arena = arena_instance.allocator();
323
324 recompressFallible(jq, arena, dest_path, pkg_hash_slice, prog_node) catch |err| switch (err) {
325 error.Canceled => |e| return e,
326 error.ReadFailed => comptime unreachable,
327 error.WriteFailed => comptime unreachable,
328 else => |e| log.warn("failed caching recompressed tarball to {f}: {t}", .{ dest_path, e }),
329 };
330 }
331
332 fn recompressFallible(
333 jq: *JobQueue,
334 arena: Allocator,
335 dest_path: Cache.Path,
336 pkg_hash_slice: []const u8,
337 prog_node: std.Progress.Node,
338 ) !void {
339 const gpa = jq.http_client.allocator;
340 const io = jq.io;
341
342 // We have to walk the file system up front in order to sort the file
343 // list for determinism purposes. The hash of the recompressed file is
344 // not critical because the true hash is based on the content alone.
345 // However, if we want Zig users to be able to share cached package
346 // data with each other via peer-to-peer protocols, we benefit greatly
347 // from the data being identical on everyone's computers.
348 var scanned_files: std.ArrayList([]const u8) = .empty;
349 defer scanned_files.deinit(gpa);
350
351 var pkg_dir = try jq.root_pkg_path.openDir(io, pkg_hash_slice, .{ .iterate = true });
352 defer pkg_dir.close(io);
353
354 {
355 var walker = try pkg_dir.walk(gpa);
356 defer walker.deinit();
357
358 while (try walker.next(io)) |entry| {
359 switch (entry.kind) {
360 .directory => continue,
361 .file, .sym_link => {},
362 else => {
363 return error.IllegalFileType;
364 },
365 }
366 const entry_path = try arena.dupe(u8, entry.path);
367 try scanned_files.append(gpa, entry_path);
368 }
369
370 std.mem.sortUnstable([]const u8, scanned_files.items, {}, stringCmp);
371 }
372
373 prog_node.setEstimatedTotalItems(scanned_files.items.len);
374
375 var atomic_file = try dest_path.root_dir.handle.createFileAtomic(io, dest_path.sub_path, .{
376 .make_path = true,
377 .replace = true,
378 });
379 defer atomic_file.deinit(io);
380
381 var file_write_buffer: [4096]u8 = undefined;
382 var file_writer = atomic_file.file.writer(io, &file_write_buffer);
383
384 var compress_buffer: [std.compress.flate.max_window_len]u8 = undefined;
385 var compress = std.compress.flate.Compress.init(&file_writer.interface, &compress_buffer, .gzip, .level_9) catch |err| switch (err) {
386 error.WriteFailed => return file_writer.err.?,
387 };
388
389 var archiver: std.tar.Writer = .{ .underlying_writer = &compress.writer };
390 archiver.prefix = pkg_hash_slice;
391
392 var file_read_buffer: [4096]u8 = undefined;
393
394 for (scanned_files.items) |entry_path| {
395 var file = try pkg_dir.openFile(io, entry_path, .{});
396 defer file.close(io);
397 var file_reader: Io.File.Reader = .init(file, io, &file_read_buffer);
398 archiver.writeFile(entry_path, &file_reader, 0) catch |err| switch (err) {
399 error.ReadFailed => return file_reader.err.?,
400 error.WriteFailed => return file_writer.err.?,
401 else => |e| return e,
402 };
403 prog_node.completeOne();
404 }
405
406 // intentionally omitting the pointless trailer
407 //try archiver.finish();
408 compress.writer.flush() catch |err| switch (err) {
409 error.WriteFailed => return file_writer.err.?,
410 };
411 try file_writer.flush();
412 try atomic_file.replace(io);
413 }
297414};
298415
416fn stringCmp(_: void, lhs: []const u8, rhs: []const u8) bool {
417 return std.mem.lessThan(u8, lhs, rhs);
418}
419
299420pub const Location = union(enum) {
300421 remote: Remote,
301422 /// A directory found inside the parent package.
......@@ -326,11 +447,12 @@ pub const RunError = error{
326447};
327448
328449pub fn run(f: *Fetch) RunError!void {
329 const io = f.job_queue.io;
450 const job_queue = f.job_queue;
451 const io = job_queue.io;
330452 const eb = &f.error_bundle;
331453 const arena = f.arena.allocator();
332454 const gpa = f.arena.child_allocator;
333 const cache_root = f.job_queue.global_cache;
455 const local_cache_root = job_queue.local_cache;
334456
335457 try eb.init(gpa);
336458
......@@ -351,13 +473,13 @@ pub fn run(f: *Fetch) RunError!void {
351473 );
352474 // Packages fetched by URL may not use relative paths to escape outside the
353475 // fetched package directory from within the package cache.
354 if (pkg_root.root_dir.eql(cache_root)) {
476 if (pkg_root.root_dir.eql(local_cache_root.root_dir)) {
355477 // `parent_package_root.sub_path` contains a path like this:
356478 // "p/$hash", or
357479 // "p/$hash/foo", with possibly more directories after "foo".
358480 // We want to fail unless the resolved relative path has a
359481 // prefix of "p/$hash/".
360 const prefix_len: usize = if (f.job_queue.read_only) 0 else "p/".len;
482 const prefix_len: usize = if (job_queue.read_only) 0 else "p/".len;
361483 const parent_sub_path = f.parent_package_root.sub_path;
362484 const end = find_end: {
363485 if (parent_sub_path.len > prefix_len) {
......@@ -380,21 +502,21 @@ pub fn run(f: *Fetch) RunError!void {
380502 f.package_root = pkg_root;
381503 try loadManifest(f, pkg_root);
382504 if (!f.has_build_zig) try checkBuildFileExistence(f);
383 if (!f.job_queue.recursive) return;
505 if (!job_queue.recursive) return;
384506 return queueJobsForDeps(f);
385507 },
386508 .remote => |remote| remote,
387509 .path_or_url => |path_or_url| {
388510 if (Io.Dir.cwd().openDir(io, path_or_url, .{ .iterate = true })) |dir| {
389511 var resource: Resource = .{ .dir = dir };
390 return f.runResource(path_or_url, &resource, null);
512 return f.runResource(path_or_url, &resource, null, false);
391513 } else |dir_err| {
392514 var server_header_buffer: [init_resource_buffer_size]u8 = undefined;
393515
394516 const file_err = if (dir_err == error.NotDir) e: {
395517 if (Io.Dir.cwd().openFile(io, path_or_url, .{})) |file| {
396518 var resource: Resource = .{ .file = file.reader(io, &server_header_buffer) };
397 return f.runResource(path_or_url, &resource, null);
519 return f.runResource(path_or_url, &resource, null, false);
398520 } else |err| break :e err;
399521 } else dir_err;
400522
......@@ -406,57 +528,73 @@ pub fn run(f: *Fetch) RunError!void {
406528 };
407529 var resource: Resource = undefined;
408530 try f.initResource(uri, &resource, &server_header_buffer);
409 return f.runResource(try uri.path.toRawMaybeAlloc(arena), &resource, null);
531 return f.runResource(try uri.path.toRawMaybeAlloc(arena), &resource, null, false);
410532 }
411533 },
412534 };
413535
536 var resource_buffer: [init_resource_buffer_size]u8 = undefined;
537
414538 if (remote.hash) |expected_hash| {
415 var prefixed_pkg_sub_path_buffer: [Package.Hash.max_len + 2]u8 = undefined;
416 prefixed_pkg_sub_path_buffer[0] = 'p';
417 prefixed_pkg_sub_path_buffer[1] = fs.path.sep;
418 const hash_slice = expected_hash.toSlice();
419 @memcpy(prefixed_pkg_sub_path_buffer[2..][0..hash_slice.len], hash_slice);
420 const prefixed_pkg_sub_path = prefixed_pkg_sub_path_buffer[0 .. 2 + hash_slice.len];
421 const prefix_len: usize = if (f.job_queue.read_only) "p/".len else 0;
422 const pkg_sub_path = prefixed_pkg_sub_path[prefix_len..];
423 if (cache_root.handle.access(io, pkg_sub_path, .{})) |_| {
539 const package_root = try job_queue.root_pkg_path.join(arena, expected_hash.toSlice());
540 if (package_root.root_dir.handle.access(io, package_root.sub_path, .{})) |_| {
424541 assert(f.lazy_status != .unavailable);
425 f.package_root = .{
426 .root_dir = cache_root,
427 .sub_path = try arena.dupe(u8, pkg_sub_path),
428 };
542 f.package_root = package_root;
429543 try loadManifest(f, f.package_root);
430544 try checkBuildFileExistence(f);
431 if (!f.job_queue.recursive) return;
545 if (!job_queue.recursive) return;
432546 return queueJobsForDeps(f);
433547 } else |err| switch (err) {
434548 error.FileNotFound => {
435 switch (f.lazy_status) {
436 .eager => {},
437 .available => if (!f.job_queue.unlazy_set.contains(expected_hash)) {
438 f.lazy_status = .unavailable;
439 return;
440 },
441 .unavailable => unreachable,
442 }
443 if (f.job_queue.read_only) return f.fail(
549 log.debug("FileNotFound: {f}", .{package_root});
550 if (job_queue.read_only) return f.fail(
444551 f.name_tok,
445 try eb.printString("package not found at '{f}{s}'", .{
446 cache_root, pkg_sub_path,
447 }),
552 try eb.printString("package not found at '{f}'", .{package_root}),
448553 );
449554 },
555 error.Canceled => |e| return e,
556 else => |e| {
557 try eb.addRootErrorMessage(.{
558 .msg = try eb.printString("unable to open package cache directory {f}: {t}", .{
559 package_root, e,
560 }),
561 });
562 return error.FetchFailed;
563 },
564 }
565
566 // Check global cache before remote fetch.
567 const cached_tarball_sub_path = try std.fmt.allocPrint(arena, "p/{s}.tar.gz", .{expected_hash.toSlice()});
568 const cached_tarball_path: Cache.Path = .{
569 .root_dir = job_queue.global_cache,
570 .sub_path = cached_tarball_sub_path,
571 };
572 if (cached_tarball_path.root_dir.handle.openFile(io, cached_tarball_path.sub_path, .{})) |file| {
573 log.debug("found global cached tarball {f}", .{cached_tarball_path});
574 var resource: Resource = .{ .file = file.reader(io, &resource_buffer) };
575 return f.runResource(cached_tarball_sub_path, &resource, remote.hash, true);
576 } else |err| switch (err) {
577 error.FileNotFound => log.debug("FileNotFound: {f}", .{cached_tarball_path}),
578 error.Canceled => |e| return e,
450579 else => |e| {
451580 try eb.addRootErrorMessage(.{
452 .msg = try eb.printString("unable to open global package cache directory '{f}{s}': {s}", .{
453 cache_root, pkg_sub_path, @errorName(e),
581 .msg = try eb.printString("unable to open globally cached package {f}: {t}", .{
582 cached_tarball_path, e,
454583 }),
455584 });
456585 return error.FetchFailed;
457586 },
458587 }
459 } else if (f.job_queue.read_only) {
588
589 switch (f.lazy_status) {
590 .eager => {},
591 .available => if (!job_queue.unlazy_set.contains(expected_hash)) {
592 f.lazy_status = .unavailable;
593 return;
594 },
595 .unavailable => unreachable,
596 }
597 } else if (job_queue.read_only) {
460598 try eb.addRootErrorMessage(.{
461599 .msg = try eb.addString("dependency is missing hash field"),
462600 .src_loc = try f.srcLoc(f.location_tok),
......@@ -465,15 +603,13 @@ pub fn run(f: *Fetch) RunError!void {
465603 }
466604
467605 // Fetch and unpack the remote into a temporary directory.
468
469606 const uri = std.Uri.parse(remote.url) catch |err| return f.fail(
470607 f.location_tok,
471 try eb.printString("invalid URI: {s}", .{@errorName(err)}),
608 try eb.printString("invalid URI: {t}", .{err}),
472609 );
473 var buffer: [init_resource_buffer_size]u8 = undefined;
474610 var resource: Resource = undefined;
475 try f.initResource(uri, &resource, &buffer);
476 return f.runResource(try uri.path.toRawMaybeAlloc(arena), &resource, remote.hash);
611 try f.initResource(uri, &resource, &resource_buffer);
612 return f.runResource(try uri.path.toRawMaybeAlloc(arena), &resource, remote.hash, false);
477613}
478614
479615pub fn deinit(f: *Fetch) void {
......@@ -487,30 +623,35 @@ fn runResource(
487623 uri_path: []const u8,
488624 resource: *Resource,
489625 remote_hash: ?Package.Hash,
626 disable_recompress: bool,
490627) RunError!void {
491 const io = f.job_queue.io;
628 const job_queue = f.job_queue;
629 assert(!job_queue.read_only);
630
631 const io = job_queue.io;
492632 defer resource.deinit(io);
633
493634 const arena = f.arena.allocator();
494635 const eb = &f.error_bundle;
495636 const s = fs.path.sep_str;
496 const cache_root = f.job_queue.global_cache;
637 const local_cache_root = job_queue.local_cache;
497638 const rand_int = r: {
498639 var x: u64 = undefined;
499640 io.random(@ptrCast(&x));
500641 break :r x;
501642 };
502643 const tmp_dir_sub_path = "tmp" ++ s ++ std.fmt.hex(rand_int);
644 const tmp_directory_path = try local_cache_root.join(arena, tmp_dir_sub_path);
503645
504646 const package_sub_path = blk: {
505 const tmp_directory_path = try cache_root.join(arena, &.{tmp_dir_sub_path});
506647 var tmp_directory: Cache.Directory = .{
507 .path = tmp_directory_path,
648 .path = tmp_directory_path.sub_path,
508649 .handle = handle: {
509 const dir = cache_root.handle.createDirPathOpen(io, tmp_dir_sub_path, .{
650 const dir = tmp_directory_path.root_dir.handle.createDirPathOpen(io, tmp_directory_path.sub_path, .{
510651 .open_options = .{ .iterate = true },
511652 }) catch |err| {
512653 try eb.addRootErrorMessage(.{
513 .msg = try eb.printString("unable to create temporary directory '{s}': {t}", .{
654 .msg = try eb.printString("unable to create temporary directory '{f}': {t}", .{
514655 tmp_directory_path, err,
515656 }),
516657 });
......@@ -524,16 +665,7 @@ fn runResource(
524665 // Fetch and unpack a resource into a temporary directory.
525666 var unpack_result = try unpackResource(f, resource, uri_path, tmp_directory);
526667
527 var pkg_path: Cache.Path = .{ .root_dir = tmp_directory, .sub_path = unpack_result.root_dir };
528
529 // Apply btrfs workaround if needed. Reopen tmp_directory.
530 if (native_os == .linux and f.job_queue.work_around_btrfs_bug) {
531 // https://github.com/ziglang/zig/issues/17095
532 pkg_path.root_dir.handle.close(io);
533 pkg_path.root_dir.handle = cache_root.handle.createDirPathOpen(io, tmp_dir_sub_path, .{
534 .open_options = .{ .iterate = true },
535 }) catch @panic("btrfs workaround failed");
536 }
668 const pkg_path: Cache.Path = .{ .root_dir = tmp_directory, .sub_path = unpack_result.root_dir };
537669
538670 // Load, parse, and validate the unpacked build.zig.zon file. It is allowed
539671 // for the file to be missing, in which case this fetched package is
......@@ -555,36 +687,40 @@ fn runResource(
555687 // directory.
556688 f.computed_hash = try computeHash(f, pkg_path, filter);
557689
558 break :blk if (unpack_result.root_dir.len > 0)
559 try fs.path.join(arena, &.{ tmp_dir_sub_path, unpack_result.root_dir })
560 else
561 tmp_dir_sub_path;
690 if (unpack_result.root_dir.len > 0)
691 break :blk try tmp_directory_path.join(arena, unpack_result.root_dir);
692
693 break :blk tmp_directory_path;
562694 };
563695
564696 const computed_package_hash = computedPackageHash(f);
565697
566 // Rename the temporary directory into the global zig package cache
567 // directory. If the hash already exists, delete the temporary directory
568 // and leave the zig package cache directory untouched as it may be in use
569 // by the system. This is done even if the hash is invalid, in case the
570 // package with the different hash is used in the future.
571
572 f.package_root = .{
573 .root_dir = cache_root,
574 .sub_path = try std.fmt.allocPrint(arena, "p" ++ s ++ "{s}", .{computed_package_hash.toSlice()}),
575 };
576 renameTmpIntoCache(io, cache_root.handle, package_sub_path, f.package_root.sub_path) catch |err| {
577 const src = try cache_root.join(arena, &.{tmp_dir_sub_path});
578 const dest = try cache_root.join(arena, &.{f.package_root.sub_path});
698 // Rename the temporary directory into the local zig package directory. If
699 // the hash already exists, delete the temporary directory and leave the
700 // zig package directory untouched as it may be in use. This is done even
701 // if the hash is invalid, in case the package with the different hash is
702 // used in the future.
703 f.package_root = try job_queue.root_pkg_path.join(arena, computed_package_hash.toSlice());
704 renameTmpIntoCache(io, package_sub_path, f.package_root) catch |err| {
579705 try eb.addRootErrorMessage(.{ .msg = try eb.printString(
580 "unable to rename temporary directory '{s}' into package cache directory '{s}': {s}",
581 .{ src, dest, @errorName(err) },
706 "unable to rename temporary directory {f} into package cache directory {f}: {t}",
707 .{ package_sub_path, f.package_root, err },
582708 ) });
583709 return error.FetchFailed;
584710 };
711
712 if (!disable_recompress) {
713 // Spin off a task to recompress the tarball, with filtered files deleted, into
714 // the global cache.
715 job_queue.group.async(io, JobQueue.recompress, .{ job_queue, computed_package_hash });
716 }
717
585718 // Remove temporary directory root if not already renamed to global cache.
586 if (!std.mem.eql(u8, package_sub_path, tmp_dir_sub_path)) {
587 cache_root.handle.deleteDir(io, tmp_dir_sub_path) catch {};
719 if (!package_sub_path.eql(tmp_directory_path)) {
720 tmp_directory_path.root_dir.handle.deleteDir(io, tmp_directory_path.sub_path) catch |err| switch (err) {
721 error.Canceled => |e| return e,
722 else => |e| log.warn("failed to delete temporary directory {f}: {t}", .{ tmp_directory_path, e }),
723 };
588724 }
589725
590726 // Validate the computed hash against the expected hash. If invalid, this
......@@ -624,7 +760,7 @@ fn runResource(
624760
625761 // Spawn a new fetch job for each dependency in the manifest file. Use
626762 // a mutex and a hash map so that redundant jobs do not get queued up.
627 if (!f.job_queue.recursive) return;
763 if (!job_queue.recursive) return;
628764 return queueJobsForDeps(f);
629765}
630766
......@@ -651,8 +787,8 @@ fn checkBuildFileExistence(f: *Fetch) RunError!void {
651787 error.FileNotFound => {},
652788 else => |e| {
653789 try eb.addRootErrorMessage(.{
654 .msg = try eb.printString("unable to access '{f}{s}': {s}", .{
655 f.package_root, Package.build_zig_basename, @errorName(e),
790 .msg = try eb.printString("unable to access '{f}{s}': {t}", .{
791 f.package_root, Package.build_zig_basename, e,
656792 }),
657793 });
658794 return error.FetchFailed;
......@@ -677,9 +813,7 @@ fn loadManifest(f: *Fetch, pkg_root: Cache.Path) RunError!void {
677813 else => |e| {
678814 const file_path = try pkg_root.join(arena, Manifest.basename);
679815 try eb.addRootErrorMessage(.{
680 .msg = try eb.printString("unable to load package manifest '{f}': {s}", .{
681 file_path, @errorName(e),
682 }),
816 .msg = try eb.printString("unable to load package manifest '{f}': {t}", .{ file_path, e }),
683817 });
684818 return error.FetchFailed;
685819 },
......@@ -698,8 +832,6 @@ fn loadManifest(f: *Fetch, pkg_root: Cache.Path) RunError!void {
698832
699833 f.manifest = try Manifest.parse(arena, ast.*, rng.interface(), .{
700834 .allow_missing_paths_field = f.allow_missing_paths_field,
701 .allow_missing_fingerprint = f.allow_missing_fingerprint,
702 .allow_name_string = f.allow_name_string,
703835 });
704836 const manifest = &f.manifest.?;
705837
......@@ -817,8 +949,6 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
817949 .job_queue = f.job_queue,
818950 .omit_missing_hash_error = false,
819951 .allow_missing_paths_field = true,
820 .allow_missing_fingerprint = true,
821 .allow_name_string = true,
822952 .use_latest_commit = false,
823953
824954 .package_root = undefined,
......@@ -1463,14 +1593,20 @@ fn recursiveDirectoryCopy(f: *Fetch, dir: Io.Dir, tmp_dir: Io.Dir) anyerror!void
14631593 }
14641594}
14651595
1466pub fn renameTmpIntoCache(io: Io, cache_dir: Io.Dir, tmp_dir_sub_path: []const u8, dest_dir_sub_path: []const u8) !void {
1467 assert(dest_dir_sub_path[1] == fs.path.sep);
1596pub fn renameTmpIntoCache(io: Io, tmp_path: Cache.Path, dest_path: Cache.Path) !void {
14681597 var handled_missing_dir = false;
14691598 while (true) {
1470 cache_dir.rename(tmp_dir_sub_path, cache_dir, dest_dir_sub_path, io) catch |err| switch (err) {
1599 Io.Dir.rename(
1600 tmp_path.root_dir.handle,
1601 tmp_path.sub_path,
1602 dest_path.root_dir.handle,
1603 dest_path.sub_path,
1604 io,
1605 ) catch |err| switch (err) {
14711606 error.FileNotFound => {
14721607 if (handled_missing_dir) return err;
1473 cache_dir.createDir(io, dest_dir_sub_path[0..1], .default_dir) catch |mkd_err| switch (mkd_err) {
1608 const parent_sub_path = Io.Dir.path.dirname(dest_path.sub_path).?;
1609 dest_path.root_dir.handle.createDir(io, parent_sub_path, .default_dir) catch |er| switch (er) {
14741610 error.PathAlreadyExists => handled_missing_dir = true,
14751611 else => |e| return e,
14761612 };
......@@ -1478,9 +1614,11 @@ pub fn renameTmpIntoCache(io: Io, cache_dir: Io.Dir, tmp_dir_sub_path: []const u
14781614 },
14791615 error.DirNotEmpty, error.AccessDenied => {
14801616 // Package has been already downloaded and may already be in use on the system.
1481 cache_dir.deleteTree(io, tmp_dir_sub_path) catch {
1617 tmp_path.root_dir.handle.deleteTree(io, tmp_path.sub_path) catch |er| switch (er) {
1618 error.Canceled => |e| return e,
14821619 // Garbage files leftover in zig-cache/tmp/ is, as they say
14831620 // on Star Trek, "operating within normal parameters".
1621 else => |e| log.warn("failed to delete temporary directory {f}: {t}", .{ tmp_path, e }),
14841622 };
14851623 },
14861624 else => |e| return e,
......@@ -2064,130 +2202,6 @@ const UnpackResult = struct {
20642202 }
20652203};
20662204
2067test "tarball with duplicate paths" {
2068 // This tarball has duplicate path 'dir1/file1' to simulate case sensitve
2069 // file system on any file sytstem.
2070 //
2071 // duplicate_paths/
2072 // duplicate_paths/dir1/
2073 // duplicate_paths/dir1/file1
2074 // duplicate_paths/dir1/file1
2075 // duplicate_paths/build.zig.zon
2076 // duplicate_paths/src/
2077 // duplicate_paths/src/main.zig
2078 // duplicate_paths/src/root.zig
2079 // duplicate_paths/build.zig
2080 //
2081
2082 const gpa = std.testing.allocator;
2083 const io = std.testing.io;
2084 var tmp = std.testing.tmpDir(.{});
2085 defer tmp.cleanup();
2086
2087 const tarball_name = "duplicate_paths.tar.gz";
2088 try saveEmbedFile(io, tarball_name, tmp.dir);
2089 const tarball_path = try std.fmt.allocPrint(gpa, ".zig-cache/tmp/{s}/{s}", .{ tmp.sub_path, tarball_name });
2090 defer gpa.free(tarball_path);
2091
2092 // Run tarball fetch, expect to fail
2093 var fb: TestFetchBuilder = undefined;
2094 var fetch = try fb.build(gpa, io, tmp.dir, tarball_path);
2095 defer fb.deinit();
2096 try std.testing.expectError(error.FetchFailed, fetch.run());
2097
2098 try fb.expectFetchErrors(1,
2099 \\error: unable to unpack tarball
2100 \\ note: unable to create file 'dir1/file1': PathAlreadyExists
2101 \\
2102 );
2103}
2104
2105test "tarball with excluded duplicate paths" {
2106 // Same as previous tarball but has build.zig.zon wich excludes 'dir1'.
2107 //
2108 // .paths = .{
2109 // "build.zig",
2110 // "build.zig.zon",
2111 // "src",
2112 // }
2113 //
2114
2115 const gpa = std.testing.allocator;
2116 const io = std.testing.io;
2117 var tmp = std.testing.tmpDir(.{});
2118 defer tmp.cleanup();
2119
2120 const tarball_name = "duplicate_paths_excluded.tar.gz";
2121 try saveEmbedFile(io, tarball_name, tmp.dir);
2122 const tarball_path = try std.fmt.allocPrint(gpa, ".zig-cache/tmp/{s}/{s}", .{ tmp.sub_path, tarball_name });
2123 defer gpa.free(tarball_path);
2124
2125 // Run tarball fetch, should succeed
2126 var fb: TestFetchBuilder = undefined;
2127 var fetch = try fb.build(gpa, io, tmp.dir, tarball_path);
2128 defer fb.deinit();
2129 try fetch.run();
2130
2131 const hex_digest = Package.multiHashHexDigest(fetch.computed_hash.digest);
2132 try std.testing.expectEqualStrings(
2133 "12200bafe035cbb453dd717741b66e9f9d1e6c674069d06121dafa1b2e62eb6b22da",
2134 &hex_digest,
2135 );
2136
2137 const expected_files: []const []const u8 = &.{
2138 "build.zig",
2139 "build.zig.zon",
2140 "src/main.zig",
2141 "src/root.zig",
2142 };
2143 try fb.expectPackageFiles(expected_files);
2144}
2145
2146test "tarball without root folder" {
2147 // Tarball with root folder. Manifest excludes dir1 and dir2.
2148 //
2149 // build.zig
2150 // build.zig.zon
2151 // dir1/
2152 // dir1/file2
2153 // dir1/file1
2154 // dir2/
2155 // dir2/file2
2156 // src/
2157 // src/main.zig
2158 //
2159
2160 const gpa = std.testing.allocator;
2161 const io = std.testing.io;
2162
2163 var tmp = std.testing.tmpDir(.{});
2164 defer tmp.cleanup();
2165
2166 const tarball_name = "no_root.tar.gz";
2167 try saveEmbedFile(io, tarball_name, tmp.dir);
2168 const tarball_path = try std.fmt.allocPrint(gpa, ".zig-cache/tmp/{s}/{s}", .{ tmp.sub_path, tarball_name });
2169 defer gpa.free(tarball_path);
2170
2171 // Run tarball fetch, should succeed
2172 var fb: TestFetchBuilder = undefined;
2173 var fetch = try fb.build(gpa, io, tmp.dir, tarball_path);
2174 defer fb.deinit();
2175 try fetch.run();
2176
2177 const hex_digest = Package.multiHashHexDigest(fetch.computed_hash.digest);
2178 try std.testing.expectEqualStrings(
2179 "12209f939bfdcb8b501a61bb4a43124dfa1b2848adc60eec1e4624c560357562b793",
2180 &hex_digest,
2181 );
2182
2183 const expected_files: []const []const u8 = &.{
2184 "build.zig",
2185 "build.zig.zon",
2186 "src/main.zig",
2187 };
2188 try fb.expectPackageFiles(expected_files);
2189}
2190
21912205test "set executable bit based on file content" {
21922206 if (!Io.File.Permissions.has_executable_bit) return error.SkipZigTest;
21932207 const gpa = std.testing.allocator;
......@@ -2254,6 +2268,7 @@ fn saveEmbedFile(io: Io, comptime tarball_name: []const u8, dir: Io.Dir) !void {
22542268const TestFetchBuilder = struct {
22552269 http_client: std.http.Client,
22562270 global_cache_directory: Cache.Directory,
2271 local_cache_path: Cache.Path,
22572272 job_queue: Fetch.JobQueue,
22582273 fetch: Fetch,
22592274
......@@ -2264,20 +2279,30 @@ const TestFetchBuilder = struct {
22642279 cache_parent_dir: std.Io.Dir,
22652280 path_or_url: []const u8,
22662281 ) !*Fetch {
2267 const cache_dir = try cache_parent_dir.createDirPathOpen(io, "zig-global-cache", .{});
2282 const global_cache_dir = try cache_parent_dir.createDirPathOpen(io, "zig-global-cache", .{});
2283 const package_root_dir = try cache_parent_dir.createDirPathOpen(io, "local-project-root", .{});
22682284
22692285 self.http_client = .{ .allocator = allocator, .io = io };
2270 self.global_cache_directory = .{ .handle = cache_dir, .path = null };
2286 self.global_cache_directory = .{ .handle = global_cache_dir, .path = "zig-global-cache" };
2287 self.local_cache_path = .{
2288 .root_dir = .{ .handle = package_root_dir, .path = "local-project-root" },
2289 .sub_path = ".zig-cache",
2290 };
22712291
22722292 self.job_queue = .{
22732293 .io = io,
22742294 .http_client = &self.http_client,
22752295 .global_cache = self.global_cache_directory,
2296 .local_cache = self.local_cache_path,
2297 .root_pkg_path = .{
2298 .root_dir = .{ .handle = package_root_dir, .path = "local-project-root" },
2299 .sub_path = "zig-pkg",
2300 },
22762301 .recursive = false,
22772302 .read_only = false,
22782303 .debug_hash = false,
2279 .work_around_btrfs_bug = false,
22802304 .mode = .needed,
2305 .prog_node = std.Progress.Node.none,
22812306 };
22822307
22832308 self.fetch = .{
......@@ -2287,14 +2312,12 @@ const TestFetchBuilder = struct {
22872312 .hash_tok = .none,
22882313 .name_tok = 0,
22892314 .lazy_status = .eager,
2290 .parent_package_root = Cache.Path{ .root_dir = Cache.Directory{ .handle = cache_dir, .path = null } },
2315 .parent_package_root = .{ .root_dir = .{ .handle = package_root_dir, .path = null } },
22912316 .parent_manifest_ast = null,
22922317 .prog_node = std.Progress.Node.none,
22932318 .job_queue = &self.job_queue,
22942319 .omit_missing_hash_error = true,
22952320 .allow_missing_paths_field = false,
2296 .allow_missing_fingerprint = true, // so we can keep using the old testdata .tar.gz
2297 .allow_name_string = true, // so we can keep using the old testdata .tar.gz
22982321 .use_latest_commit = true,
22992322
23002323 .package_root = undefined,
src/Package/Fetch/testdata/duplicate_paths.tar.gz deleted
Binary files a/src/Package/Fetch/testdata/duplicate_paths.tar.gz and /dev/null differ
src/Package/Fetch/testdata/duplicate_paths_excluded.tar.gz deleted
Binary files a/src/Package/Fetch/testdata/duplicate_paths_excluded.tar.gz and /dev/null differ
src/Package/Fetch/testdata/no_root.tar.gz deleted
Binary files a/src/Package/Fetch/testdata/no_root.tar.gz and /dev/null differ
src/Package/Manifest.zig+7-27
......@@ -49,10 +49,6 @@ arena_state: std.heap.ArenaAllocator.State,
4949
5050pub const ParseOptions = struct {
5151 allow_missing_paths_field: bool = false,
52 /// Deprecated, to be removed after 0.14.0 is tagged.
53 allow_name_string: bool = true,
54 /// Deprecated, to be removed after 0.14.0 is tagged.
55 allow_missing_fingerprint: bool = true,
5652};
5753
5854pub const Error = Allocator.Error;
......@@ -77,8 +73,6 @@ pub fn parse(gpa: Allocator, ast: Ast, rng: std.Random, options: ParseOptions) E
7773 .dependencies_node = .none,
7874 .paths = .{},
7975 .allow_missing_paths_field = options.allow_missing_paths_field,
80 .allow_name_string = options.allow_name_string,
81 .allow_missing_fingerprint = options.allow_missing_fingerprint,
8276 .minimum_zig_version = null,
8377 .buf = .{},
8478 };
......@@ -151,8 +145,6 @@ const Parse = struct {
151145 dependencies_node: Ast.Node.OptionalIndex,
152146 paths: std.StringArrayHashMapUnmanaged(void),
153147 allow_missing_paths_field: bool,
154 allow_name_string: bool,
155 allow_missing_fingerprint: bool,
156148 minimum_zig_version: ?std.SemanticVersion,
157149
158150 const InnerError = error{ ParseFailure, OutOfMemory };
......@@ -221,12 +213,10 @@ const Parse = struct {
221213 });
222214 }
223215 p.id = n.id;
224 } else if (!p.allow_missing_fingerprint) {
216 } else {
225217 try appendError(p, main_token, "missing top-level 'fingerprint' field; suggested value: 0x{x}", .{
226218 Package.Fingerprint.generate(rng, p.name).int(),
227219 });
228 } else {
229 p.id = 0;
230220 }
231221 }
232222
......@@ -395,19 +385,6 @@ const Parse = struct {
395385 const ast = p.ast;
396386 const main_token = ast.nodeMainToken(node);
397387
398 if (p.allow_name_string and ast.nodeTag(node) == .string_literal) {
399 const name = try parseString(p, node);
400 if (!std.zig.isValidId(name))
401 return fail(p, main_token, "name must be a valid bare zig identifier (hint: switch from string to enum literal)", .{});
402
403 if (name.len > max_name_len)
404 return fail(p, main_token, "name '{f}' exceeds max length of {d}", .{
405 std.zig.fmtId(name), max_name_len,
406 });
407
408 return name;
409 }
410
411388 if (ast.nodeTag(node) != .enum_literal)
412389 return fail(p, main_token, "expected enum literal", .{});
413390
......@@ -606,7 +583,8 @@ test "basic" {
606583
607584 const example =
608585 \\.{
609 \\ .name = "foo",
586 \\ .name = .foo,
587 \\ .fingerprint = 0x8c736521490b23df,
610588 \\ .version = "3.2.1",
611589 \\ .paths = .{""},
612590 \\ .dependencies = .{
......@@ -656,7 +634,8 @@ test "minimum_zig_version" {
656634
657635 const example =
658636 \\.{
659 \\ .name = "foo",
637 \\ .name = .foo,
638 \\ .fingerprint = 0x8c736521490b23df,
660639 \\ .version = "3.2.1",
661640 \\ .paths = .{""},
662641 \\ .minimum_zig_version = "0.11.1",
......@@ -690,7 +669,8 @@ test "minimum_zig_version - invalid version" {
690669
691670 const example =
692671 \\.{
693 \\ .name = "foo",
672 \\ .name = .foo,
673 \\ .fingerprint = 0x8c736521490b23df,
694674 \\ .version = "3.2.1",
695675 \\ .minimum_zig_version = "X.11.1",
696676 \\ .paths = .{""},
src/main.zig+40-26
......@@ -5098,8 +5098,6 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
50985098 }
50995099 }
51005100
5101 const work_around_btrfs_bug = native_os == .linux and
5102 EnvVar.ZIG_BTRFS_WORKAROUND.isSet(environ_map);
51035101 const root_prog_node = std.Progress.start(io, .{
51045102 .disable_printing = (color == .off),
51055103 .root_name = "Compile Build Script",
......@@ -5241,24 +5239,29 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
52415239 .io = io,
52425240 .http_client = &http_client,
52435241 .global_cache = dirs.global_cache,
5242 .local_cache = .{ .root_dir = dirs.local_cache, .sub_path = "" },
5243 .root_pkg_path = .{ .root_dir = build_root.directory, .sub_path = "zig-pkg" },
52445244 .read_only = false,
52455245 .recursive = true,
52465246 .debug_hash = false,
5247 .work_around_btrfs_bug = work_around_btrfs_bug,
52485247 .unlazy_set = unlazy_set,
52495248 .mode = fetch_mode,
5249 .prog_node = fetch_prog_node,
52505250 };
52515251 defer job_queue.deinit();
52525252
52535253 if (system_pkg_dir_path) |p| {
5254 job_queue.global_cache = .{
5255 .path = p,
5256 .handle = Io.Dir.cwd().openDir(io, p, .{}) catch |err| {
5257 fatal("unable to open system package directory '{s}': {s}", .{
5258 p, @errorName(err),
5259 });
5254 const system_pkg_path: Path = .{
5255 .root_dir = .{
5256 .path = p,
5257 .handle = Io.Dir.cwd().openDir(io, p, .{}) catch |err| {
5258 fatal("unable to open system package directory '{s}': {t}", .{ p, err });
5259 },
52605260 },
5261 .sub_path = "",
52615262 };
5263 job_queue.global_cache = system_pkg_path.root_dir;
5264 job_queue.root_pkg_path = system_pkg_path;
52625265 job_queue.read_only = true;
52635266 cleanup_build_dir = job_queue.global_cache.handle;
52645267 } else {
......@@ -5283,8 +5286,6 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8,
52835286 .job_queue = &job_queue,
52845287 .omit_missing_hash_error = true,
52855288 .allow_missing_paths_field = false,
5286 .allow_missing_fingerprint = false,
5287 .allow_name_string = false,
52885289 .use_latest_commit = false,
52895290
52905291 .package_root = undefined,
......@@ -6938,8 +6939,6 @@ fn cmdFetch(
69386939 dev.check(.fetch_command);
69396940
69406941 const color: Color = .auto;
6941 const work_around_btrfs_bug = native_os == .linux and
6942 EnvVar.ZIG_BTRFS_WORKAROUND.isSet(environ_map);
69436942 var opt_path_or_url: ?[]const u8 = null;
69446943 var override_global_cache_dir: ?[]const u8 = EnvVar.ZIG_GLOBAL_CACHE_DIR.get(environ_map);
69456944 var debug_hash: bool = false;
......@@ -7003,15 +7002,32 @@ fn cmdFetch(
70037002 };
70047003 defer global_cache_directory.handle.close(io);
70057004
7005 const cwd_path = try introspect.getResolvedCwd(io, arena);
7006
7007 var build_root = try findBuildRoot(arena, io, .{
7008 .cwd_path = cwd_path,
7009 });
7010 defer build_root.deinit(io);
7011
7012 const local_cache_path: Path = .{
7013 .root_dir = build_root.directory,
7014 .sub_path = ".zig-cache",
7015 };
7016
70067017 var job_queue: Package.Fetch.JobQueue = .{
70077018 .io = io,
70087019 .http_client = &http_client,
70097020 .global_cache = global_cache_directory,
7021 .local_cache = local_cache_path,
7022 .root_pkg_path = .{
7023 .root_dir = build_root.directory,
7024 .sub_path = "zig-pkg",
7025 },
70107026 .recursive = false,
70117027 .read_only = false,
70127028 .debug_hash = debug_hash,
7013 .work_around_btrfs_bug = work_around_btrfs_bug,
70147029 .mode = .all,
7030 .prog_node = root_prog_node,
70157031 };
70167032 defer job_queue.deinit();
70177033
......@@ -7028,8 +7044,6 @@ fn cmdFetch(
70287044 .job_queue = &job_queue,
70297045 .omit_missing_hash_error = true,
70307046 .allow_missing_paths_field = false,
7031 .allow_missing_fingerprint = true,
7032 .allow_name_string = true,
70337047 .use_latest_commit = true,
70347048
70357049 .package_root = undefined,
......@@ -7077,13 +7091,6 @@ fn cmdFetch(
70777091 },
70787092 };
70797093
7080 const cwd_path = try introspect.getResolvedCwd(io, arena);
7081
7082 var build_root = try findBuildRoot(arena, io, .{
7083 .cwd_path = cwd_path,
7084 });
7085 defer build_root.deinit(io);
7086
70877094 // The name to use in case the manifest file needs to be created now.
70887095 const init_root_name = fs.path.basename(build_root.directory.path orelse cwd_path);
70897096 var manifest, var ast = try loadManifest(gpa, arena, io, .{
......@@ -7247,18 +7254,25 @@ fn createDependenciesModule(
72477254 defer tmp_dir.close(io);
72487255 try tmp_dir.writeFile(io, .{ .sub_path = basename, .data = source });
72497256 }
7257 const tmp_dir_path: Path = .{
7258 .root_dir = dirs.local_cache,
7259 .sub_path = tmp_dir_sub_path,
7260 };
72507261
72517262 var hh: Cache.HashHelper = .{};
72527263 hh.addBytes(build_options.version);
72537264 hh.addBytes(source);
72547265 const hex_digest = hh.final();
72557266
7256 const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest);
7257 try Package.Fetch.renameTmpIntoCache(io, dirs.local_cache.handle, tmp_dir_sub_path, o_dir_sub_path);
7267 const o_dir_path: Path = .{
7268 .root_dir = dirs.local_cache,
7269 .sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest),
7270 };
7271 try Package.Fetch.renameTmpIntoCache(io, tmp_dir_path, o_dir_path);
72587272
72597273 const deps_mod = try Package.Module.create(arena, .{
72607274 .paths = .{
7261 .root = try .fromRoot(arena, dirs, .local_cache, o_dir_sub_path),
7275 .root = try .fromRoot(arena, dirs, .local_cache, o_dir_path.sub_path),
72627276 .root_src_path = basename,
72637277 },
72647278 .fully_qualified_name = "root.@dependencies",