Skip to content

Commit 5948f68

Browse files
fix(server): discover editors on the hydrated PATH
Editor discovery read PATH through Effect Config. The default ConfigProvider copies process.env the first time it is used, which happens while ServerConfig resolves - before fixPath() hydrates PATH from the login shell. Discovery therefore kept looking at the bare launchd PATH (/usr/bin:/bin:/usr/sbin:/sbin) of a Dock/Finder launch, where only `open` resolves, so the Open-in picker offered Finder and nothing else. Launching the app from a terminal hid the bug because the inherited PATH was already complete. Read the launcher's env from HostProcessEnvironment instead, which is the object fixPath() mutates. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 93e0416 commit 5948f68

2 files changed

Lines changed: 60 additions & 48 deletions

File tree

‎apps/server/src/process/externalLauncher.test.ts‎

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as NodeOS from "node:os";
55
import * as NodePath from "node:path";
66
import * as NodeServices from "@effect/platform-node/NodeServices";
77
import { assert, it } from "@effect/vitest";
8-
import * as ConfigProvider from "effect/ConfigProvider";
98
import * as Effect from "effect/Effect";
109
import * as Fiber from "effect/Fiber";
1110
import * as FileSystem from "effect/FileSystem";
@@ -16,7 +15,7 @@ import * as Stream from "effect/Stream";
1615
import * as TestClock from "effect/testing/TestClock";
1716
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
1817

19-
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
18+
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
2019
import { SpawnExecutableResolution } from "@t3tools/shared/shell";
2120
import * as ExternalLauncher from "./externalLauncher.ts";
2221

@@ -89,7 +88,7 @@ const testLayer = (input: {
8988
SpawnExecutableResolution,
9089
(command) => input.resolveExecutable?.(command) ?? command,
9190
),
92-
ConfigProvider.layer(ConfigProvider.fromEnv({ env: input.env ?? {} })),
91+
Layer.succeed(HostProcessEnvironment, input.env ?? {}),
9392
);
9493
};
9594

@@ -716,6 +715,31 @@ it.effect.skipIf(windowsHost)("reveals by opening the containing directory on Li
716715
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
717716
);
718717

718+
// A Dock/Finder launch starts with the bare launchd PATH and `fixPath` hydrates
719+
// process.env in place afterwards; discovery has to see the hydrated value.
720+
it.effect.skipIf(windowsHost)("discovers editors on a PATH hydrated after startup", () =>
721+
Effect.gen(function* () {
722+
const fileSystem = yield* FileSystem.FileSystem;
723+
const path = yield* Path.Path;
724+
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
725+
const cursorPath = path.join(binDir, "cursor");
726+
yield* fileSystem.writeFileString(cursorPath, "#!/bin/sh\n");
727+
yield* fileSystem.chmod(cursorPath, 0o755);
728+
729+
const emptyDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-empty-" });
730+
const env: Record<string, string> = { PATH: emptyDir };
731+
const editors = yield* Effect.gen(function* () {
732+
const launcher = yield* ExternalLauncher.ExternalLauncher;
733+
env.PATH = `${binDir}:${emptyDir}`;
734+
return yield* launcher.resolveAvailableEditors();
735+
}).pipe(Effect.provide(testLayer({ platform: "linux", env })));
736+
737+
// Exact match: a lookup that fell back to the ambient process PATH would
738+
// also report whatever editors the host machine has installed.
739+
assert.deepEqual(editors, ["cursor"]);
740+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
741+
);
742+
719743
it.effect.skipIf(windowsHost)(
720744
"does not advertise a Linux file manager without a graphical session",
721745
() =>
@@ -968,14 +992,10 @@ it.effect("memoizes editor discovery and refreshes after the cache window", () =
968992
Layer.mergeAll(
969993
launcherLayer,
970994
Layer.succeed(HostProcessPlatform, "win32"),
971-
ConfigProvider.layer(
972-
ConfigProvider.fromEnv({
973-
env: {
974-
PATH: "C:\\t3-editor-discovery-cache-test",
975-
PATHEXT: ".COM;.EXE;.BAT;.CMD",
976-
},
977-
}),
978-
),
995+
Layer.succeed(HostProcessEnvironment, {
996+
PATH: "C:\\t3-editor-discovery-cache-test",
997+
PATHEXT: ".COM;.EXE;.BAT;.CMD",
998+
}),
979999
TestClock.layer(),
9801000
),
9811001
),
@@ -1033,14 +1053,10 @@ it.effect("rescans after an interrupted discovery instead of caching the interru
10331053
Layer.mergeAll(
10341054
launcherLayer,
10351055
Layer.succeed(HostProcessPlatform, "win32"),
1036-
ConfigProvider.layer(
1037-
ConfigProvider.fromEnv({
1038-
env: {
1039-
PATH: "C:\\t3-editor-discovery-interrupt-test",
1040-
PATHEXT: ".COM;.EXE;.BAT;.CMD",
1041-
},
1042-
}),
1043-
),
1056+
Layer.succeed(HostProcessEnvironment, {
1057+
PATH: "C:\\t3-editor-discovery-interrupt-test",
1058+
PATHEXT: ".COM;.EXE;.BAT;.CMD",
1059+
}),
10441060
),
10451061
),
10461062
);

‎apps/server/src/process/externalLauncher.ts‎

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ import {
1818
type FileManagerRevealKind,
1919
type LaunchEditorInput,
2020
} from "@t3tools/contracts";
21-
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
21+
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
2222
import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell";
2323
import * as Clock from "effect/Clock";
24-
import * as Config from "effect/Config";
2524
import * as Context from "effect/Context";
2625
import * as Effect from "effect/Effect";
2726
import * as Encoding from "effect/Encoding";
@@ -82,37 +81,34 @@ const DETACHED_IGNORE_STDIO_OPTIONS = {
8281
stderr: "ignore",
8382
} as const satisfies ChildProcess.CommandOptions;
8483

85-
const compactEnv = (input: Record<string, Option.Option<string>>): NodeJS.ProcessEnv =>
86-
Object.fromEntries(
87-
Object.entries(input).flatMap(([key, value]) =>
88-
Option.match(value, {
89-
onNone: () => [],
90-
onSome: (resolved) => [[key, resolved]],
84+
const BROWSER_LAUNCH_ENV_NAMES = [
85+
"SYSTEMROOT",
86+
"windir",
87+
"WSL_DISTRO_NAME",
88+
"WSL_INTEROP",
89+
"SSH_CONNECTION",
90+
"SSH_TTY",
91+
"container",
92+
"DISPLAY",
93+
"WAYLAND_DISPLAY",
94+
] as const;
95+
96+
const COMMAND_LOOKUP_ENV_NAMES = ["PATH", "Path", "path", "PATHEXT"] as const;
97+
98+
// Not Config: the default ConfigProvider snapshots process.env on first use,
99+
// before `fixPath` hydrates PATH, so it only ever sees the bare launchd PATH.
100+
const readHostEnv = (names: ReadonlyArray<string>) =>
101+
Effect.map(HostProcessEnvironment, (env): NodeJS.ProcessEnv =>
102+
Object.fromEntries(
103+
names.flatMap((name) => {
104+
const value = env[name];
105+
return value === undefined || value.length === 0 ? [] : [[name, value]];
91106
}),
92107
),
93108
);
94109

95-
const BrowserLaunchEnvConfig = Config.all({
96-
SYSTEMROOT: Config.String("SYSTEMROOT").pipe(Config.option),
97-
windir: Config.String("windir").pipe(Config.option),
98-
WSL_DISTRO_NAME: Config.String("WSL_DISTRO_NAME").pipe(Config.option),
99-
WSL_INTEROP: Config.String("WSL_INTEROP").pipe(Config.option),
100-
SSH_CONNECTION: Config.String("SSH_CONNECTION").pipe(Config.option),
101-
SSH_TTY: Config.String("SSH_TTY").pipe(Config.option),
102-
container: Config.String("container").pipe(Config.option),
103-
DISPLAY: Config.String("DISPLAY").pipe(Config.option),
104-
WAYLAND_DISPLAY: Config.String("WAYLAND_DISPLAY").pipe(Config.option),
105-
}).pipe(Config.map(compactEnv));
106-
107-
const CommandLookupEnvConfig = Config.all({
108-
PATH: Config.String("PATH").pipe(Config.option),
109-
Path: Config.String("Path").pipe(Config.option),
110-
path: Config.String("path").pipe(Config.option),
111-
PATHEXT: Config.String("PATHEXT").pipe(Config.option),
112-
}).pipe(Config.map(compactEnv));
113-
114-
const readBrowserLaunchEnv = BrowserLaunchEnvConfig.pipe(Effect.orElseSucceed(() => ({})));
115-
const readCommandLookupEnv = CommandLookupEnvConfig.pipe(Effect.orElseSucceed(() => ({})));
110+
const readBrowserLaunchEnv = readHostEnv(BROWSER_LAUNCH_ENV_NAMES);
111+
const readCommandLookupEnv = readHostEnv(COMMAND_LOOKUP_ENV_NAMES);
116112

117113
function parseTargetPathAndPosition(target: string): Option.Option<TargetPathAndPosition> {
118114
const match = TARGET_WITH_POSITION_PATTERN.exec(target);

0 commit comments

Comments
 (0)