os: directory functions should prefer cached home if available

This fixes tests as well if env vars are set.
This commit is contained in:
Mitchell Hashimoto
2025-01-03 09:46:13 -08:00
parent 7eb35d7275
commit e03c428728

View File

@ -58,6 +58,15 @@ fn dir(
opts: Options,
internal_opts: InternalOptions,
) ![]u8 {
// If we have a cached home dir, use that.
if (opts.home) |home| {
return try std.fs.path.join(alloc, &[_][]const u8{
home,
internal_opts.default_subdir,
opts.subdir orelse "",
});
}
// First check the env var. On Windows we have to allocate so this tracks
// both whether we have the env var and whether we own it.
// on Windows we treat `LOCALAPPDATA` as a fallback for `XDG_CONFIG_HOME`
@ -93,15 +102,6 @@ fn dir(
return try alloc.dupe(u8, env);
}
// If we have a cached home dir, use that.
if (opts.home) |home| {
return try std.fs.path.join(alloc, &[_][]const u8{
home,
internal_opts.default_subdir,
opts.subdir orelse "",
});
}
// Get our home dir
var buf: [1024]u8 = undefined;
if (try homedir.home(&buf)) |home| {