Merge pull request #1971 from hauleth/fix/append-to-manpath-instead-of-overriding

fix: instead of overriding MANPATHs set by OS, append to them
This commit is contained in:
Mitchell Hashimoto
2024-07-18 18:53:56 -07:00
committed by GitHub
2 changed files with 24 additions and 11 deletions

View File

@ -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,

View File

@ -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.
// 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.appendEnv(alloc, manpath, dir),
try internal_os.appendEnvAlways(
alloc,
env.get("MATHPATH") orelse "",
dir,
),
);
} else {
try env.put("MANPATH", dir);
}
}
}