Skip to content
Open
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
40 changes: 37 additions & 3 deletions apps/server/src/open.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { FileSystem, Path, Effect } from "effect";

import {
isCommandAvailable,
isAppInstalled,
launchDetached,
resolveAvailableEditors,
resolveEditorLaunch,
} from "./open";
import {EDITORS} from "@t3tools/contracts";

it.layer(NodeServices.layer)("resolveEditorLaunch", (it) => {
it.effect("returns commands for command-based editors", () =>
Expand Down Expand Up @@ -82,7 +84,8 @@ it.layer(NodeServices.layer)("resolveEditorLaunch", (it) => {

const ideaLaunch = yield* resolveEditorLaunch(
{ cwd: "/tmp/workspace", editor: "idea" },
"darwin",
"linux",
{ PATH: "" },
);
assert.deepEqual(ideaLaunch, {
command: "idea",
Expand Down Expand Up @@ -172,7 +175,8 @@ it.layer(NodeServices.layer)("resolveEditorLaunch", (it) => {

const ideaLineOnly = yield* resolveEditorLaunch(
{ cwd: "/tmp/workspace/AGENTS.md:48", editor: "idea" },
"darwin",
"linux",
{ PATH: "" },
);
assert.deepEqual(ideaLineOnly, {
command: "idea",
Expand All @@ -181,7 +185,8 @@ it.layer(NodeServices.layer)("resolveEditorLaunch", (it) => {

const ideaLineAndColumn = yield* resolveEditorLaunch(
{ cwd: "/tmp/workspace/src/open.ts:71:5", editor: "idea" },
"darwin",
"linux",
{ PATH: "" },
);
assert.deepEqual(ideaLineAndColumn, {
command: "idea",
Expand Down Expand Up @@ -221,6 +226,25 @@ it.layer(NodeServices.layer)("resolveEditorLaunch", (it) => {
}),
);

it.effect("falls back to open -a on macOS when CLI is missing but .app is installed", () =>
Effect.gen(function* () {
const idea = EDITORS.find((e) => e.id === "idea");
assert.isDefined(idea);

if (!isAppInstalled(idea, "darwin")) return;

const launch = yield* resolveEditorLaunch(
{ cwd: "/tmp/workspace", editor: "idea" },
"darwin",
{ PATH: "" },
);
assert.deepEqual(launch, {
command: "open",
args: ["-a", "IntelliJ IDEA", "--args", "/tmp/workspace"],
});
}),
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New tests silently pass without any assertions

Low Severity

Both new tests guard all their assertions behind if (!isAppInstalled(idea, "darwin")) return;, which checks the real filesystem for /Applications/IntelliJ IDEA.app. On any non-macOS machine or any macOS machine without IntelliJ installed (i.e., virtually every CI runner), the tests early-return and pass with zero assertions executed. This means the open -a fallback code path has no regression protection in CI. Since isAppInstalled uses raw statSync rather than the Effect FileSystem service, the check can't be mocked via layers either.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0a4a35d. Configure here.


it.effect("maps file-manager editor to OS open commands", () =>
Effect.gen(function* () {
const launch1 = yield* resolveEditorLaunch(
Expand Down Expand Up @@ -383,6 +407,16 @@ it.layer(NodeServices.layer)("resolveAvailableEditors", (it) => {
}),
);

it("includes editors detected via macOS .app bundle", () => {
const idea = EDITORS.find((e) => e.id === "idea");
assert.isDefined(idea);

if (!isAppInstalled(idea, "darwin")) return;

const editors = resolveAvailableEditors("darwin", { PATH: "" });
assert.isTrue(editors.includes("idea"));
});

it("omits file-manager when the platform opener is unavailable", () => {
const editors = resolveAvailableEditors("linux", {
PATH: "",
Expand Down
57 changes: 46 additions & 11 deletions apps/server/src/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { spawn } from "node:child_process";
import { accessSync, constants, statSync } from "node:fs";
import { extname, join } from "node:path";

import { EDITORS, OpenError, type EditorId } from "@t3tools/contracts";
import { EDITORS, OpenError, type EditorId, EditorDefinition } from "@t3tools/contracts";
import { ServiceMap, Effect, Layer } from "effect";

// ==============================
Expand Down Expand Up @@ -53,10 +53,7 @@ function parseTargetPathAndPosition(target: string): {
};
}

function resolveCommandEditorArgs(
editor: (typeof EDITORS)[number],
target: string,
): ReadonlyArray<string> {
function resolveCommandEditorArgs(editor: EditorDefinition, target: string): ReadonlyArray<string> {
const parsedTarget = parseTargetPathAndPosition(target);

switch (editor.launchStyle) {
Expand Down Expand Up @@ -203,6 +200,31 @@ export function isCommandAvailable(
return false;
}

function resolveAppPaths(appName: string, platform: NodeJS.Platform): ReadonlyArray<string> {
switch (platform) {
case "darwin":
return [`/Applications/${appName}.app`];
default:
return [];
}
}

export function isAppInstalled(
editor: EditorDefinition,
platform: NodeJS.Platform,
): editor is EditorDefinition & { appName: string } {
if (!("appName" in editor)) return false;
for (const appPath of resolveAppPaths(editor.appName, platform)) {
try {
statSync(appPath);
return true;
} catch {
// not found at this path
}
}
return false;
}

export function resolveAvailableEditors(
platform: NodeJS.Platform = process.platform,
env: NodeJS.ProcessEnv = process.env,
Expand All @@ -221,6 +243,8 @@ export function resolveAvailableEditors(
const command = resolveAvailableCommand(editor.commands, { platform, env });
if (command !== null) {
available.push(editor.id);
} else if (isAppInstalled(editor, platform)) {
available.push(editor.id);
}
}

Expand Down Expand Up @@ -269,12 +293,23 @@ export const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
}

if (editorDef.commands) {
const command =
resolveAvailableCommand(editorDef.commands, { platform, env }) ?? editorDef.commands[0];
return {
command,
args: resolveCommandEditorArgs(editorDef, input.cwd),
};
const command = resolveAvailableCommand(editorDef.commands, { platform, env });
const args = resolveCommandEditorArgs(editorDef, input.cwd);
if (command) {
return { command, args };
}

if (isAppInstalled(editorDef, platform)) {
switch (platform) {
case "darwin":
return {
command: "open",
args: ["-a", editorDef.appName, "--args", ...args],
};
}
}

return { command: editorDef.commands[0], args };
}

if (editorDef.id !== "file-manager") {
Expand Down
11 changes: 9 additions & 2 deletions packages/contracts/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { TrimmedNonEmptyString } from "./baseSchemas";
export const EditorLaunchStyle = Schema.Literals(["direct-path", "goto", "line-column"]);
export type EditorLaunchStyle = typeof EditorLaunchStyle.Type;

type EditorDefinition = {
export type EditorDefinition = {
readonly id: string;
readonly label: string;
readonly commands: readonly [string, ...string[]] | null;
readonly launchStyle: EditorLaunchStyle;
readonly appName?: string;
};

export const EDITORS = [
Expand All @@ -24,7 +25,13 @@ export const EDITORS = [
{ id: "vscodium", label: "VSCodium", commands: ["codium"], launchStyle: "goto" },
{ id: "zed", label: "Zed", commands: ["zed", "zeditor"], launchStyle: "direct-path" },
{ id: "antigravity", label: "Antigravity", commands: ["agy"], launchStyle: "goto" },
{ id: "idea", label: "IntelliJ IDEA", commands: ["idea"], launchStyle: "line-column" },
{
id: "idea",
label: "IntelliJ IDEA",
commands: ["idea"],
launchStyle: "line-column",
appName: "IntelliJ IDEA",
},
{ id: "file-manager", label: "File Manager", commands: null, launchStyle: "direct-path" },
] as const satisfies ReadonlyArray<EditorDefinition>;

Expand Down
Loading