Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - the Windows launch smoke tests use filesystem notifications to observe real PowerShell launches.
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, it } from "@effect/vitest";
4import * as ConfigProvider from "effect/ConfigProvider";
5import * as Deferred from "effect/Deferred";
6import * as Cause from "effect/Cause";
7import * as Effect from "effect/Effect";
8import * as Exit from "effect/Exit";
9import * as Fiber from "effect/Fiber";
10import * as FileSystem from "effect/FileSystem";
11import * as Layer from "effect/Layer";
12import * as Path from "effect/Path";
13import * as PlatformError from "effect/PlatformError";
14import * as Sink from "effect/Sink";
15import * as Stream from "effect/Stream";
16import { ChildProcess, ChildProcessSpawner } from "effect/process";
17
18import * as HostProcess from "@t3tools/shared/HostProcess";
19import { SpawnExecutableResolution } from "@t3tools/shared/shell";
20import * as ExternalLauncher from "./externalLauncher.ts";
21
22// Tests below write `#!/bin/sh` stubs into a real temp dir and hand that
23// directory to a posix-mocked resolver as PATH. On a Windows host the temp
24// path carries a drive letter, so the posix `:` split shatters it; there is
25// no posix executable to find there anyway.
26const windowsHost = HostProcess.Platform.defaultValue() === "win32";
27
28interface MockSpawnResult {
29 readonly spawnError?: PlatformError.PlatformError;
30 readonly exitCode?: number;
31 readonly stdout?: string;
32 readonly stall?: boolean;
33}
34
35function makeMockDetachedHandle(input: MockSpawnResult & { readonly onUnref?: () => void } = {}) {
36 return ChildProcessSpawner.makeHandle({
37 pid: ChildProcessSpawner.ProcessId(1),
38 exitCode: input.stall
39 ? Effect.never
40 : Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
41 isRunning: Effect.succeed(true),
42 kill: () => Effect.void,
43 unref: Effect.sync(() => {
44 input.onUnref?.();
45 return Effect.void;
46 }),
47 stdin: Sink.drain,
48 stdout:
49 input.stdout === undefined
50 ? Stream.empty
51 : Stream.make(new TextEncoder().encode(input.stdout)),
52 stderr: Stream.empty,
53 all: Stream.empty,
54 getInputFd: () => Sink.drain,
55 getOutputFd: () => Stream.empty,
56 });
57}
58
59const layerTest = (input: {
60 readonly platform: NodeJS.Platform;
61 readonly env?: Record<string, string>;
62 readonly resolveExecutable?: (command: string) => string | undefined;
63 readonly onSpawn?: (command: ChildProcess.StandardCommand) => void;
64 readonly onUnref?: () => void;
65 readonly spawnResult?: (command: ChildProcess.StandardCommand) => MockSpawnResult | undefined;
66}) => {
67 const layerSpawner = Layer.succeed(
68 ChildProcessSpawner.ChildProcessSpawner,
69 ChildProcessSpawner.make((command) =>
70 Effect.gen(function* () {
71 assert.equal(ChildProcess.isStandardCommand(command), true);
72 if (!ChildProcess.isStandardCommand(command)) {
73 throw new Error("Expected a standard command");
74 }
75 input.onSpawn?.(command);
76 const result = input.spawnResult?.(command);
77 if (result?.spawnError) return yield* result.spawnError;
78 return makeMockDetachedHandle({
79 ...(input.onUnref === undefined ? {} : { onUnref: input.onUnref }),
80 ...result,
81 });
82 }),
83 ),
84 );
85
86 return Layer.mergeAll(
87 ExternalLauncher.layer.pipe(Layer.provide(Layer.merge(NodeServices.layer, layerSpawner))),
88 Layer.succeed(HostProcess.Platform, input.platform),
89 Layer.succeed(
90 SpawnExecutableResolution,
91 (command) => input.resolveExecutable?.(command) ?? command,
92 ),
93 ConfigProvider.layer(ConfigProvider.fromEnv({ env: input.env ?? {} })),
94 );
95};
96
97it.effect.skipIf(windowsHost)(
98 "launched browsers and editors do not inherit ELECTRON_RUN_AS_NODE",
99 () =>
100 Effect.gen(function* () {
101 const fileSystem = yield* FileSystem.FileSystem;
102 const path = yield* Path.Path;
103 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
104 const codePath = path.join(binDir, "code");
105 yield* fileSystem.writeFileString(codePath, "#!/bin/sh\n");
106 yield* fileSystem.chmod(codePath, 0o755);
107
108 const spawned: ChildProcess.StandardCommand[] = [];
109 yield* Effect.gen(function* () {
110 const launcher = yield* ExternalLauncher.ExternalLauncher;
111 yield* launcher.launchBrowser("https://example.com");
112 yield* launcher.launchEditor({ editor: "vscode", cwd: "/workspace" });
113 }).pipe(
114 Effect.provide(
115 layerTest({
116 platform: "linux",
117 env: { PATH: binDir, DISPLAY: ":0" },
118 onSpawn: (command) => spawned.push(command),
119 }),
120 ),
121 Effect.provideService(HostProcess.Environment, {
122 PATH: binDir,
123 DISPLAY: ":0",
124 ELECTRON_RUN_AS_NODE: "1",
125 }),
126 );
127
128 assert.deepEqual(
129 spawned.map((command) => ({
130 command: command.command,
131 env: command.options.env,
132 extendEnv: command.options.extendEnv,
133 })),
134 ["xdg-open", "code"].map((command) => ({
135 command,
136 env: { PATH: binDir, DISPLAY: ":0" },
137 extendEnv: false,
138 })),
139 );
140 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
141);
142
143it.effect("launches an installed editor with platform-safe arguments", () =>
144 Effect.gen(function* () {
145 const fileSystem = yield* FileSystem.FileSystem;
146 const path = yield* Path.Path;
147 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
148 yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n");
149
150 let spawned: ChildProcess.StandardCommand | undefined;
151 yield* Effect.gen(function* () {
152 const launcher = yield* ExternalLauncher.ExternalLauncher;
153 yield* launcher.launchEditor({
154 editor: "vscode",
155 cwd: "C:\\workspace with spaces\\src\\index.ts:12:4",
156 });
157 }).pipe(
158 Effect.provide(
159 layerTest({
160 platform: "win32",
161 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
162 resolveExecutable: (command) =>
163 command === "code" ? "C:\\Program Files\\Microsoft VS Code\\bin\\code.CMD" : command,
164 onSpawn: (command) => {
165 spawned = command;
166 },
167 }),
168 ),
169 );
170
171 assert.ok(spawned);
172 assert.equal(
173 spawned.command,
174 [
175 '^"C:\\Program^ Files\\Microsoft^ VS^ Code\\bin\\code.CMD^"',
176 '^"--goto^"',
177 '^"C:\\workspace^ with^ spaces\\src\\index.ts:12:4^"',
178 ].join(" "),
179 );
180 assert.deepEqual(spawned.args, []);
181 assert.equal(spawned.options.shell, true);
182 assert.equal(spawned.options.detached, true);
183 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
184);
185
186for (const {} of ["darwin", "linux"] as const) {
187}
188
189for (const {} of [
190 { kind: "browser", folderVerb: "none" },
191 { kind: "reveal", folderVerb: "none" },
192 { kind: "reveal", folderVerb: "" },
193 { kind: "reveal", folderVerb: "open" },
194] as const) {
195}
196
197it.effect.skipIf(windowsHost)(
198 "reveals a WSL file in Windows File Explorer through its UNC path",
199 () =>
200 Effect.gen(function* () {
201 const fileSystem = yield* FileSystem.FileSystem;
202 const path = yield* Path.Path;
203 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
204 for (const name of ["explorer.exe", "powershell.exe", "xdg-open"]) {
205 const filePath = path.join(binDir, name);
206 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
207 yield* fileSystem.chmod(filePath, 0o755);
208 }
209
210 let spawned: ChildProcess.StandardCommand | undefined;
211 const result = yield* Effect.gen(function* () {
212 const launcher = yield* ExternalLauncher.ExternalLauncher;
213 const kind = yield* launcher.resolveFileManagerRevealKind();
214 const editors = yield* launcher.resolveAvailableEditors();
215 yield* launcher.launchEditor({
216 editor: "file-manager",
217 cwd: "/home/t3/workspace [draft]/author's $media/clip|draft.mp4",
218 reveal: true,
219 });
220 return { kind, editors };
221 }).pipe(
222 Effect.provide(
223 layerTest({
224 platform: "linux",
225 env: {
226 PATH: binDir,
227 WSL_DISTRO_NAME: "Ubuntu-24.04",
228 WSL_INTEROP: "/run/WSL/1_interop",
229 },
230 onSpawn: (command) => {
231 spawned = command;
232 },
233 }),
234 ),
235 );
236
237 assert.equal(result.kind, "file-explorer");
238 assert.equal(result.editors.includes("file-manager"), true);
239 assert.ok(spawned);
240 assert.equal(spawned.command, "powershell.exe");
241 assert.equal(spawned.options.detached, true);
242 const encodedCommand = spawned.args[spawned.args.length - 1] ?? "";
243 const decodedCommand = Buffer.from(encodedCommand, "base64").toString("utf16le");
244 assert.equal(
245 decodedCommand,
246 "$ProgressPreference = 'SilentlyContinue'; $folderVerb = (Get-ItemProperty -LiteralPath 'Registry::HKEY_CLASSES_ROOT\\Directory\\shell' -ErrorAction SilentlyContinue).'(default)'; if ($folderVerb -and $folderVerb -ne 'none') { Invoke-Item -LiteralPath '\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\workspace [draft]\\author''s $media' } else { Start-Process 'explorer.exe' -ArgumentList ('/select,\"' + '\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\workspace [draft]\\author''s $media\\clip|draft.mp4' + '\"') }",
247 );
248 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
249);
250
251for (const wsl of [false, true]) {
252 it.effect.skipIf(windowsHost)(
253 "selects a file over D-Bus on " + (wsl ? "WSLg without interop PowerShell" : "Linux"),
254 () =>
255 Effect.gen(function* () {
256 const fileSystem = yield* FileSystem.FileSystem;
257 const path = yield* Path.Path;
258 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
259 for (const name of ["xdg-open", "xdg-mime", "gdbus", ...(wsl ? ["explorer.exe"] : [])]) {
260 const filePath = path.join(binDir, name);
261 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
262 yield* fileSystem.chmod(filePath, 0o755);
263 }
264 const target = "/workspace/media/a 'quoted' \"clip\" #100% ? é\\name.mp4";
265 const spawnedCommands: ChildProcess.StandardCommand[] = [];
266 yield* Effect.gen(function* () {
267 const launcher = yield* ExternalLauncher.ExternalLauncher;
268 yield* launcher.launchEditor({ editor: "file-manager", cwd: target, reveal: true });
269 }).pipe(
270 Effect.provide(
271 layerTest({
272 platform: "linux",
273 env: {
274 PATH: binDir,
275 WAYLAND_DISPLAY: "wayland-1",
276 ...(wsl
277 ? { WSL_DISTRO_NAME: "Ubuntu-24.04", WSL_INTEROP: "/run/WSL/1_interop" }
278 : {}),
279 },
280 onSpawn: (command) => spawnedCommands.push(command),
281 spawnResult: (command) =>
282 command.command === "xdg-mime"
283 ? { stdout: "com.thisisgm.flea.desktop\n" }
284 : undefined,
285 }),
286 ),
287 );
288
289 const reveal = spawnedCommands.find((command) => command.command === "gdbus");
290 assert.ok(reveal);
291 assert.deepEqual(reveal.args, [
292 "call",
293 "--session",
294 "--dest",
295 "org.freedesktop.FileManager1",
296 "--object-path",
297 "/org/freedesktop/FileManager1",
298 "--method",
299 "org.freedesktop.FileManager1.ShowItems",
300 "[\"file:///workspace/media/a%20'quoted'%20%22clip%22%20%23100%25%20%3F%20%C3%A9%5Cname.mp4\"]",
301 "",
302 ]);
303 assert.deepEqual(
304 spawnedCommands.filter((command) => command.command !== "xdg-mime"),
305 [reveal],
306 );
307 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
308 );
309}
310
311for (const {} of ["rejected", "spawn-error", "stalled"] as const) {
312}
313
314for (const {} of [
315 {
316 platform: "darwin",
317 installPath: "Applications/Antigravity IDE.app/Contents/Resources/app/bin/antigravity-ide",
318 editor: "antigravity",
319 args: ["--goto", "/workspace with spaces/file.ts:12:4"],
320 },
321 {
322 platform: "linux",
323 installPath: ".local/bin/antigravity-ide",
324 editor: "antigravity",
325 args: ["--goto", "/workspace with spaces/file.ts:12:4"],
326 },
327 {
328 platform: "darwin",
329 installPath: "Applications/Cursor.app/Contents/Resources/app/bin/code",
330 editor: "cursor",
331 args: ["--classic", "--goto", "/workspace with spaces/file.ts:12:4"],
332 },
333 {
334 platform: "darwin",
335 installPath: "Applications/Kiro.app/Contents/Resources/app/bin/code",
336 editor: "kiro",
337 args: ["--goto", "/workspace with spaces/file.ts:12:4"],
338 },
339 {
340 platform: "darwin",
341 installPath: "Applications/WebStorm.app/Contents/MacOS/webstorm",
342 editor: "webstorm",
343 args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
344 },
345 {
346 platform: "darwin",
347 installPath: "Applications/Zed.app/Contents/MacOS/cli",
348 editor: "zed",
349 args: ["/workspace with spaces/file.ts:12:4"],
350 },
351 {
352 platform: "win32",
353 installPath: "Programs/Cursor/resources/app/bin/cursor.cmd",
354 editor: "cursor",
355 args: ['^"--classic^"', '^"--goto^"', '^"/workspace^ with^ spaces/file.ts:12:4^"'],
356 },
357 {
358 platform: "win32",
359 installPath: "Programs/Microsoft VS Code/bin/code.cmd",
360 editor: "vscode",
361 args: ['^"--goto^"', '^"/workspace^ with^ spaces/file.ts:12:4^"'],
362 },
363 {
364 platform: "win32",
365 installPath: "Programs/JetBrains/WebStorm 2026.2/bin/webstorm64.exe",
366 editor: "webstorm",
367 args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
368 },
369 {
370 platform: "win32",
371 installPath: "Programs/WebStorm/bin/webstorm64.exe",
372 editor: "webstorm",
373 args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
374 },
375 {
376 platform: "win32",
377 installPath: "Programs/Zed/bin/zed.exe",
378 editor: "zed",
379 args: ["/workspace with spaces/file.ts:12:4"],
380 },
381 {
382 platform: "linux",
383 installPath: ".local/share/JetBrains/Toolbox/scripts/idea",
384 editor: "idea",
385 args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
386 },
387 {
388 platform: "linux",
389 installPath: ".local/bin/zed",
390 editor: "zed",
391 args: ["/workspace with spaces/file.ts:12:4"],
392 },
393] as const) {
394}
395
396for (const cwd of ["/workspace with spaces/", "/workspace with spaces/file.ts:12:4"]) {
397 it.effect.skipIf(windowsHost)(
398 `opens ${cwd} with the macOS Zed bundle when another zed is on PATH`,
399 () =>
400 Effect.gen(function* () {
401 const fs = yield* FileSystem.FileSystem;
402 const path = yield* Path.Path;
403 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-zed-collision-" });
404 const executable = path.join(home, "Applications/Zed.app/Contents/MacOS/cli");
405 const binDir = path.join(home, "zfs/bin");
406 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
407 yield* fs.makeDirectory(binDir, { recursive: true });
408 for (const command of [executable, path.join(binDir, "zed")]) {
409 yield* fs.writeFileString(command, "#!/bin/sh\n");
410 yield* fs.chmod(command, 0o755);
411 }
412
413 let spawned: ChildProcess.StandardCommand | undefined;
414 yield* Effect.gen(function* () {
415 const launcher = yield* ExternalLauncher.ExternalLauncher;
416 assert.include(yield* launcher.resolveAvailableEditors(), "zed");
417 yield* launcher.launchEditor({ editor: "zed", cwd });
418 }).pipe(
419 Effect.provide(
420 layerTest({
421 platform: "darwin",
422 env: { HOME: home, PATH: binDir },
423 onSpawn: (command) => {
424 spawned = command;
425 },
426 }),
427 ),
428 );
429 assert.ok(spawned);
430 assert.equal(spawned.command, executable);
431 assert.deepEqual(spawned.args, [cwd]);
432 assert.equal(spawned.options.shell, false);
433 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
434 );
435}
436
437it.effect.each([
438 { editor: "webstorm", installPath: "Applications/WebStorm 2026.2.app/Contents/MacOS/webstorm" },
439 { editor: "idea", installPath: "Applications/IntelliJ IDEA 2026.1.4.app/Contents/MacOS/idea" },
440] as const)(
441 "discovers and launches $editor from a versioned macOS bundle",
442 ({ editor, installPath }) =>
443 Effect.gen(function* () {
444 const fs = yield* FileSystem.FileSystem;
445 const path = yield* Path.Path;
446 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-versioned-" });
447 const executable = path.join(home, installPath);
448 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
449 yield* fs.writeFileString(executable, "#!/bin/sh\n");
450 yield* fs.chmod(executable, 0o755);
451
452 let spawned: ChildProcess.StandardCommand | undefined;
453 yield* Effect.gen(function* () {
454 const launcher = yield* ExternalLauncher.ExternalLauncher;
455 assert.include(yield* launcher.resolveAvailableEditors(), editor);
456 yield* launcher.launchEditor({ editor, cwd: "/workspace with spaces/file.ts:12:4" });
457 }).pipe(
458 Effect.provide(
459 layerTest({
460 platform: "darwin",
461 env: { HOME: home, PATH: path.join(home, "empty") },
462 onSpawn: (command) => {
463 spawned = command;
464 },
465 }),
466 ),
467 );
468
469 assert.ok(spawned);
470 assert.equal(spawned.command, executable);
471 assert.deepEqual(spawned.args, [
472 "--line",
473 "12",
474 "--column",
475 "4",
476 "/workspace with spaces/file.ts",
477 ]);
478 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
479);
480
481it.effect("matches macOS bundles on the name boundary only", () =>
482 Effect.gen(function* () {
483 const fs = yield* FileSystem.FileSystem;
484 const path = yield* Path.Path;
485 const unrelatedHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-unrelated-" });
486 const unrelated = path.join(
487 unrelatedHome,
488 "Applications/WebStormHelper.app/Contents/MacOS/webstorm",
489 );
490 yield* fs.makeDirectory(path.dirname(unrelated), { recursive: true });
491 yield* fs.writeFileString(unrelated, "#!/bin/sh\n");
492 yield* fs.chmod(unrelated, 0o755);
493
494 const editorsFor = (home: string) =>
495 Effect.gen(function* () {
496 const launcher = yield* ExternalLauncher.ExternalLauncher;
497 return yield* launcher.resolveAvailableEditors();
498 }).pipe(
499 Effect.provide(
500 layerTest({ platform: "darwin", env: { HOME: home, PATH: path.join(home, "bin") } }),
501 ),
502 );
503
504 assert.notInclude(yield* editorsFor(unrelatedHome), "webstorm");
505
506 const exactHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-exact-" });
507 const exact = path.join(exactHome, "Applications/WebStorm.app/Contents/MacOS/webstorm");
508 yield* fs.makeDirectory(path.dirname(exact), { recursive: true });
509 yield* fs.writeFileString(exact, "#!/bin/sh\n");
510 yield* fs.chmod(exact, 0o755);
511
512 assert.include(yield* editorsFor(exactHome), "webstorm");
513 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
514);
515
516it.effect("keeps the macOS VS Code Insiders bundle out of stable VS Code", () =>
517 Effect.gen(function* () {
518 const fs = yield* FileSystem.FileSystem;
519 const path = yield* Path.Path;
520 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-insiders-" });
521 // The real Insiders bundle ships both `code-insiders` and `code`.
522 const bin = path.join(
523 home,
524 "Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin",
525 );
526 yield* fs.makeDirectory(bin, { recursive: true });
527 for (const name of ["code", "code-insiders"]) {
528 yield* fs.writeFileString(path.join(bin, name), "#!/bin/sh\n");
529 yield* fs.chmod(path.join(bin, name), 0o755);
530 }
531
532 let spawned: ChildProcess.StandardCommand | undefined;
533 const editors = yield* Effect.gen(function* () {
534 const launcher = yield* ExternalLauncher.ExternalLauncher;
535 const available = yield* launcher.resolveAvailableEditors();
536 yield* launcher.launchEditor({ editor: "vscode-insiders", cwd: "/workspace/file.ts" });
537 return available;
538 }).pipe(
539 Effect.provide(
540 layerTest({
541 platform: "darwin",
542 env: { HOME: home, PATH: path.join(home, "empty") },
543 onSpawn: (command) => {
544 spawned = command;
545 },
546 }),
547 ),
548 );
549
550 assert.notInclude(editors, "vscode");
551 assert.include(editors, "vscode-insiders");
552 assert.equal(spawned?.command, path.join(bin, "code-insiders"));
553 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
554);
555
556for (const { platform, installPath, onPath } of [
557 { platform: "darwin", installPath: ".local/bin/agy", onPath: true },
558 { platform: "linux", installPath: ".local/bin/agy", onPath: false },
559 { platform: "win32", installPath: "agy/bin/agy.cmd", onPath: true },
560] as const) {
561 it.effect.skipIf(windowsHost && platform !== "win32")(
562 `does not report the agy CLI as the Antigravity IDE on ${platform}`,
563 () =>
564 Effect.gen(function* () {
565 const fs = yield* FileSystem.FileSystem;
566 const path = yield* Path.Path;
567 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-cli-" });
568 const executable = path.join(home, installPath);
569 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
570 yield* fs.writeFileString(executable, "#!/bin/sh\n");
571 yield* fs.chmod(executable, 0o755);
572 const editors = yield* Effect.gen(function* () {
573 const launcher = yield* ExternalLauncher.ExternalLauncher;
574 return yield* launcher.resolveAvailableEditors();
575 }).pipe(
576 Effect.provide(
577 layerTest({
578 platform,
579 env: {
580 HOME: home,
581 LOCALAPPDATA: home,
582 PATH: onPath ? path.dirname(executable) : path.join(home, "empty"),
583 PATHEXT: ".COM;.EXE;.BAT;.CMD",
584 },
585 }),
586 ),
587 );
588 assert.notInclude(editors, "antigravity");
589 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
590 );
591}
592
593it.effect.skipIf(windowsHost)("ignores unusable app bundles and keeps PATH launchers first", () =>
594 Effect.gen(function* () {
595 const fs = yield* FileSystem.FileSystem;
596 const path = yield* Path.Path;
597 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-priority-" });
598 const executable = path.join(home, "Applications/Cursor.app/Contents/Resources/app/bin/code");
599 const env = { HOME: home, PATH: path.join(home, "bin") };
600 const discover = Effect.gen(function* () {
601 const launcher = yield* ExternalLauncher.ExternalLauncher;
602 return yield* launcher.resolveAvailableEditors();
603 });
604 const before = yield* discover.pipe(Effect.provide(layerTest({ platform: "darwin", env })));
605 yield* fs.makeDirectory(executable, { recursive: true });
606 assert.deepEqual(
607 yield* discover.pipe(Effect.provide(layerTest({ platform: "darwin", env }))),
608 before,
609 );
610 yield* fs.remove(executable, { recursive: true });
611 yield* fs.writeFileString(executable, "#!/bin/sh\n");
612 yield* fs.chmod(executable, 0o644);
613 assert.deepEqual(
614 yield* discover.pipe(Effect.provide(layerTest({ platform: "darwin", env }))),
615 before,
616 );
617 yield* fs.chmod(executable, 0o755);
618 yield* fs.makeDirectory(env.PATH);
619 yield* fs.writeFileString(path.join(env.PATH, "cursor"), "#!/bin/sh\n");
620 yield* fs.chmod(path.join(env.PATH, "cursor"), 0o755);
621 let spawned: ChildProcess.StandardCommand | undefined;
622 yield* Effect.gen(function* () {
623 const launcher = yield* ExternalLauncher.ExternalLauncher;
624 yield* launcher.launchEditor({ editor: "cursor", cwd: "/workspace" });
625 }).pipe(
626 Effect.provide(
627 layerTest({
628 platform: "darwin",
629 env: { ...env, PATH: `${env.PATH}:/usr/bin` },
630 onSpawn: (command) => {
631 spawned = command;
632 },
633 }),
634 ),
635 );
636 assert.equal(spawned?.command, "cursor");
637 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
638);
639
640it.effect("keeps scanning after the caller is interrupted and shares that scan", () => {
641 const fileInfo = { type: "File" } as FileSystem.File.Info;
642 const release = Deferred.makeUnsafe<void>();
643 let parkedStats = 0;
644 const layerLauncher = ExternalLauncher.layer.pipe(
645 Layer.provide(
646 Layer.mergeAll(
647 FileSystem.layerNoop({
648 stat: () =>
649 Effect.gen(function* () {
650 if (!Deferred.isDoneUnsafe(release)) {
651 parkedStats += 1;
652 yield* Deferred.await(release);
653 }
654 return fileInfo;
655 }),
656 }),
657 Path.layer,
658 Layer.succeed(
659 ChildProcessSpawner.ChildProcessSpawner,
660 ChildProcessSpawner.make(() => Effect.sync(() => makeMockDetachedHandle())),
661 ),
662 ),
663 ),
664 );
665
666 return Effect.gen(function* () {
667 const launcher = yield* ExternalLauncher.ExternalLauncher;
668
669 const interrupted = yield* Effect.forkChild(launcher.resolveAvailableEditors());
670 yield* Effect.yieldNow;
671 yield* Fiber.interrupt(interrupted);
672
673 const next = yield* Effect.forkChild(launcher.resolveAvailableEditors());
674 yield* Effect.yieldNow;
675 assert.equal(parkedStats, 1);
676
677 yield* Deferred.succeed(release, undefined);
678 const editors = yield* Fiber.join(next);
679 assert.equal(editors.includes("vscode"), true);
680 }).pipe(
681 Effect.provide(
682 Layer.mergeAll(
683 layerLauncher,
684 Layer.succeed(HostProcess.Platform, "win32"),
685 ConfigProvider.layer(
686 ConfigProvider.fromEnv({
687 env: {
688 PATH: "C:\\t3-editor-discovery-interrupt-test",
689 PATHEXT: ".COM;.EXE;.BAT;.CMD",
690 },
691 }),
692 ),
693 ),
694 ),
695 );
696});
697
698const launchWindowsShimEditor = (cwd: string) =>
699 Effect.gen(function* () {
700 const fileSystem = yield* FileSystem.FileSystem;
701 const path = yield* Path.Path;
702 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
703 yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n");
704 let spawned: ChildProcess.StandardCommand | undefined;
705 const exit = yield* Effect.gen(function* () {
706 const launcher = yield* ExternalLauncher.ExternalLauncher;
707 return yield* launcher.launchEditor({ editor: "vscode", cwd }).pipe(Effect.exit);
708 }).pipe(
709 Effect.provide(
710 layerTest({
711 platform: "win32",
712 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
713 resolveExecutable: (command) =>
714 command === "code" ? "C:\\Program Files\\Microsoft VS Code\\bin\\code.CMD" : command,
715 onSpawn: (command) => {
716 spawned = command;
717 },
718 }),
719 ),
720 );
721 return { exit, spawned };
722 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
723
724it.effect.each([
725 ["CRLF", "C:\\workspace\\file.ts\r\ncalc.exe"],
726 ["LF", "C:\\workspace\\file.ts\ncalc.exe"],
727 ["double quote", 'C:\\workspace\\file.ts" & calc.exe & "'],
728] as const)("does not start a Windows command shim for a path with a $0", ([, cwd]) =>
729 Effect.gen(function* () {
730 const { exit, spawned } = yield* launchWindowsShimEditor(cwd);
731 assert.equal(spawned, undefined);
732 assert.isTrue(Exit.isFailure(exit));
733 if (Exit.isFailure(exit)) {
734 assert.instanceOf(
735 Cause.squash(exit.cause),
736 ExternalLauncher.ExternalLauncherUnsupportedTargetError,
737 );
738 }
739 }),
740);
741
742it.effect("starts a Windows command shim for unicode paths with cmd metacharacters", () =>
743 Effect.gen(function* () {
744 const { exit, spawned } = yield* launchWindowsShimEditor(
745 "C:\\Users\\jö\\R&D (100%)\\naïve ^file!.ts:3:7",
746 );
747 assert.isTrue(Exit.isSuccess(exit));
748 assert.ok(spawned);
749 assert.equal(spawned.options.shell, true);
750 assert.deepEqual(spawned.args, []);
751 assert.isTrue(
752 spawned.command.endsWith(
753 ' ^"--goto^" ^"C:\\Users\\jö\\R^&D^ ^(100^%^)\\naïve^ ^^file^!.ts:3:7^"',
754 ),
755 );
756 }),
757);