fix(setup): ignore empty home directory selections

Skip empty home environment values while preserving nonempty caller selections and platform precedence. Setup, doctor, and uninstall now use the same fallback home instead of writing relative paths in the current directory.

Validation: focused home tests and the public setup/doctor/uninstall lifecycle under Node 24.18. Package verification and pack dry run are included in the integrated gate.

Fixes #2325
This commit is contained in:
morluto
2026-10-13 04:11:55 +08:00
parent 1c37914306
commit c339f9ebe7
3 changed files with 221 additions and 6 deletions
+18 -6
View File
@@ -1,17 +1,29 @@
import { userInfo } from "node:os";
const firstNonempty = (...values: readonly (string | undefined)[]) =>
values.find(
(value): value is string => value !== undefined && value.length > 0,
);
/** Resolve the caller's selected home before consulting the host account. */
export const homeDirectoryFromEnvironment = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
): string => {
const windowsDriveHome =
environment.HOMEDRIVE !== undefined &&
environment.HOMEDRIVE.length > 0 &&
environment.HOMEPATH !== undefined &&
environment.HOMEPATH.length > 0
? `${environment.HOMEDRIVE}${environment.HOMEPATH}`
: undefined;
const selected =
platform === "win32"
? (environment.USERPROFILE ??
(environment.HOMEDRIVE !== undefined &&
environment.HOMEPATH !== undefined
? `${environment.HOMEDRIVE}${environment.HOMEPATH}`
: environment.HOME))
: (environment.HOME ?? environment.USERPROFILE);
? firstNonempty(
environment.USERPROFILE,
windowsDriveHome,
environment.HOME,
)
: firstNonempty(environment.HOME, environment.USERPROFILE);
return selected ?? userInfo().homedir;
};
+64
View File
@@ -0,0 +1,64 @@
import { userInfo } from "node:os";
import { describe, expect, it } from "vitest";
import { homeDirectoryFromEnvironment } from "./config/homeDirectory.js";
describe("home directory environment selection", () => {
it("skips an empty HOME before selecting USERPROFILE on POSIX", () => {
expect(
homeDirectoryFromEnvironment(
{ HOME: "", USERPROFILE: "relative/profile" },
"linux",
),
).toBe("relative/profile");
});
it("falls back to the account home when POSIX home variables are empty", () => {
expect(
homeDirectoryFromEnvironment({ HOME: "", USERPROFILE: "" }, "linux"),
).toBe(userInfo().homedir);
});
it("preserves a nonempty relative Windows profile ahead of drive variables", () => {
expect(
homeDirectoryFromEnvironment(
{
USERPROFILE: "relative/profile",
HOMEDRIVE: "D:",
HOMEPATH: "\\Users\\account",
HOME: "other/home",
},
"win32",
),
).toBe("relative/profile");
});
it("uses the Windows drive pair when USERPROFILE is empty", () => {
expect(
homeDirectoryFromEnvironment(
{
USERPROFILE: "",
HOMEDRIVE: "D:",
HOMEPATH: "\\Users\\account",
HOME: "other/home",
},
"win32",
),
).toBe("D:\\Users\\account");
});
it("falls back to the account home when all Windows home values are empty", () => {
expect(
homeDirectoryFromEnvironment(
{
USERPROFILE: "",
HOMEDRIVE: "",
HOMEPATH: "",
HOME: "",
},
"win32",
),
).toBe(userInfo().homedir);
});
});