Skip to content

Commit 408ff8a

Browse files
Bil0000shivamhwp
andauthored
fix: detect installed editors outside PATH (pingdotgg#12439)
Co-authored-by: shivam <[email protected]>
1 parent 7a326c2 commit 408ff8a

6 files changed

Lines changed: 341 additions & 37 deletions

File tree

‎apps/desktop/src/ipc/methods/window.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { assert, describe, it } from "@effect/vitest";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
4+
import * as FileSystem from "effect/FileSystem";
5+
import * as Path from "effect/Path";
26
import * as Effect from "effect/Effect";
37
import * as Layer from "effect/Layer";
48
import * as Option from "effect/Option";
@@ -26,6 +30,7 @@ import {
2630
getWindowFullscreenState,
2731
pasteAsText,
2832
pickProjectFavicon,
33+
probeRemoteEditors,
2934
} from "./window.ts";
3035

3136
const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
@@ -262,3 +267,34 @@ describe("pickProjectFavicon", () => {
262267
}),
263268
);
264269
});
270+
271+
it.effect.skipIf(HostProcessPlatform.defaultValue() === "win32")(
272+
"finds remote editors installed without PATH launchers",
273+
() =>
274+
Effect.gen(function* () {
275+
const fs = yield* FileSystem.FileSystem;
276+
const path = yield* Path.Path;
277+
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-remote-editors-" });
278+
for (const app of ["Cursor", "Visual Studio Code", "WebStorm"]) {
279+
const executable = path.join(
280+
home,
281+
"Applications",
282+
`${app}.app`,
283+
app === "WebStorm" ? "Contents/MacOS/webstorm" : "Contents/Resources/app/bin/code",
284+
);
285+
yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
286+
yield* fs.writeFileString(executable, "#!/bin/sh\n");
287+
yield* fs.chmod(executable, 0o755);
288+
}
289+
const editors = yield* probeRemoteEditors.handler(undefined).pipe(
290+
Effect.provideService(HostProcessEnvironment, {
291+
HOME: home,
292+
PATH: path.join(home, "empty"),
293+
}),
294+
Effect.provideService(HostProcessPlatform, "darwin"),
295+
);
296+
assert.include(editors, "cursor");
297+
assert.include(editors, "vscode");
298+
assert.notInclude(editors, "webstorm");
299+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
300+
);

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
type PickedThemeFile,
1515
} from "@t3tools/contracts";
1616
import { WORKSPACE_IMAGE_PREVIEW_EXTENSIONS } from "@t3tools/shared/filePreview";
17-
import { isCommandAvailable } from "@t3tools/shared/shell";
17+
import { resolveEditorCommand } from "@t3tools/shared/editor";
18+
import * as HostProcess from "@t3tools/shared/hostProcess";
1819
import * as NodeOS from "node:os";
1920
import * as FileSystem from "effect/FileSystem";
2021
import * as Path from "effect/Path";
@@ -333,20 +334,13 @@ export const probeRemoteEditors = DesktopIpc.makeIpcMethod({
333334
channel: IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL,
334335
payload: Schema.Undefined,
335336
result: Schema.Array(EditorId),
336-
// Probes THIS machine (where the renderer runs) for remote-capable editor
337-
// CLIs, unlike the server's probe which walks the environment host's PATH.
338-
// A Finder-launched app can miss PATH entries; an empty result makes the
339-
// renderer fall back to VS Code only, so that fails soft.
340337
handler: Effect.fn("desktop.ipc.window.probeRemoteEditors")(function* () {
341338
const available: Array<EditorId> = [];
339+
const env = yield* HostProcess.HostProcessEnvironment;
342340
for (const editorId of REMOTE_CAPABLE_EDITOR_IDS) {
343-
const commands = EDITORS.find((editor) => editor.id === editorId)?.commands;
344-
if (!commands) continue;
345-
for (const command of commands) {
346-
if (yield* isCommandAvailable(command, { env: process.env })) {
347-
available.push(editorId);
348-
break;
349-
}
341+
const editor = EDITORS.find((editor) => editor.id === editorId);
342+
if (editor && Option.isSome(yield* resolveEditorCommand(editor, env))) {
343+
available.push(editorId);
350344
}
351345
}
352346
return available;

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

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,159 @@ it.effect("discovers editors through the service API", () =>
922922
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
923923
);
924924

925+
for (const { platform, installPath, editor, args } of [
926+
{
927+
platform: "darwin",
928+
installPath: "Applications/Cursor.app/Contents/Resources/app/bin/code",
929+
editor: "cursor",
930+
args: ["--classic", "--goto", "/workspace with spaces/file.ts:12:4"],
931+
},
932+
{
933+
platform: "darwin",
934+
installPath: "Applications/Kiro.app/Contents/Resources/app/bin/code",
935+
editor: "kiro",
936+
args: ["--goto", "/workspace with spaces/file.ts:12:4"],
937+
},
938+
{
939+
platform: "darwin",
940+
installPath: "Applications/WebStorm.app/Contents/MacOS/webstorm",
941+
editor: "webstorm",
942+
args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
943+
},
944+
{
945+
platform: "darwin",
946+
installPath: "Applications/Zed.app/Contents/MacOS/cli",
947+
editor: "zed",
948+
args: ["/workspace with spaces/file.ts:12:4"],
949+
},
950+
{
951+
platform: "win32",
952+
installPath: "Programs/Cursor/resources/app/bin/cursor.cmd",
953+
editor: "cursor",
954+
args: ['^"--classic^"', '^"--goto^"', '^"/workspace^ with^ spaces/file.ts:12:4^"'],
955+
},
956+
{
957+
platform: "win32",
958+
installPath: "Programs/Microsoft VS Code/bin/code.cmd",
959+
editor: "vscode",
960+
args: ['^"--goto^"', '^"/workspace^ with^ spaces/file.ts:12:4^"'],
961+
},
962+
{
963+
platform: "win32",
964+
installPath: "Programs/JetBrains/WebStorm 2026.2/bin/webstorm64.exe",
965+
editor: "webstorm",
966+
args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
967+
},
968+
{
969+
platform: "win32",
970+
installPath: "Programs/WebStorm/bin/webstorm64.exe",
971+
editor: "webstorm",
972+
args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
973+
},
974+
{
975+
platform: "win32",
976+
installPath: "Programs/Zed/bin/zed.exe",
977+
editor: "zed",
978+
args: ["/workspace with spaces/file.ts:12:4"],
979+
},
980+
{
981+
platform: "linux",
982+
installPath: ".local/share/JetBrains/Toolbox/scripts/idea",
983+
editor: "idea",
984+
args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
985+
},
986+
{
987+
platform: "linux",
988+
installPath: ".local/bin/zed",
989+
editor: "zed",
990+
args: ["/workspace with spaces/file.ts:12:4"],
991+
},
992+
] as const) {
993+
it.effect.skipIf(windowsHost && platform !== "win32")(
994+
`discovers and launches ${editor} outside PATH on ${platform}`,
995+
() =>
996+
Effect.gen(function* () {
997+
const fs = yield* FileSystem.FileSystem;
998+
const path = yield* Path.Path;
999+
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor installs-" });
1000+
const executable = path.join(home, installPath);
1001+
yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
1002+
yield* fs.writeFileString(executable, "#!/bin/sh\n");
1003+
yield* fs.chmod(executable, 0o755);
1004+
let spawned: ChildProcess.StandardCommand | undefined;
1005+
yield* Effect.gen(function* () {
1006+
const launcher = yield* ExternalLauncher.ExternalLauncher;
1007+
assert.include(yield* launcher.resolveAvailableEditors(), editor);
1008+
yield* launcher.launchEditor({ editor, cwd: "/workspace with spaces/file.ts:12:4" });
1009+
}).pipe(
1010+
Effect.provide(
1011+
testLayer({
1012+
platform,
1013+
env: { HOME: home, LOCALAPPDATA: home, PATH: path.join(home, "empty") },
1014+
onSpawn: (command) => {
1015+
spawned = command;
1016+
},
1017+
}),
1018+
),
1019+
);
1020+
assert.ok(spawned);
1021+
assert.equal(
1022+
spawned.command,
1023+
executable.endsWith(".cmd") ? `^"${executable.replaceAll(" ", "^ ")}^"` : executable,
1024+
);
1025+
assert.deepEqual(spawned.args, args);
1026+
assert.equal(spawned.options.shell, executable.endsWith(".cmd"));
1027+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1028+
);
1029+
}
1030+
1031+
it.effect.skipIf(windowsHost)("ignores unusable app bundles and keeps PATH launchers first", () =>
1032+
Effect.gen(function* () {
1033+
const fs = yield* FileSystem.FileSystem;
1034+
const path = yield* Path.Path;
1035+
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-priority-" });
1036+
const executable = path.join(home, "Applications/Cursor.app/Contents/Resources/app/bin/code");
1037+
const env = { HOME: home, PATH: path.join(home, "bin") };
1038+
const discover = Effect.gen(function* () {
1039+
const launcher = yield* ExternalLauncher.ExternalLauncher;
1040+
return yield* launcher.resolveAvailableEditors();
1041+
});
1042+
const before = yield* discover.pipe(Effect.provide(testLayer({ platform: "darwin", env })));
1043+
yield* fs.makeDirectory(executable, { recursive: true });
1044+
assert.deepEqual(
1045+
yield* discover.pipe(Effect.provide(testLayer({ platform: "darwin", env }))),
1046+
before,
1047+
);
1048+
yield* fs.remove(executable, { recursive: true });
1049+
yield* fs.writeFileString(executable, "#!/bin/sh\n");
1050+
yield* fs.chmod(executable, 0o644);
1051+
assert.deepEqual(
1052+
yield* discover.pipe(Effect.provide(testLayer({ platform: "darwin", env }))),
1053+
before,
1054+
);
1055+
yield* fs.chmod(executable, 0o755);
1056+
yield* fs.makeDirectory(env.PATH);
1057+
yield* fs.writeFileString(path.join(env.PATH, "cursor"), "#!/bin/sh\n");
1058+
yield* fs.chmod(path.join(env.PATH, "cursor"), 0o755);
1059+
let spawned: ChildProcess.StandardCommand | undefined;
1060+
yield* Effect.gen(function* () {
1061+
const launcher = yield* ExternalLauncher.ExternalLauncher;
1062+
yield* launcher.launchEditor({ editor: "cursor", cwd: "/workspace" });
1063+
}).pipe(
1064+
Effect.provide(
1065+
testLayer({
1066+
platform: "darwin",
1067+
env: { ...env, PATH: `${env.PATH}:/usr/bin` },
1068+
onSpawn: (command) => {
1069+
spawned = command;
1070+
},
1071+
}),
1072+
),
1073+
);
1074+
assert.equal(spawned?.command, "cursor");
1075+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1076+
);
1077+
9251078
it.effect("memoizes editor discovery and refreshes after the cache window", () => {
9261079
let statCalls = 0;
9271080
const fileInfo = { type: "File" } as FileSystem.File.Info;

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

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
type FileManagerRevealKind,
1919
type LaunchEditorInput,
2020
} from "@t3tools/contracts";
21+
import { resolveEditorCommand } from "@t3tools/shared/editor";
2122
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
2223
import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell";
2324
import * as Clock from "effect/Clock";
@@ -109,6 +110,12 @@ const CommandLookupEnvConfig = Config.all({
109110
Path: Config.String("Path").pipe(Config.option),
110111
path: Config.String("path").pipe(Config.option),
111112
PATHEXT: Config.String("PATHEXT").pipe(Config.option),
113+
HOME: Config.String("HOME").pipe(Config.option),
114+
LOCALAPPDATA: Config.String("LOCALAPPDATA").pipe(Config.option),
115+
ProgramFiles: Config.String("ProgramFiles").pipe(Config.option),
116+
ProgramW6432: Config.String("ProgramW6432").pipe(Config.option),
117+
XDG_DATA_HOME: Config.String("XDG_DATA_HOME").pipe(Config.option),
118+
"ProgramFiles(x86)": Config.String("ProgramFiles(x86)").pipe(Config.option),
112119
}).pipe(Config.map(compactEnv));
113120

114121
const readBrowserLaunchEnv = BrowserLaunchEnvConfig.pipe(Effect.orElseSucceed(() => ({})));
@@ -154,26 +161,6 @@ function resolveCommandEditorArgs(
154161
}
155162
}
156163

157-
function resolveEditorArgs(
158-
editor: (typeof EDITORS)[number],
159-
target: string,
160-
): ReadonlyArray<string> {
161-
const baseArgs = "baseArgs" in editor ? editor.baseArgs : [];
162-
return [...baseArgs, ...resolveCommandEditorArgs(editor, target)];
163-
}
164-
165-
const resolveAvailableCommand = Effect.fn("externalLauncher.resolveAvailableCommand")(function* (
166-
commands: ReadonlyArray<string>,
167-
env: NodeJS.ProcessEnv,
168-
): Effect.fn.Return<Option.Option<string>, never, FileSystem.FileSystem | Path.Path> {
169-
for (const command of commands) {
170-
if (yield* isCommandAvailable(command, { env })) {
171-
return Option.some(command);
172-
}
173-
}
174-
return Option.none();
175-
});
176-
177164
function encodeUtf16LeBase64(input: string): string {
178165
const bytes = new Uint8Array(input.length * 2);
179166
for (let index = 0; index < input.length; index += 1) {
@@ -435,7 +422,7 @@ const buildAvailableEditors = Effect.fn("externalLauncher.buildAvailableEditors"
435422
continue;
436423
}
437424

438-
const command = yield* resolveAvailableCommand(editor.commands, env);
425+
const command = yield* resolveEditorCommand(editor, env);
439426
if (Option.isSome(command)) {
440427
available.push(editor.id);
441428
}
@@ -538,15 +525,18 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
538525
}
539526

540527
if (editorDef.commands) {
541-
const command = Option.getOrElse(
542-
yield* resolveAvailableCommand(editorDef.commands, env),
543-
() => editorDef.commands[0],
528+
const { command, baseArgs } = Option.getOrElse(
529+
yield* resolveEditorCommand(editorDef, env),
530+
() => ({
531+
command: editorDef.commands[0],
532+
baseArgs: "baseArgs" in editorDef ? editorDef.baseArgs : [],
533+
}),
544534
);
545535
return {
546536
editor: editorDef.id,
547537
target: input.cwd,
548538
command,
549-
args: resolveEditorArgs(editorDef, input.cwd),
539+
args: [...baseArgs, ...resolveCommandEditorArgs(editorDef, input.cwd)],
550540
};
551541
}
552542

‎packages/shared/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
"types": "./src/httpObservability.ts",
6464
"import": "./src/httpObservability.ts"
6565
},
66+
"./editor": {
67+
"types": "./src/editor.ts",
68+
"import": "./src/editor.ts"
69+
},
6670
"./shell": {
6771
"types": "./src/shell.ts",
6872
"import": "./src/shell.ts"

0 commit comments

Comments
 (0)