fix: detect installed editors outside PATH (#12439)
Co-authored-by: shivam <[email protected]>
This commit is contained in:
1 parent
7a326c244f
commit
408ff8ae9b
6 files changed
+341
-37
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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";
|
||||
@@ -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(() => ({})));
|
||||
@@ -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) {
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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)],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user