Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

externalLauncher.test.ts

754 lines27.7 KB

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