Skip to content

Commit d8798dd

Browse files
fix(shell): normalize Windows PATH keys in copied environments (#19965)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 86f4a74 commit d8798dd

6 files changed

Lines changed: 77 additions & 4 deletions

File tree

‎apps/desktop/src/shell/DesktopShellEnvironment.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,23 @@ describe("DesktopShellEnvironment", () => {
486486
}),
487487
);
488488

489+
it.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
490+
Effect.gen(function* () {
491+
const env: NodeJS.ProcessEnv = { Path: "C:\\Windows\\System32" };
492+
493+
yield* runShellEnvironment({
494+
env,
495+
platform: "win32",
496+
handler: (command) =>
497+
command._tag === "StandardCommand" && command.args.includes("-NoProfile")
498+
? envOutput({ PATH: "C:\\Shell\\Bin" })
499+
: "",
500+
});
501+
502+
assert.deepEqual(env, { PATH: "C:\\Shell\\Bin;C:\\Windows\\System32" });
503+
}),
504+
);
505+
489506
it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
490507
Effect.gen(function* () {
491508
const env: NodeJS.ProcessEnv = {

‎apps/desktop/src/shell/DesktopShellEnvironment.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { listLoginShellCandidates } from "@t3tools/shared/shell";
1+
import { listLoginShellCandidates, setWindowsPath } from "@t3tools/shared/shell";
22
import * as Context from "effect/Context";
33
import * as Duration from "effect/Duration";
44
import * as Effect from "effect/Effect";
@@ -386,7 +386,7 @@ const installWindowsEnvironment = Effect.fn("desktop.shellEnvironment.installWin
386386
]);
387387

388388
if (Option.isSome(mergedPath)) {
389-
config.env.PATH = mergedPath.value;
389+
setWindowsPath(config.env, mergedPath.value);
390390
}
391391
if (!config.env.FNM_DIR && profile.FNM_DIR) {
392392
config.env.FNM_DIR = profile.FNM_DIR;

‎apps/server/src/os-jank.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,19 @@ effectIt.effect("repairs Windows PATH even when the desktop marks it prepared",
152152
assert.equal(env.PATH, "C:\\Tools;C:\\Windows");
153153
}),
154154
);
155+
156+
effectIt.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
157+
Effect.gen(function* () {
158+
const env: NodeJS.ProcessEnv = { Path: "C:\\Windows" };
159+
160+
yield* fixPath().pipe(
161+
Effect.provideService(HostProcess.Platform, "win32"),
162+
Effect.provideService(HostProcess.Environment, env),
163+
Effect.provideService(WindowsShellEnvironment, () => ({ PATH: "C:\\Tools" })),
164+
Effect.provideService(CommandAvailability, () => Effect.succeed(true)),
165+
Effect.provide(NodeServices.layer),
166+
);
167+
168+
assert.deepEqual(env, { PATH: "C:\\Tools;C:\\Windows" });
169+
}),
170+
);

‎apps/server/src/os-jank.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
readEnvironmentFromLoginShell,
66
readPathFromLaunchctl,
77
resolveWindowsEnvironment,
8+
setWindowsPath,
89
} from "@t3tools/shared/shell";
910
import * as Effect from "effect/Effect";
1011
import * as FileSystem from "effect/FileSystem";
@@ -71,7 +72,10 @@ export const fixPath = Effect.fn("fixPath")(function* (options?: {
7172
),
7273
);
7374
for (const [key, value] of Object.entries(repairedEnvironment)) {
74-
if (value !== undefined) {
75+
if (value === undefined) continue;
76+
if (key === "PATH") {
77+
setWindowsPath(env, value);
78+
} else {
7579
env[key] = value;
7680
}
7781
}

‎packages/shared/src/shell.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,29 @@ effectIt.layer(NodeServices.layer)("resolveWindowsEnvironment", (it) => {
879879
}),
880880
);
881881

882+
it.effect(
883+
"probes node with one PATH key when inherited environment records have mixed casing",
884+
() =>
885+
Effect.gen(function* () {
886+
const probedEnvironments: Array<NodeJS.ProcessEnv | undefined> = [];
887+
888+
yield* withWindowsEnvironmentMocks(
889+
resolveWindowsEnvironment({ Path: "C:\\Stale", PATH: "C:\\Windows\\System32" }),
890+
() => ({ PATH: "C:\\Shell\\Bin" }),
891+
(_command, options) =>
892+
Effect.sync(() => {
893+
probedEnvironments.push(options?.env);
894+
return true;
895+
}),
896+
);
897+
898+
const pathEntries = Object.entries(probedEnvironments[0] ?? {}).filter(
899+
([key]) => key.toUpperCase() === "PATH",
900+
);
901+
expect(pathEntries).toEqual([["PATH", "C:\\Shell\\Bin;C:\\Windows\\System32"]]);
902+
}),
903+
);
904+
882905
it.effect("loads the PowerShell profile when baseline env cannot resolve node", () =>
883906
Effect.gen(function* () {
884907
const readEnvironment = vi.fn(

‎packages/shared/src/shell.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,13 +805,26 @@ function readWindowsEnvironmentSafely(
805805
}
806806
}
807807

808+
/** Keep one PATH spelling in copied Windows environment records. */
809+
export function setWindowsPath(env: NodeJS.ProcessEnv, value: string): void {
810+
for (const key of Object.keys(env)) {
811+
if (key.toUpperCase() === "PATH") {
812+
delete env[key];
813+
}
814+
}
815+
env.PATH = value;
816+
}
817+
808818
function mergeWindowsEnv(
809819
currentEnv: NodeJS.ProcessEnv,
810820
patch: Partial<Record<string, string>>,
811821
): NodeJS.ProcessEnv {
812822
const nextEnv: NodeJS.ProcessEnv = { ...currentEnv };
813823
for (const [key, value] of Object.entries(patch)) {
814-
if (value !== undefined) {
824+
if (value === undefined) continue;
825+
if (key === "PATH") {
826+
setWindowsPath(nextEnv, value);
827+
} else {
815828
nextEnv[key] = value;
816829
}
817830
}

0 commit comments

Comments
 (0)