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

externalLauncher.test.ts

658 lines25.0 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​
183it.effect.skipIf(windowsHost)(
184 "reveals a WSL file in Windows File Explorer through its UNC path",
185 () =>
186 Effect.gen(function* () {
187 const fileSystem = yield* FileSystem.FileSystem;
188 const path = yield* Path.Path;
189 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
190 for (const name of ["explorer.exe", "powershell.exe", "xdg-open"]) {
191 const filePath = path.join(binDir, name);
192 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
193 yield* fileSystem.chmod(filePath, 0o755);
194 }
195​
196 let spawned: ChildProcess.StandardCommand | undefined;
197 const result = yield* Effect.gen(function* () {
198 const launcher = yield* ExternalLauncher.ExternalLauncher;
199 const kind = yield* launcher.resolveFileManagerRevealKind();
200 const editors = yield* launcher.resolveAvailableEditors();
201 yield* launcher.launchEditor({
202 editor: "file-manager",
203 cwd: "/home/t3/workspace [draft]/author's $media/clip|draft.mp4",
204 reveal: true,
205 });
206 return { kind, editors };
207 }).pipe(
208 Effect.provide(
209 layerTest({
210 platform: "linux",
211 env: {
212 PATH: binDir,
213 WSL_DISTRO_NAME: "Ubuntu-24.04",
214 WSL_INTEROP: "/run/WSL/1_interop",
215 },
216 onSpawn: (command) => {
217 spawned = command;
218 },
219 }),
220 ),
221 );
222​
223 assert.equal(result.kind, "file-explorer");
224 assert.equal(result.editors.includes("file-manager"), true);
225 assert.ok(spawned);
226 assert.equal(spawned.command, "powershell.exe");
227 assert.equal(spawned.options.detached, true);
228 const encodedCommand = spawned.args[spawned.args.length - 1] ?? "";
229 const decodedCommand = Buffer.from(encodedCommand, "base64").toString("utf16le");
230 assert.equal(
231 decodedCommand,
232 "$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' + '\"') }",
233 );
234 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
235);
236​
237for (const wsl of [false, true]) {
238 it.effect.skipIf(windowsHost)(
239 "selects a file over D-Bus on " + (wsl ? "WSLg without interop PowerShell" : "Linux"),
240 () =>
241 Effect.gen(function* () {
242 const fileSystem = yield* FileSystem.FileSystem;
243 const path = yield* Path.Path;
244 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
245 for (const name of ["xdg-open", "xdg-mime", "gdbus", ...(wsl ? ["explorer.exe"] : [])]) {
246 const filePath = path.join(binDir, name);
247 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
248 yield* fileSystem.chmod(filePath, 0o755);
249 }
250 const target = "/workspace/media/a 'quoted' \"clip\" #100% ? é\\name.mp4";
251 const spawnedCommands: ChildProcess.StandardCommand[] = [];
252 yield* Effect.gen(function* () {
253 const launcher = yield* ExternalLauncher.ExternalLauncher;
254 yield* launcher.launchEditor({ editor: "file-manager", cwd: target, reveal: true });
255 }).pipe(
256 Effect.provide(
257 layerTest({
258 platform: "linux",
259 env: {
260 PATH: binDir,
261 WAYLAND_DISPLAY: "wayland-1",
262 ...(wsl
263 ? { WSL_DISTRO_NAME: "Ubuntu-24.04", WSL_INTEROP: "/run/WSL/1_interop" }
264 : {}),
265 },
266 onSpawn: (command) => spawnedCommands.push(command),
267 spawnResult: (command) =>
268 command.command === "xdg-mime"
269 ? { stdout: "com.thisisgm.flea.desktop\n" }
270 : undefined,
271 }),
272 ),
273 );
274​
275 const reveal = spawnedCommands.find((command) => command.command === "gdbus");
276 assert.ok(reveal);
277 assert.deepEqual(reveal.args, [
278 "call",
279 "--session",
280 "--dest",
281 "org.freedesktop.FileManager1",
282 "--object-path",
283 "/org/freedesktop/FileManager1",
284 "--method",
285 "org.freedesktop.FileManager1.ShowItems",
286 "[\"file:///workspace/media/a%20'quoted'%20%22clip%22%20%23100%25%20%3F%20%C3%A9%5Cname.mp4\"]",
287 "",
288 ]);
289 assert.deepEqual(
290 spawnedCommands.filter((command) => command.command !== "xdg-mime"),
291 [reveal],
292 );
293 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
294 );
296​
297for (const cwd of ["/workspace with spaces/", "/workspace with spaces/file.ts:12:4"]) {
298 it.effect.skipIf(windowsHost)(
299 `opens ${cwd} with the macOS Zed bundle when another zed is on PATH`,
300 () =>
301 Effect.gen(function* () {
302 const fs = yield* FileSystem.FileSystem;
303 const path = yield* Path.Path;
304 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-zed-collision-" });
305 const executable = path.join(home, "Applications/Zed.app/Contents/MacOS/cli");
306 const binDir = path.join(home, "zfs/bin");
307 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
308 yield* fs.makeDirectory(binDir, { recursive: true });
309 for (const command of [executable, path.join(binDir, "zed")]) {
310 yield* fs.writeFileString(command, "#!/bin/sh\n");
311 yield* fs.chmod(command, 0o755);
312 }
313​
314 let spawned: ChildProcess.StandardCommand | undefined;
315 yield* Effect.gen(function* () {
316 const launcher = yield* ExternalLauncher.ExternalLauncher;
317 assert.include(yield* launcher.resolveAvailableEditors(), "zed");
318 yield* launcher.launchEditor({ editor: "zed", cwd });
319 }).pipe(
320 Effect.provide(
321 layerTest({
322 platform: "darwin",
323 env: { HOME: home, PATH: binDir },
324 onSpawn: (command) => {
325 spawned = command;
326 },
327 }),
328 ),
329 );
330 assert.ok(spawned);
331 assert.equal(spawned.command, executable);
332 assert.deepEqual(spawned.args, [cwd]);
333 assert.equal(spawned.options.shell, false);
334 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
335 );
337​
338it.effect.each([
339 { editor: "webstorm", installPath: "Applications/WebStorm 2026.2.app/Contents/MacOS/webstorm" },
340 { editor: "idea", installPath: "Applications/IntelliJ IDEA 2026.1.4.app/Contents/MacOS/idea" },
341] as const)(
342 "discovers and launches $editor from a versioned macOS bundle",
343 ({ editor, installPath }) =>
344 Effect.gen(function* () {
345 const fs = yield* FileSystem.FileSystem;
346 const path = yield* Path.Path;
347 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-versioned-" });
348 const executable = path.join(home, installPath);
349 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
350 yield* fs.writeFileString(executable, "#!/bin/sh\n");
351 yield* fs.chmod(executable, 0o755);
352​
353 let spawned: ChildProcess.StandardCommand | undefined;
354 yield* Effect.gen(function* () {
355 const launcher = yield* ExternalLauncher.ExternalLauncher;
356 assert.include(yield* launcher.resolveAvailableEditors(), editor);
357 yield* launcher.launchEditor({ editor, cwd: "/workspace with spaces/file.ts:12:4" });
358 }).pipe(
359 Effect.provide(
360 layerTest({
361 platform: "darwin",
362 env: { HOME: home, PATH: path.join(home, "empty") },
363 onSpawn: (command) => {
364 spawned = command;
365 },
366 }),
367 ),
368 );
369​
370 assert.ok(spawned);
371 assert.equal(spawned.command, executable);
372 assert.deepEqual(spawned.args, [
373 "--line",
374 "12",
375 "--column",
376 "3",
377 "/workspace with spaces/file.ts",
378 ]);
379 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
380);
381​
382it.effect("matches macOS bundles on the name boundary only", () =>
383 Effect.gen(function* () {
384 const fs = yield* FileSystem.FileSystem;
385 const path = yield* Path.Path;
386 const unrelatedHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-unrelated-" });
387 const unrelated = path.join(
388 unrelatedHome,
389 "Applications/WebStormHelper.app/Contents/MacOS/webstorm",
390 );
391 yield* fs.makeDirectory(path.dirname(unrelated), { recursive: true });
392 yield* fs.writeFileString(unrelated, "#!/bin/sh\n");
393 yield* fs.chmod(unrelated, 0o755);
394​
395 const editorsFor = (home: string) =>
396 Effect.gen(function* () {
397 const launcher = yield* ExternalLauncher.ExternalLauncher;
398 return yield* launcher.resolveAvailableEditors();
399 }).pipe(
400 Effect.provide(
401 layerTest({ platform: "darwin", env: { HOME: home, PATH: path.join(home, "bin") } }),
402 ),
403 );
404​
405 assert.notInclude(yield* editorsFor(unrelatedHome), "webstorm");
406​
407 const exactHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-exact-" });
408 const exact = path.join(exactHome, "Applications/WebStorm.app/Contents/MacOS/webstorm");
409 yield* fs.makeDirectory(path.dirname(exact), { recursive: true });
410 yield* fs.writeFileString(exact, "#!/bin/sh\n");
411 yield* fs.chmod(exact, 0o755);
412​
413 assert.include(yield* editorsFor(exactHome), "webstorm");
414 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
415);
416​
417it.effect("keeps the macOS VS Code Insiders bundle out of stable VS Code", () =>
418 Effect.gen(function* () {
419 const fs = yield* FileSystem.FileSystem;
420 const path = yield* Path.Path;
421 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-insiders-" });
422 // The real Insiders bundle ships both `code-insiders` and `code`.
423 const bin = path.join(
424 home,
425 "Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin",
426 );
427 yield* fs.makeDirectory(bin, { recursive: true });
428 for (const name of ["code", "code-insiders"]) {
429 yield* fs.writeFileString(path.join(bin, name), "#!/bin/sh\n");
430 yield* fs.chmod(path.join(bin, name), 0o755);
431 }
432​
433 let spawned: ChildProcess.StandardCommand | undefined;
434 const editors = yield* Effect.gen(function* () {
435 const launcher = yield* ExternalLauncher.ExternalLauncher;
436 const available = yield* launcher.resolveAvailableEditors();
437 yield* launcher.launchEditor({ editor: "vscode-insiders", cwd: "/workspace/file.ts" });
438 return available;
439 }).pipe(
440 Effect.provide(
441 layerTest({
442 platform: "darwin",
443 env: { HOME: home, PATH: path.join(home, "empty") },
444 onSpawn: (command) => {
445 spawned = command;
446 },
447 }),
448 ),
449 );
450​
451 assert.notInclude(editors, "vscode");
452 assert.include(editors, "vscode-insiders");
453 assert.equal(spawned?.command, path.join(bin, "code-insiders"));
454 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
455);
456​
457for (const { platform, installPath, onPath } of [
458 { platform: "darwin", installPath: ".local/bin/agy", onPath: true },
459 { platform: "linux", installPath: ".local/bin/agy", onPath: false },
460 { platform: "win32", installPath: "agy/bin/agy.cmd", onPath: true },
461] as const) {
462 it.effect.skipIf(windowsHost && platform !== "win32")(
463 `does not report the agy CLI as the Antigravity IDE on ${platform}`,
464 () =>
465 Effect.gen(function* () {
466 const fs = yield* FileSystem.FileSystem;
467 const path = yield* Path.Path;
468 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-cli-" });
469 const executable = path.join(home, installPath);
470 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
471 yield* fs.writeFileString(executable, "#!/bin/sh\n");
472 yield* fs.chmod(executable, 0o755);
473 const editors = yield* Effect.gen(function* () {
474 const launcher = yield* ExternalLauncher.ExternalLauncher;
475 return yield* launcher.resolveAvailableEditors();
476 }).pipe(
477 Effect.provide(
478 layerTest({
479 platform,
480 env: {
481 HOME: home,
482 LOCALAPPDATA: home,
483 PATH: onPath ? path.dirname(executable) : path.join(home, "empty"),
484 PATHEXT: ".COM;.EXE;.BAT;.CMD",
485 },
486 }),
487 ),
488 );
489 assert.notInclude(editors, "antigravity");
490 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
491 );
493​
494it.effect.skipIf(windowsHost)("ignores unusable app bundles and keeps PATH launchers first", () =>
495 Effect.gen(function* () {
496 const fs = yield* FileSystem.FileSystem;
497 const path = yield* Path.Path;
498 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-priority-" });
499 const executable = path.join(home, "Applications/Cursor.app/Contents/Resources/app/bin/code");
500 const env = { HOME: home, PATH: path.join(home, "bin") };
501 const discover = Effect.gen(function* () {
502 const launcher = yield* ExternalLauncher.ExternalLauncher;
503 return yield* launcher.resolveAvailableEditors();
504 });
505 const before = yield* discover.pipe(Effect.provide(layerTest({ platform: "darwin", env })));
506 yield* fs.makeDirectory(executable, { recursive: true });
507 assert.deepEqual(
508 yield* discover.pipe(Effect.provide(layerTest({ platform: "darwin", env }))),
509 before,
510 );
511 yield* fs.remove(executable, { recursive: true });
512 yield* fs.writeFileString(executable, "#!/bin/sh\n");
513 yield* fs.chmod(executable, 0o644);
514 assert.deepEqual(
515 yield* discover.pipe(Effect.provide(layerTest({ platform: "darwin", env }))),
516 before,
517 );
518 yield* fs.chmod(executable, 0o755);
519 yield* fs.makeDirectory(env.PATH);
520 yield* fs.writeFileString(path.join(env.PATH, "cursor"), "#!/bin/sh\n");
521 yield* fs.chmod(path.join(env.PATH, "cursor"), 0o755);
522 let spawned: ChildProcess.StandardCommand | undefined;
523 yield* Effect.gen(function* () {
524 const launcher = yield* ExternalLauncher.ExternalLauncher;
525 yield* launcher.launchEditor({ editor: "cursor", cwd: "/workspace" });
526 }).pipe(
527 Effect.provide(
528 layerTest({
529 platform: "darwin",
530 env: { ...env, PATH: `${env.PATH}:/usr/bin` },
531 onSpawn: (command) => {
532 spawned = command;
533 },
534 }),
535 ),
536 );
537 assert.equal(spawned?.command, "cursor");
538 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
539);
540​
541it.effect("keeps scanning after the caller is interrupted and shares that scan", () => {
542 const fileInfo = { type: "File" } as FileSystem.File.Info;
543 const release = Deferred.makeUnsafe<void>();
544 let parkedStats = 0;
545 const layerLauncher = ExternalLauncher.layer.pipe(
546 Layer.provide(
547 Layer.mergeAll(
548 FileSystem.layerNoop({
549 stat: () =>
550 Effect.gen(function* () {
551 if (!Deferred.isDoneUnsafe(release)) {
552 parkedStats += 1;
553 yield* Deferred.await(release);
554 }
555 return fileInfo;
556 }),
557 }),
558 Path.layer,
559 Layer.succeed(
560 ChildProcessSpawner.ChildProcessSpawner,
561 ChildProcessSpawner.make(() => Effect.sync(() => makeMockDetachedHandle())),
562 ),
563 ),
564 ),
565 );
566​
567 return Effect.gen(function* () {
568 const launcher = yield* ExternalLauncher.ExternalLauncher;
569​
570 const interrupted = yield* Effect.forkChild(launcher.resolveAvailableEditors());
571 yield* Effect.yieldNow;
572 yield* Fiber.interrupt(interrupted);
573​
574 const next = yield* Effect.forkChild(launcher.resolveAvailableEditors());
575 yield* Effect.yieldNow;
576 assert.equal(parkedStats, 1);
577​
578 yield* Deferred.succeed(release, undefined);
579 const editors = yield* Fiber.join(next);
580 assert.equal(editors.includes("vscode"), true);
581 }).pipe(
582 Effect.provide(
583 Layer.mergeAll(
584 layerLauncher,
585 Layer.succeed(HostProcess.Platform, "win32"),
586 ConfigProvider.layer(
587 ConfigProvider.fromEnv({
588 env: {
589 PATH: "C:\\t3-editor-discovery-interrupt-test",
590 PATHEXT: ".COM;.EXE;.BAT;.CMD",
591 },
592 }),
593 ),
594 ),
595 ),
596 );
597});
598​
599const launchWindowsShimEditor = (cwd: string) =>
600 Effect.gen(function* () {
601 const fileSystem = yield* FileSystem.FileSystem;
602 const path = yield* Path.Path;
603 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
604 yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n");
605 let spawned: ChildProcess.StandardCommand | undefined;
606 const exit = yield* Effect.gen(function* () {
607 const launcher = yield* ExternalLauncher.ExternalLauncher;
608 return yield* launcher.launchEditor({ editor: "vscode", cwd }).pipe(Effect.exit);
609 }).pipe(
610 Effect.provide(
611 layerTest({
612 platform: "win32",
613 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
614 resolveExecutable: (command) =>
615 command === "code" ? "C:\\Program Files\\Microsoft VS Code\\bin\\code.CMD" : command,
616 onSpawn: (command) => {
617 spawned = command;
618 },
619 }),
620 ),
621 );
622 return { exit, spawned };
623 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
624​
625it.effect.each([
626 ["CRLF", "C:\\workspace\\file.ts\r\ncalc.exe"],
627 ["LF", "C:\\workspace\\file.ts\ncalc.exe"],
628 ["double quote", 'C:\\workspace\\file.ts" & calc.exe & "'],
629] as const)("does not start a Windows command shim for a path with a $0", ([, cwd]) =>
630 Effect.gen(function* () {
631 const { exit, spawned } = yield* launchWindowsShimEditor(cwd);
632 assert.equal(spawned, undefined);
633 assert.isTrue(Exit.isFailure(exit));
634 if (Exit.isFailure(exit)) {
635 assert.instanceOf(
636 Cause.squash(exit.cause),
637 ExternalLauncher.ExternalLauncherUnsupportedTargetError,
638 );
639 }
640 }),
641);
642​
643it.effect("starts a Windows command shim for unicode paths with cmd metacharacters", () =>
644 Effect.gen(function* () {
645 const { exit, spawned } = yield* launchWindowsShimEditor(
646 "C:\\Users\\jö\\R&D (100%)\\naïve ^file!.ts:3:7",
647 );
648 assert.isTrue(Exit.isSuccess(exit));
649 assert.ok(spawned);
650 assert.equal(spawned.options.shell, true);
651 assert.deepEqual(spawned.args, []);
652 assert.isTrue(
653 spawned.command.endsWith(
654 ' ^"--goto^" ^"C:\\Users\\jö\\R^&D^ ^(100^%^)\\naïve^ ^^file^!.ts:3:7^"',
655 ),
656 );
657 }),
658);