Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions apps/desktop/src/ipc/methods/window.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { assert, describe, it } from "@effect/vitest";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
Expand Down Expand Up @@ -26,6 +30,7 @@ import {
getWindowFullscreenState,
pasteAsText,
pickProjectFavicon,
probeRemoteEditors,
} from "./window.ts";

const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
Expand Down Expand Up @@ -262,3 +267,34 @@ describe("pickProjectFavicon", () => {
}),
);
});

it.effect.skipIf(HostProcessPlatform.defaultValue() === "win32")(
"finds remote editors installed without PATH launchers",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-remote-editors-" });
for (const app of ["Cursor", "Visual Studio Code", "WebStorm"]) {
const executable = path.join(
home,
"Applications",
`${app}.app`,
app === "WebStorm" ? "Contents/MacOS/webstorm" : "Contents/Resources/app/bin/code",
);
yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
yield* fs.writeFileString(executable, "#!/bin/sh\n");
yield* fs.chmod(executable, 0o755);
}
const editors = yield* probeRemoteEditors.handler(undefined).pipe(
Effect.provideService(HostProcessEnvironment, {
HOME: home,
PATH: path.join(home, "empty"),
}),
Effect.provideService(HostProcessPlatform, "darwin"),
);
assert.include(editors, "cursor");
assert.include(editors, "vscode");
assert.notInclude(editors, "webstorm");
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
18 changes: 6 additions & 12 deletions apps/desktop/src/ipc/methods/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
type PickedThemeFile,
} from "@t3tools/contracts";
import { WORKSPACE_IMAGE_PREVIEW_EXTENSIONS } from "@t3tools/shared/filePreview";
import { isCommandAvailable } from "@t3tools/shared/shell";
import { resolveEditorCommand } from "@t3tools/shared/editor";
import * as HostProcess from "@t3tools/shared/hostProcess";
import * as NodeOS from "node:os";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
Expand Down Expand Up @@ -333,20 +334,13 @@ export const probeRemoteEditors = DesktopIpc.makeIpcMethod({
channel: IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL,
payload: Schema.Undefined,
result: Schema.Array(EditorId),
// Probes THIS machine (where the renderer runs) for remote-capable editor
// CLIs, unlike the server's probe which walks the environment host's PATH.
// A Finder-launched app can miss PATH entries; an empty result makes the
// renderer fall back to VS Code only, so that fails soft.
handler: Effect.fn("desktop.ipc.window.probeRemoteEditors")(function* () {
const available: Array<EditorId> = [];
const env = yield* HostProcess.HostProcessEnvironment;
for (const editorId of REMOTE_CAPABLE_EDITOR_IDS) {
const commands = EDITORS.find((editor) => editor.id === editorId)?.commands;
if (!commands) continue;
for (const command of commands) {
if (yield* isCommandAvailable(command, { env: process.env })) {
available.push(editorId);
break;
}
const editor = EDITORS.find((editor) => editor.id === editorId);
if (editor && Option.isSome(yield* resolveEditorCommand(editor, env))) {
available.push(editorId);
}
}
return available;
Expand Down
153 changes: 153 additions & 0 deletions apps/server/src/process/externalLauncher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,159 @@ it.effect("discovers editors through the service API", () =>
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);

for (const { platform, installPath, editor, args } of [
{
platform: "darwin",
installPath: "Applications/Cursor.app/Contents/Resources/app/bin/code",
editor: "cursor",
args: ["--classic", "--goto", "/workspace with spaces/file.ts:12:4"],
},
{
platform: "darwin",
installPath: "Applications/Kiro.app/Contents/Resources/app/bin/code",
editor: "kiro",
args: ["--goto", "/workspace with spaces/file.ts:12:4"],
},
{
platform: "darwin",
installPath: "Applications/WebStorm.app/Contents/MacOS/webstorm",
editor: "webstorm",
args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
},
{
platform: "darwin",
installPath: "Applications/Zed.app/Contents/MacOS/cli",
editor: "zed",
args: ["/workspace with spaces/file.ts:12:4"],
},
{
platform: "win32",
installPath: "Programs/Cursor/resources/app/bin/cursor.cmd",
editor: "cursor",
args: ['^"--classic^"', '^"--goto^"', '^"/workspace^ with^ spaces/file.ts:12:4^"'],
},
{
platform: "win32",
installPath: "Programs/Microsoft VS Code/bin/code.cmd",
editor: "vscode",
args: ['^"--goto^"', '^"/workspace^ with^ spaces/file.ts:12:4^"'],
},
{
platform: "win32",
installPath: "Programs/JetBrains/WebStorm 2026.2/bin/webstorm64.exe",
editor: "webstorm",
args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
},
{
platform: "win32",
installPath: "Programs/WebStorm/bin/webstorm64.exe",
editor: "webstorm",
args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
},
{
platform: "win32",
installPath: "Programs/Zed/bin/zed.exe",
editor: "zed",
args: ["/workspace with spaces/file.ts:12:4"],
},
{
platform: "linux",
installPath: ".local/share/JetBrains/Toolbox/scripts/idea",
editor: "idea",
args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
},
{
platform: "linux",
installPath: ".local/bin/zed",
editor: "zed",
args: ["/workspace with spaces/file.ts:12:4"],
},
] as const) {
it.effect.skipIf(windowsHost && platform !== "win32")(
`discovers and launches ${editor} outside PATH on ${platform}`,
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor installs-" });
const executable = path.join(home, installPath);
yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
yield* fs.writeFileString(executable, "#!/bin/sh\n");
yield* fs.chmod(executable, 0o755);
let spawned: ChildProcess.StandardCommand | undefined;
yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
assert.include(yield* launcher.resolveAvailableEditors(), editor);
yield* launcher.launchEditor({ editor, cwd: "/workspace with spaces/file.ts:12:4" });
}).pipe(
Effect.provide(
testLayer({
platform,
env: { HOME: home, LOCALAPPDATA: home, PATH: path.join(home, "empty") },
onSpawn: (command) => {
spawned = command;
},
}),
),
);
assert.ok(spawned);
assert.equal(
spawned.command,
executable.endsWith(".cmd") ? `^"${executable.replaceAll(" ", "^ ")}^"` : executable,
);
assert.deepEqual(spawned.args, args);
assert.equal(spawned.options.shell, executable.endsWith(".cmd"));
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
}

it.effect.skipIf(windowsHost)("ignores unusable app bundles and keeps PATH launchers first", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-priority-" });
const executable = path.join(home, "Applications/Cursor.app/Contents/Resources/app/bin/code");
const env = { HOME: home, PATH: path.join(home, "bin") };
const discover = Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
return yield* launcher.resolveAvailableEditors();
});
const before = yield* discover.pipe(Effect.provide(testLayer({ platform: "darwin", env })));
yield* fs.makeDirectory(executable, { recursive: true });
assert.deepEqual(
yield* discover.pipe(Effect.provide(testLayer({ platform: "darwin", env }))),
before,
);
yield* fs.remove(executable, { recursive: true });
yield* fs.writeFileString(executable, "#!/bin/sh\n");
yield* fs.chmod(executable, 0o644);
assert.deepEqual(
yield* discover.pipe(Effect.provide(testLayer({ platform: "darwin", env }))),
before,
);
yield* fs.chmod(executable, 0o755);
yield* fs.makeDirectory(env.PATH);
yield* fs.writeFileString(path.join(env.PATH, "cursor"), "#!/bin/sh\n");
yield* fs.chmod(path.join(env.PATH, "cursor"), 0o755);
let spawned: ChildProcess.StandardCommand | undefined;
yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
yield* launcher.launchEditor({ editor: "cursor", cwd: "/workspace" });
}).pipe(
Effect.provide(
testLayer({
platform: "darwin",
env: { ...env, PATH: `${env.PATH}:/usr/bin` },
onSpawn: (command) => {
spawned = command;
},
}),
),
);
assert.equal(spawned?.command, "cursor");
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);

it.effect("memoizes editor discovery and refreshes after the cache window", () => {
let statCalls = 0;
const fileInfo = { type: "File" } as FileSystem.File.Info;
Expand Down
40 changes: 15 additions & 25 deletions apps/server/src/process/externalLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type FileManagerRevealKind,
type LaunchEditorInput,
} from "@t3tools/contracts";
import { resolveEditorCommand } from "@t3tools/shared/editor";
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell";
import * as Clock from "effect/Clock";
Expand Down Expand Up @@ -109,6 +110,12 @@ const CommandLookupEnvConfig = Config.all({
Path: Config.String("Path").pipe(Config.option),
path: Config.String("path").pipe(Config.option),
PATHEXT: Config.String("PATHEXT").pipe(Config.option),
HOME: Config.String("HOME").pipe(Config.option),
LOCALAPPDATA: Config.String("LOCALAPPDATA").pipe(Config.option),
ProgramFiles: Config.String("ProgramFiles").pipe(Config.option),
ProgramW6432: Config.String("ProgramW6432").pipe(Config.option),
XDG_DATA_HOME: Config.String("XDG_DATA_HOME").pipe(Config.option),
"ProgramFiles(x86)": Config.String("ProgramFiles(x86)").pipe(Config.option),
}).pipe(Config.map(compactEnv));

const readBrowserLaunchEnv = BrowserLaunchEnvConfig.pipe(Effect.orElseSucceed(() => ({})));
Expand Down Expand Up @@ -154,26 +161,6 @@ function resolveCommandEditorArgs(
}
}

function resolveEditorArgs(
editor: (typeof EDITORS)[number],
target: string,
): ReadonlyArray<string> {
const baseArgs = "baseArgs" in editor ? editor.baseArgs : [];
return [...baseArgs, ...resolveCommandEditorArgs(editor, target)];
}

const resolveAvailableCommand = Effect.fn("externalLauncher.resolveAvailableCommand")(function* (
commands: ReadonlyArray<string>,
env: NodeJS.ProcessEnv,
): Effect.fn.Return<Option.Option<string>, never, FileSystem.FileSystem | Path.Path> {
for (const command of commands) {
if (yield* isCommandAvailable(command, { env })) {
return Option.some(command);
}
}
return Option.none();
});

function encodeUtf16LeBase64(input: string): string {
const bytes = new Uint8Array(input.length * 2);
for (let index = 0; index < input.length; index += 1) {
Expand Down Expand Up @@ -435,7 +422,7 @@ const buildAvailableEditors = Effect.fn("externalLauncher.buildAvailableEditors"
continue;
}

const command = yield* resolveAvailableCommand(editor.commands, env);
const command = yield* resolveEditorCommand(editor, env);
if (Option.isSome(command)) {
available.push(editor.id);
}
Expand Down Expand Up @@ -538,15 +525,18 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
}

if (editorDef.commands) {
const command = Option.getOrElse(
yield* resolveAvailableCommand(editorDef.commands, env),
() => editorDef.commands[0],
const { command, baseArgs } = Option.getOrElse(
yield* resolveEditorCommand(editorDef, env),
() => ({
command: editorDef.commands[0],
baseArgs: "baseArgs" in editorDef ? editorDef.baseArgs : [],
}),
);
return {
editor: editorDef.id,
target: input.cwd,
command,
args: resolveEditorArgs(editorDef, input.cwd),
args: [...baseArgs, ...resolveCommandEditorArgs(editorDef, input.cwd)],
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
"types": "./src/httpObservability.ts",
"import": "./src/httpObservability.ts"
},
"./editor": {
"types": "./src/editor.ts",
"import": "./src/editor.ts"
},
"./shell": {
"types": "./src/shell.ts",
"import": "./src/shell.ts"
Expand Down
Loading
Loading