From d0471726b19a8f24d74d58481cd071f788a46097 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 25 Oct 2026 03:59:50 -0700 Subject: [PATCH] cmd/fetch: refinement around how local dependencies are handled - src: local name path would work as intended when the dep was in the root but when a local dep had a local dep of its own 'path' was still relative to the root instead of the module dir --- docs/zig.mod.md | 2 +- src/cmd/fetch.zig | 4 ++++ src/common.zig | 18 ++++++++---------- src/util/modfile.zig | 4 ++++ src/util/module.zig | 2 +- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/docs/zig.mod.md b/docs/zig.mod.md index 6e6210f412afe8b253ba03f59492038599925343..401b61b54a804cc827950f12a3171b1f213ee816 100644 --- a/docs/zig.mod.md +++ b/docs/zig.mod.md @@ -88,7 +88,7 @@ The available `type`s are: For the full details on `Dep` types, you can check out the source where the enum is defined: https://github.com/nektro/zigmod/blob/master/src/util/dep_type.zig. -> Note: the `local` type modifies the input behavior to be shorthand for `
` rather than `path version` since the latter fields don't make sense for local files. +> Note: the `local` type modifies the input behavior to be shorthand for `name path` rather than `path ?version` since the latter fields don't make sense for local files. #### Dep `version` - Type: `string-string` diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index 42f352d661609052cabf8f8cb6b5a60170b6d39f..da0ced2a0937e69265546fcc7a0e215bdfe2abc0 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -325,6 +325,10 @@ fn print_dirs(w: nfs.File, list: []const zigmod.Module, alloc: std.mem.Allocator try w.print(" pub const _{s} = \"{}\";\n", .{ mod.short_id(), u.altStringEscape(cwd_realpath) }); continue; } + if (mod.type == .local) { + try w.print(" pub const _{s} = \"{}\";\n", .{ mod.short_id(), u.altStringEscape(mod.clean_path) }); + continue; + } try w.print(" pub const _{s} = cache ++ \"/{}\";\n", .{ mod.short_id(), u.altStringEscape(mod.clean_path) }); } } diff --git a/src/common.zig b/src/common.zig index 7f78fa48e0d885ff552e622690895477e27776fd..d1669d49e0fb5d5b4a6210d60a2785ec9ae4611a 100644 --- a/src/common.zig +++ b/src/common.zig @@ -39,12 +39,12 @@ pub fn collect_deps_deep(cachepath: [:0]const u8, mdir: nfs.Dir, options: *Colle } try moduledeps.append(try collect_deps(cachepath, mdir, ".", .local, options)); for (m.rootdeps) |*d| { - if (try get_module_from_dep(d, cachepath, options)) |founddep| { + if (try get_module_from_dep(d, mdir, cachepath, options)) |founddep| { try moduledeps.append(founddep); } } for (m.builddeps) |*d| { - if (try get_module_from_dep(d, cachepath, options)) |founddep| { + if (try get_module_from_dep(d, mdir, cachepath, options)) |founddep| { try moduledeps.append(founddep); } } @@ -72,7 +72,7 @@ pub fn collect_deps(cachepath: [:0]const u8, mdir: nfs.Dir, mdir_path: string, d try gen_files_package(options.alloc, cachepath, mdir, m.files); } for (m.deps) |*d| { - if (try get_module_from_dep(d, cachepath, options)) |founddep| { + if (try get_module_from_dep(d, if (dtype == .local) mdir else nfs.cwd(), cachepath, options)) |founddep| { try moduledeps.append(founddep); } } @@ -122,9 +122,6 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! } switch (d.type) { .local => { - if (!std.mem.endsWith(u8, d.main, ".zig")) { - return gpa.dupeSentinel(u8, d.main, 0); - } return d.path; }, .system_lib, .framework => { @@ -216,7 +213,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! } } -pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: [:0]const u8, options: *CollectOptions) anyerror!?zigmod.Module { +pub fn get_module_from_dep(d: *zigmod.Dep, localdir: nfs.Dir, cachepath: [:0]const u8, options: *CollectOptions) anyerror!?zigmod.Module { if (options.lock) |lock| { for (lock) |item| { if (std.mem.eql(u8, item[0], try d.clean_path(options.alloc))) { @@ -229,7 +226,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: [:0]const u8, options: *Co } if (!d.is_for_this()) return null; const modpath = try get_modpath(cachepath, d.*, options); - const moddir = if (modpath.len == 0) try nfs.cwd().openDir(cachepath, .{}) else try nfs.cwd().openDir(modpath, .{}); + const moddir = if (modpath.len == 0) try nfs.cwd().openDir(cachepath, .{}) else try localdir.openDir(modpath, .{}); const nocache = d.type.isLocal(); if (!nocache) try options.already_fetched.append(modpath); @@ -287,10 +284,12 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: [:0]const u8, options: *Co }; dd.dep = d.*; dd.for_build = d.for_build; - const save = dd; if (d.type != .local) { const new_clean_path = extras.trimPrefix(modpath, cachepath)[1..]; dd.clean_path = new_clean_path.ptr[0..new_clean_path.len :0]; + } else { + dd.clean_path = try moddir.realpathAlloc(gpa, "."); + dd.clean_path = dd.clean_path[std.fs.path.dirname(std.fs.path.dirname(cachepath).?).?.len + 1 ..]; } if (std.mem.eql(u8, &dd.id, &zigmod.Dep.EMPTY)) dd.id = u.random_string(48); if (d.name.len > 0) dd.name = d.name; @@ -300,7 +299,6 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: [:0]const u8, options: *Co if (d.c_source_files.len > 0) dd.c_source_files = d.c_source_files; if (d.only_os.len > 0) dd.only_os = d.only_os; if (d.except_os.len > 0) dd.except_os = d.except_os; - if (d.type == .local) dd.main = try std.fs.path.join(options.alloc, &.{ d.main, save.main }); if (dd.is_for_this()) return dd; return null; }, diff --git a/src/util/modfile.zig b/src/util/modfile.zig index ff4f3e0080f0d398f1c1b14d0cccb34df25a9382..2e663b412625088ef9d5b6b3eb9b4504511e934e 100644 --- a/src/util/modfile.zig +++ b/src/util/modfile.zig @@ -132,6 +132,10 @@ pub const ModFile = struct { main = version.?; version = ""; } + if (!std.mem.endsWith(u8, main, ".zig")) { + path = main; + main = ""; + } } var id = zigmod.Dep.EMPTY; diff --git a/src/util/module.zig b/src/util/module.zig index 5bf8854e1e0865ee681d2d17fc1c94f37a18bc33..4f4129d4c7af5d117c8e51258616598c66c548f1 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -38,7 +38,7 @@ pub const Module = struct { errdefer moddeps.deinit(); for (dep.deps) |*d| { - if (try common.get_module_from_dep(d, cachepath, options)) |founddep| { + if (try common.get_module_from_dep(d, nfs.cwd(), cachepath, options)) |founddep| { try moddeps.append(founddep); } } -- 2.54.1