Skip to content
TypeScript

fix(shell): normalize Windows PATH keys in copied environments (#19965)

Julius Marminged8798dd7e9ae
diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts
index 6efe817574a..8f77089e72c 100644
--- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts
+++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts
@@ -486,6 +486,23 @@ describe("DesktopShellEnvironment", () => {
}),
);
+ it.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
+ Effect.gen(function* () {
+ const env: NodeJS.ProcessEnv = { Path: "C:\\Windows\\System32" };
+
+ yield* runShellEnvironment({
+ env,
+ platform: "win32",
+ handler: (command) =>
+ command._tag === "StandardCommand" && command.args.includes("-NoProfile")
+ ? envOutput({ PATH: "C:\\Shell\\Bin" })
+ : "",
+ });
+
+ assert.deepEqual(env, { PATH: "C:\\Shell\\Bin;C:\\Windows\\System32" });
+ }),
+ );
+
it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
Effect.gen(function* () {
const env: NodeJS.ProcessEnv = {
diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts
index 2b41b6ea7db..39651a7e5b9 100644
--- a/apps/desktop/src/shell/DesktopShellEnvironment.ts
+++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts
@@ -1,4 +1,4 @@
-import { listLoginShellCandidates } from "@t3tools/shared/shell";
+import { listLoginShellCandidates, setWindowsPath } from "@t3tools/shared/shell";
import * as Context from "effect/Context";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
@@ -386,7 +386,7 @@ const installWindowsEnvironment = Effect.fn("desktop.shellEnvironment.installWin
]);
if (Option.isSome(mergedPath)) {
- config.env.PATH = mergedPath.value;
+ setWindowsPath(config.env, mergedPath.value);
}
if (!config.env.FNM_DIR && profile.FNM_DIR) {
config.env.FNM_DIR = profile.FNM_DIR;
diff --git a/apps/server/src/os-jank.test.ts b/apps/server/src/os-jank.test.ts
index b8f5848ff10..c06ef9eab6d 100644
--- a/apps/server/src/os-jank.test.ts
+++ b/apps/server/src/os-jank.test.ts
@@ -152,3 +152,19 @@ effectIt.effect("repairs Windows PATH even when the desktop marks it prepared",
assert.equal(env.PATH, "C:\\Tools;C:\\Windows");
}),
);
+
+effectIt.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
+ Effect.gen(function* () {
+ const env: NodeJS.ProcessEnv = { Path: "C:\\Windows" };
+
+ yield* fixPath().pipe(
+ Effect.provideService(HostProcess.Platform, "win32"),
+ Effect.provideService(HostProcess.Environment, env),
+ Effect.provideService(WindowsShellEnvironment, () => ({ PATH: "C:\\Tools" })),
+ Effect.provideService(CommandAvailability, () => Effect.succeed(true)),
+ Effect.provide(NodeServices.layer),
+ );
+
+ assert.deepEqual(env, { PATH: "C:\\Tools;C:\\Windows" });
+ }),
+);
diff --git a/apps/server/src/os-jank.ts b/apps/server/src/os-jank.ts
index c1948c60908..deeefae367b 100644
--- a/apps/server/src/os-jank.ts
+++ b/apps/server/src/os-jank.ts
@@ -5,6 +5,7 @@ import {
readEnvironmentFromLoginShell,
readPathFromLaunchctl,
resolveWindowsEnvironment,
+ setWindowsPath,
} from "@t3tools/shared/shell";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
@@ -71,7 +72,10 @@ export const fixPath = Effect.fn("fixPath")(function* (options?: {
),
);
for (const [key, value] of Object.entries(repairedEnvironment)) {
- if (value !== undefined) {
+ if (value === undefined) continue;
+ if (key === "PATH") {
+ setWindowsPath(env, value);
+ } else {
env[key] = value;
}
}
diff --git a/packages/shared/src/shell.test.ts b/packages/shared/src/shell.test.ts
index 77ee8890575..d401824cee4 100644
--- a/packages/shared/src/shell.test.ts
+++ b/packages/shared/src/shell.test.ts
@@ -879,6 +879,29 @@ effectIt.layer(NodeServices.layer)("resolveWindowsEnvironment", (it) => {
}),
);
+ it.effect(
+ "probes node with one PATH key when inherited environment records have mixed casing",
+ () =>
+ Effect.gen(function* () {
+ const probedEnvironments: Array<NodeJS.ProcessEnv | undefined> = [];
+
+ yield* withWindowsEnvironmentMocks(
+ resolveWindowsEnvironment({ Path: "C:\\Stale", PATH: "C:\\Windows\\System32" }),
+ () => ({ PATH: "C:\\Shell\\Bin" }),
+ (_command, options) =>
+ Effect.sync(() => {
+ probedEnvironments.push(options?.env);
+ return true;
+ }),
+ );
+
+ const pathEntries = Object.entries(probedEnvironments[0] ?? {}).filter(
+ ([key]) => key.toUpperCase() === "PATH",
+ );
+ expect(pathEntries).toEqual([["PATH", "C:\\Shell\\Bin;C:\\Windows\\System32"]]);
+ }),
+ );
+
it.effect("loads the PowerShell profile when baseline env cannot resolve node", () =>
Effect.gen(function* () {
const readEnvironment = vi.fn(
diff --git a/packages/shared/src/shell.ts b/packages/shared/src/shell.ts
index 36200c4fa0c..fe376cb183a 100644
--- a/packages/shared/src/shell.ts
+++ b/packages/shared/src/shell.ts
@@ -805,13 +805,26 @@ function readWindowsEnvironmentSafely(
}
}
+/** Keep one PATH spelling in copied Windows environment records. */
+export function setWindowsPath(env: NodeJS.ProcessEnv, value: string): void {
+ for (const key of Object.keys(env)) {
+ if (key.toUpperCase() === "PATH") {
+ delete env[key];
+ }
+ }
+ env.PATH = value;
+}
+
function mergeWindowsEnv(
currentEnv: NodeJS.ProcessEnv,
patch: Partial<Record<string, string>>,
): NodeJS.ProcessEnv {
const nextEnv: NodeJS.ProcessEnv = { ...currentEnv };
for (const [key, value] of Object.entries(patch)) {
- if (value !== undefined) {
+ if (value === undefined) continue;
+ if (key === "PATH") {
+ setWindowsPath(nextEnv, value);
+ } else {
nextEnv[key] = value;
}
}