authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-20 20:05:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 18:54:36-07:00
logdf92898ec3f805112150ee4b40e0d02b763524bb
tree622e6e9dcc7d72c344973ec5ecd28b305246df3b
parent619f23b3c7a5276ab974478223ec41ce81c91b6c

Maker: fix packagePath function

Currently, neither configurer nor Maker is aware of the standard zig package path, and the root path is stored as a bare string rather than relative to a known base directory. Without changing that, we must construct a cwd relative path here rather than using knowledge of the standard package path plus package hash. Also fixes a bug that would have been prevented by implementing the accepted proposal https://github.com/ziglang/zig/issues/25315

8 files changed, 56 insertions(+), 44 deletions(-)

lib/compiler/Maker.zig+6-8
......@@ -123,10 +123,6 @@ pub fn main(init: process.Init.Minimal) !void {
123123 .local_cache_root = local_cache_directory,
124124 .zig_lib_directory = zig_lib_directory,
125125 .build_root_directory = build_root_directory,
126 .pkg_root = .{
127 .root_dir = build_root_directory,
128 .sub_path = "zig-pkg",
129 },
130126 };
131127
132128 graph.cache.addPrefix(.{ .path = null, .handle = cwd });
......@@ -1779,11 +1775,13 @@ pub fn packagePath(
17791775 .root_dir = graph.build_root_directory,
17801776 .sub_path = sub_path,
17811777 };
1782 const hash = package.hash.slice(c);
1783 const pkg_root = graph.pkg_root;
1778 // Currently, neither configurer nor Maker is aware of the standard zig
1779 // package path, and the root path is stored as a bare string rather than
1780 // relative to a known base directory. Without changing that, we must
1781 // construct a cwd relative path here.
17841782 return .{
1785 .root_dir = pkg_root.root_dir,
1786 .sub_path = try Dir.path.join(arena, &.{ pkg_root.sub_path, hash, sub_path }),
1783 .root_dir = .cwd(),
1784 .sub_path = try Dir.path.join(arena, &.{ package.root_path.slice(c), sub_path }),
17871785 };
17881786}
17891787
lib/compiler/Maker/Graph.zig-1
......@@ -18,7 +18,6 @@ global_cache_root: Directory,
1818local_cache_root: Directory,
1919zig_lib_directory: Directory,
2020build_root_directory: Directory,
21pkg_root: Path,
2221
2322debug_compiler_runtime_libs: ?std.builtin.OptimizeMode = null,
2423incremental: ?bool = null,
lib/compiler/configurer.zig+12-10
......@@ -169,6 +169,7 @@ const Serialize = struct {
169169 gop.value_ptr.* = @enumFromInt(try wc.addExtra(@as(Configuration.Package, .{
170170 .hash = try wc.addString(b.pkg_hash),
171171 .dep_prefix = try wc.addString(b.dep_prefix),
172 .root_path = try wc.addString(try b.root.toString(arena)),
172173 })));
173174 }
174175 return gop.value_ptr.*;
......@@ -233,7 +234,7 @@ const Serialize = struct {
233234
234235 fn addSystemLib(s: *Serialize, sl: *const std.Build.Module.SystemLib) !Configuration.SystemLib.Index {
235236 const wc = s.wc;
236 return @enumFromInt(try wc.addDeduped(@as(Configuration.SystemLib, .{
237 return try wc.addDeduped(Configuration.SystemLib, .{
237238 .flags = .{
238239 .needed = sl.needed,
239240 .weak = sl.weak,
......@@ -242,7 +243,7 @@ const Serialize = struct {
242243 .search_strategy = sl.search_strategy,
243244 },
244245 .name = try wc.addString(sl.name),
245 })));
246 });
246247 }
247248
248249 fn addCSourceFile(s: *Serialize, csf: *const std.Build.Module.CSourceFile) !Configuration.CSourceFile.Index {
......@@ -291,10 +292,10 @@ const Serialize = struct {
291292 fn addEnvironMap(s: *Serialize, opt_map: ?*std.process.Environ.Map) !?Configuration.EnvironMap.Index {
292293 const wc = s.wc;
293294 const map = opt_map orelse return null;
294 return @enumFromInt(try wc.addDeduped(@as(Configuration.EnvironMap, .{
295 return try wc.addDeduped(Configuration.EnvironMap, .{
295296 .keys = try wc.addStringList(map.array_hash_map.keys()),
296297 .values = try wc.addStringList(map.array_hash_map.values()),
297 })));
298 });
298299 }
299300
300301 fn initArgsList(s: *Serialize, args: []const Step.Run.Arg) ![]const Configuration.Step.Run.Arg.Index {
......@@ -643,9 +644,10 @@ const Serialize = struct {
643644 comptime assert(std.mem.eql(u8, @typeInfo(Configuration.Module).@"struct".fields[2].name, "import_table"));
644645 comptime assert(@typeInfo(Configuration.Module).@"struct".fields[2].type == Configuration.ImportTable.Index);
645646 assert(wc.extra.items[@intFromEnum(module_index) + 2] == @intFromEnum(Configuration.ImportTable.Index.invalid));
646 wc.extra.items[@intFromEnum(module_index) + 2] = try wc.addDeduped(@as(Configuration.ImportTable, .{
647 const import_table_index = try wc.addDeduped(Configuration.ImportTable, .{
647648 .imports = .{ .mal = imports },
648 }));
649 });
650 wc.extra.items[@intFromEnum(module_index) + 2] = @intFromEnum(import_table_index);
649651
650652 return module_index;
651653 }
......@@ -687,9 +689,9 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
687689 for (dep_steps, step.dependencies.items) |*dest, src|
688690 dest.* = @enumFromInt(s.step_map.getIndex(src).?);
689691
690 const deps: Configuration.Deps.Index = @enumFromInt(try wc.addDeduped(@as(Configuration.Deps, .{
692 const deps: Configuration.Deps.Index = try wc.addDeduped(Configuration.Deps, .{
691693 .steps = .{ .slice = dep_steps },
692 })));
694 });
693695
694696 try wc.steps.ensureTotalCapacity(gpa, s.step_map.entries.capacity);
695697 wc.steps.appendAssumeCapacity(.{
......@@ -1278,10 +1280,10 @@ fn addOptionalResolvedTarget(
12781280 optional_resolved_target: ?std.Build.ResolvedTarget,
12791281) !Configuration.ResolvedTarget.OptionalIndex {
12801282 const resolved_target = optional_resolved_target orelse return .none;
1281 return @enumFromInt(try wc.addDeduped(@as(Configuration.ResolvedTarget, .{
1283 return .init(try wc.addDeduped(Configuration.ResolvedTarget, .{
12821284 .query = try wc.addTargetQuery(&resolved_target.query),
12831285 .result = try wc.addTarget(resolved_target.result),
1284 })));
1286 }));
12851287}
12861288
12871289fn addInstallDir(wc: *Configuration.Wip, install_dir: ?std.Build.InstallDir) !Configuration.InstallDestDir {
lib/std/Build.zig+1
......@@ -1896,6 +1896,7 @@ fn markNeededLazyDep(b: *Build, pkg_hash: []const u8) void {
18961896/// In other words, if this function returns `null` it means that the only
18971897/// purpose of completing the configure phase is to find out all the other lazy
18981898/// dependencies that are also required.
1899///
18991900/// It is allowed to use this function for non-lazy dependencies, in which case
19001901/// it will never return `null`. This allows toggling laziness via
19011902/// build.zig.zon without changing build.zig logic.
lib/std/Build/Cache/Path.zig+1-1
......@@ -24,7 +24,7 @@ pub fn cwd() Path {
2424}
2525
2626pub fn initCwd(sub_path: []const u8) Path {
27 return .{ .root_dir = Cache.Directory.cwd(), .sub_path = sub_path };
27 return .{ .root_dir = .cwd(), .sub_path = sub_path };
2828}
2929
3030pub fn join(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path {
lib/std/Build/Configuration.zig+17-7
......@@ -374,25 +374,28 @@ pub const Wip = struct {
374374
375375 /// Same as `addExtra` but uses a hash map to possibly return an already
376376 /// existing index instead of appending to `extra`.
377 pub fn addDeduped(wip: *Wip, extra: anytype) Allocator.Error!u32 {
377 pub fn addDeduped(wip: *Wip, comptime T: type, v: T) Allocator.Error!T.Index {
378378 const gpa = wip.gpa;
379379 const revert_index = wip.extra.items.len;
380 const extra_len = Storage.extraLen(extra);
381 try wip.extra.ensureUnusedCapacity(gpa, extra_len);
382 const new_index = addExtraAssumeCapacity(wip, extra);
380 const upper_bound_len = Storage.extraLen(v);
381 try wip.extra.ensureUnusedCapacity(gpa, upper_bound_len);
382 try wip.dedupe_table.ensureUnusedCapacityContext(gpa, 1, @as(ExtraSlice.Context, .{
383 .extra = wip.extra.items,
384 }));
385 const new_index = addExtraAssumeCapacity(wip, v);
383386 const len: u32 = @intCast(wip.extra.items.len - new_index);
384387 assert(len != 0);
385 const gop = try wip.dedupe_table.getOrPutContext(gpa, .{
388 const gop = wip.dedupe_table.getOrPutAssumeCapacityContext(.{
386389 .index = new_index,
387390 .len = len,
388391 }, @as(ExtraSlice.Context, .{ .extra = wip.extra.items }));
389392
390393 if (gop.found_existing) {
391394 wip.extra.items.len = revert_index;
392 return gop.key_ptr.index;
395 return @enumFromInt(gop.key_ptr.index);
393396 }
394397
395 return new_index;
398 return @enumFromInt(new_index);
396399 }
397400
398401 pub fn addExtraAssumeCapacity(wip: *Wip, extra: anytype) u32 {
......@@ -1518,6 +1521,7 @@ pub const OptionalGeneratedFileIndex = enum(u32) {
15181521pub const Package = struct {
15191522 dep_prefix: String,
15201523 hash: String,
1524 root_path: String,
15211525
15221526 pub const Index = enum(u32) {
15231527 root = max_u32,
......@@ -2075,6 +2079,12 @@ pub const ResolvedTarget = struct {
20752079 none = max_u32,
20762080 _,
20772081
2082 pub fn init(i: Index) OptionalIndex {
2083 const result: OptionalIndex = @enumFromInt(@intFromEnum(i));
2084 assert(result != .none);
2085 return result;
2086 }
2087
20782088 pub fn unwrap(this: @This()) ?Index {
20792089 return switch (this) {
20802090 .none => null,
src/Package/Fetch.zig+2-2
......@@ -782,7 +782,7 @@ fn runResource(
782782 f.package_root = try ls.pkg_root.join(arena, computed_package_hash.toSlice());
783783 renameTmpIntoCache(io, package_sub_path, f.package_root) catch |err| {
784784 try eb.addRootErrorMessage(.{ .msg = try eb.printString(
785 "unable to rename temporary directory {f} into package cache directory {f}: {t}",
785 "failed renaming temporary directory {f} into package cache directory {f}: {t}",
786786 .{ package_sub_path, f.package_root, err },
787787 ) });
788788 return error.FetchFailed;
......@@ -802,7 +802,7 @@ fn runResource(
802802 if (!package_sub_path.eql(tmp_directory_path)) {
803803 tmp_directory_path.root_dir.handle.deleteDir(io, tmp_directory_path.sub_path) catch |err| switch (err) {
804804 error.Canceled => |e| return e,
805 else => |e| log.warn("failed to delete temporary directory {f}: {t}", .{ tmp_directory_path, e }),
805 else => |e| log.warn("failed deleting temporary directory {f}: {t}", .{ tmp_directory_path, e }),
806806 };
807807 }
808808
src/main.zig+17-15
......@@ -4985,16 +4985,16 @@ fn cmdBuild(
49854985 configure_argv.addManyAsArrayAssumeCapacity(2).* = .{ "--zig", self_exe_path };
49864986
49874987 make_argv.addManyAsArrayAssumeCapacity(2).* = .{ "--zig-lib-dir", undefined };
4988 const argv_index_zig_lib_dir = make_argv.items.len - 1;
4988 const make_argv_index_zig_lib_dir = make_argv.items.len - 1;
49894989
49904990 make_argv.addManyAsArrayAssumeCapacity(2).* = .{ "--build-root", undefined };
49914991 const make_argv_index_build_root = make_argv.items.len - 1;
49924992
49934993 make_argv.addManyAsArrayAssumeCapacity(2).* = .{ "--local-cache", undefined };
4994 const argv_index_cache_dir = make_argv.items.len - 1;
4994 const make_argv_index_cache_dir = make_argv.items.len - 1;
49954995
49964996 make_argv.addManyAsArrayAssumeCapacity(2).* = .{ "--global-cache", undefined };
4997 const argv_index_global_cache_dir = make_argv.items.len - 1;
4997 const make_argv_index_global_cache_dir = make_argv.items.len - 1;
49984998
49994999 make_argv.addManyAsArrayAssumeCapacity(2).* = .{ "--configuration", undefined };
50005000 const argv_index_configuration_file = make_argv.items.len - 1;
......@@ -5285,10 +5285,20 @@ fn cmdBuild(
52855285 } });
52865286 defer _ = make_runner_task.cancel(io) catch {};
52875287
5288 make_argv.items[argv_index_zig_lib_dir] = dirs.zig_lib.path orelse cwd_path;
5288 const pkg_root: Path = if (override_pkg_dir) |p|
5289 .initCwd(p)
5290 else if (system_pkg_dir_path) |p|
5291 .initCwd(p)
5292 else
5293 .{
5294 .root_dir = build_root.directory,
5295 .sub_path = "zig-pkg",
5296 };
5297
5298 make_argv.items[make_argv_index_zig_lib_dir] = dirs.zig_lib.path orelse cwd_path;
52895299 make_argv.items[make_argv_index_build_root] = build_root.directory.path orelse cwd_path;
5290 make_argv.items[argv_index_global_cache_dir] = dirs.global_cache.path orelse cwd_path;
5291 make_argv.items[argv_index_cache_dir] = dirs.local_cache.path orelse cwd_path;
5300 make_argv.items[make_argv_index_global_cache_dir] = dirs.global_cache.path orelse cwd_path;
5301 make_argv.items[make_argv_index_cache_dir] = dirs.local_cache.path orelse cwd_path;
52925302
52935303 configure_argv.items[conf_argv_index_build_root] = build_root.directory.path orelse cwd_path;
52945304
......@@ -5385,15 +5395,7 @@ fn cmdBuild(
53855395 .global_cache = dirs.global_cache,
53865396 .local_storage = &.{
53875397 .cache_root = .{ .root_dir = dirs.local_cache, .sub_path = "" },
5388 .pkg_root = if (override_pkg_dir) |p|
5389 .initCwd(p)
5390 else if (system_pkg_dir_path) |p|
5391 .initCwd(p)
5392 else
5393 .{
5394 .root_dir = build_root.directory,
5395 .sub_path = "zig-pkg",
5396 },
5398 .pkg_root = pkg_root,
53975399 },
53985400 .recursive = true,
53995401 .debug_hash = false,