authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-19 17:53:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-19 17:54:39-07:00
log5ee54cd52ad5f03ce24ec8c0ffee35972df3a5f3
tree6ae58cc4a9c3efb731a2784d5c8366c675a64e88
parent7b0f777ffdd2900e7dcd5db096b27a06bc416263

Compilation: avoid putting superfluous dot in error paths


1 files changed, 11 insertions(+), 13 deletions(-)

src/Compilation.zig+11-13
......@@ -435,7 +435,7 @@ pub const Path = struct {
435435 const dir = switch (p.root) {
436436 .none => {
437437 const cwd_sub_path = absToCwdRelative(p.sub_path, dirs.cwd);
438 return .{ Io.Dir.cwd(), cwd_sub_path };
438 return .{ Io.Dir.cwd(), if (cwd_sub_path.len == 0) "." else cwd_sub_path };
439439 },
440440 .zig_lib => dirs.zig_lib.handle,
441441 .global_cache => dirs.global_cache.handle,
......@@ -456,20 +456,18 @@ pub const Path = struct {
456456 comp: *Compilation,
457457 pub fn format(f: Formatter, w: *Writer) Writer.Error!void {
458458 const root_path: []const u8 = switch (f.p.root) {
459 .zig_lib => f.comp.dirs.zig_lib.path orelse ".",
460 .global_cache => f.comp.dirs.global_cache.path orelse ".",
461 .local_cache => f.comp.dirs.local_cache.path orelse ".",
462 .build_root => f.comp.dirs.build_root.path orelse ".",
459 .zig_lib => f.comp.dirs.zig_lib.path orelse "",
460 .global_cache => f.comp.dirs.global_cache.path orelse "",
461 .local_cache => f.comp.dirs.local_cache.path orelse "",
462 .build_root => f.comp.dirs.build_root.path orelse "",
463463 .none => {
464 const cwd_sub_path = absToCwdRelative(f.p.sub_path, f.comp.dirs.cwd);
465 try w.writeAll(cwd_sub_path);
464 try w.writeAll(absToCwdRelative(f.p.sub_path, f.comp.dirs.cwd));
466465 return;
467466 },
468467 };
469 assert(root_path.len != 0);
470468 try w.writeAll(root_path);
471469 if (f.p.sub_path.len > 0) {
472 try w.writeByte(fs.path.sep);
470 if (root_path.len != 0) try w.writeByte(fs.path.sep);
473471 try w.writeAll(f.p.sub_path);
474472 }
475473 }
......@@ -477,16 +475,16 @@ pub const Path = struct {
477475
478476 /// Given the `sub_path` of a `Path` with `Path.root == .none`, attempts to convert
479477 /// the (absolute) path to a cwd-relative path. Otherwise, returns the absolute path
480 /// unmodified. The returned string is never empty: "" is converted to ".".
478 /// unmodified. The returned string is never "."; empty string will be returned instead.
481479 fn absToCwdRelative(sub_path: []const u8, cwd_path: []const u8) []const u8 {
482480 if (builtin.target.os.tag == .wasi) {
483 if (sub_path.len == 0) return ".";
481 if (sub_path.len == 0) return "";
484482 assert(!fs.path.isAbsolute(sub_path));
485483 return sub_path;
486484 }
487485 assert(fs.path.isAbsolute(sub_path));
488486 if (!std.mem.startsWith(u8, sub_path, cwd_path)) return sub_path;
489 if (sub_path.len == cwd_path.len) return "."; // the strings are equal
487 if (sub_path.len == cwd_path.len) return ""; // the strings are equal
490488 const path_sep_index = path_sep_index: {
491489 // cwd is just a root, e.g. / or C:\
492490 if (cwd_path[cwd_path.len - 1] == fs.path.sep) break :path_sep_index cwd_path.len - 1;
......@@ -647,7 +645,7 @@ pub const Path = struct {
647645 const cwd_sub_path = absToCwdRelative(p.sub_path, dirs.cwd);
648646 return .{
649647 .root_dir = .cwd(),
650 .sub_path = cwd_sub_path,
648 .sub_path = if (cwd_sub_path.len == 0) null else cwd_sub_path,
651649 };
652650 },
653651 };