diff --git a/src/os/env.zig b/src/os/env.zig index 1c2270337..b40d2fef1 100644 --- a/src/os/env.zig +++ b/src/os/env.zig @@ -20,6 +20,19 @@ pub fn appendEnv( if (current.len == 0) return try alloc.dupe(u8, value); // Otherwise we must prefix. + return try appendEnvAlways(alloc, current, value); +} + +/// Always append value to environment, even when it is empty. +/// This is useful because some env vars (like MANPATH) want there +/// to be an empty prefix to preserve existing values. +/// +/// The returned value is always allocated so it must be freed. +pub fn appendEnvAlways( + alloc: Allocator, + current: []const u8, + value: []const u8, +) ![]u8 { return try std.fmt.allocPrint(alloc, "{s}{s}{s}", .{ current, PATH_SEP, diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 65607c67f..ac6bff7b7 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -648,17 +648,17 @@ const Subprocess = struct { break :man; }; - if (env.get("MANPATH")) |manpath| { - // Append to the existing MANPATH. It's very unlikely that our bundle's - // resources directory already appears here so we don't spend the time - // searching for it. - try env.put( - "MANPATH", - try internal_os.appendEnv(alloc, manpath, dir), - ); - } else { - try env.put("MANPATH", dir); - } + // Always append with colon in front, as it mean that if + // `MANPATH` is empty, then it should be treated as an extra + // path instead of overriding all paths set by OS. + try env.put( + "MANPATH", + try internal_os.appendEnvAlways( + alloc, + env.get("MATHPATH") orelse "", + dir, + ), + ); } }