@devries-cameron20 / t3code Mirror · read-only / commits / 37714a8de5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 16:00 UTC

perf(desktop): revealing a file on Windows no longer waits on PowerShell (#33064)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: lyn1dev <lyn1dev@users.noreply.github.com>

lyn1 committed Nov 14, 2026 at 04:57 UTC 37714a8de5effecdea2d712d0828d4769f080a59
3 files changed +169 -10
apps/server/src/process/externalLauncher.test.ts
+68
@@ -21,6 +21,7 @@ import { resolveEditorCommand } from "@t3tools/shared/editor";
21 import * as HostProcess from "@t3tools/shared/HostProcess";
22 import { SpawnExecutableResolution } from "@t3tools/shared/shell";
23 import * as ExternalLauncher from "./externalLauncher.ts";
24 +import { WindowsVerbatimSpawn } from "./windowsVerbatimSpawn.ts";
25
26 // Windows drive letters break mocked POSIX PATH splitting; these tests also require /bin/sh stubs.
27 const windowsHost = HostProcess.Platform.defaultValue() === "win32";
@@ -392,6 +393,73 @@ it.effect.skipIf(windowsHost)(
393 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
394 );
395
396 +const STOCK_FOLDER_VERB =
397 + "\r\nHKEY_CLASSES_ROOT\\Directory\\shell\r\n (Default) REG_SZ none\r\n\r\n";
398 +
399 +it.effect.each([
400 + ["selects the file with Explorer on stock Windows", { stdout: STOCK_FOLDER_VERB }, true],
401 + [
402 + "keeps the PowerShell reveal for a replacement file manager",
403 + { stdout: " (Default) REG_SZ open\r\n" },
404 + false,
405 + ],
406 + ["keeps the PowerShell reveal when the registry lookup fails", { exitCode: 1 }, false],
407 +] as const)("%s", ([, registry, native]) =>
408 + Effect.gen(function* () {
409 + const fileSystem = yield* FileSystem.FileSystem;
410 + const path = yield* Path.Path;
411 + const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
412 + yield* fileSystem.writeFileString(path.join(binDir, "explorer.EXE"), "");
413 + const spawned: ChildProcess.StandardCommand[] = [];
414 + const verbatim: Array<readonly [string, ReadonlyArray<string>]> = [];
415 + const exit = yield* Effect.gen(function* () {
416 + const launcher = yield* ExternalLauncher.ExternalLauncher;
417 + return yield* launcher
418 + .launchEditor({
419 + editor: "file-manager",
420 + cwd: "C:/Users/t3/my project/a,b.ts",
421 + reveal: true,
422 + })
423 + .pipe(Effect.exit);
424 + }).pipe(
425 + Effect.provideService(WindowsVerbatimSpawn, (command, args) =>
426 + Effect.sync(() => verbatim.push([command, args])),
427 + ),
428 + Effect.provide(
429 + layerTest({
430 + platform: "win32",
431 + env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD", SYSTEMROOT: "C:\\Windows" },
432 + onSpawn: (command) => spawned.push(command),
433 + spawnResult: (command) => (command.command.endsWith("reg.exe") ? registry : undefined),
434 + }),
435 + ),
436 + );
437 +
438 + assert.deepEqual(
439 + spawned.map((command) => command.command),
440 + ["C:\\Windows\\System32\\reg.exe"],
441 + );
442 + if (native) {
443 + assert.isTrue(Exit.isSuccess(exit));
444 + assert.deepEqual(verbatim, [
445 + ["C:\\Windows\\explorer.exe", ['/select,"C:\\Users\\t3\\my project\\a,b.ts"']],
446 + ]);
447 + } else {
448 + assert.deepEqual(verbatim, []);
449 + // The PowerShell launch is reached; this host has no powershell.exe to run it.
450 + assert.isTrue(Exit.isFailure(exit));
451 + if (Exit.isFailure(exit)) {
452 + const error = Cause.squash(exit.cause);
453 + assert.instanceOf(error, ExternalLauncher.ExternalLauncherCommandNotFoundError);
454 + assert.equal(
455 + (error as ExternalLauncher.ExternalLauncherCommandNotFoundError).command,
456 + "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
457 + );
458 + }
459 + }
460 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
461 +);
462 +
463 for (const wsl of [false, true]) {
464 it.effect.skipIf(windowsHost)(
465 "selects a file over D-Bus on " + (wsl ? "WSLg without interop PowerShell" : "Linux"),
apps/server/src/process/externalLauncher.ts
+72 -10
@@ -34,6 +34,7 @@ import * as ChildProcess from "effect/process/ChildProcess";
34 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
35
36 import { makeStaleWhileRevalidate } from "../environment/staleWhileRevalidate.ts";
37 +import { WindowsVerbatimSpawn } from "./windowsVerbatimSpawn.ts";
38 export {
39 ExternalLauncherError,
40 ExternalLauncherBrowserSpawnError,
@@ -184,8 +185,12 @@ function escapePowerShellStringLiteral(input: string): string {
185 return `'${input.replaceAll("'", "''")}'`;
186 }
187
188 +function resolveWindowsRoot(env: NodeJS.ProcessEnv): string {
189 + return env.SYSTEMROOT || env.windir || String.raw`C:\Windows`;
190 +}
191 +
192 function resolvePowerShellPath(env: NodeJS.ProcessEnv = {}): string {
188 - return `${env.SYSTEMROOT || env.windir || String.raw`C:\Windows`}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
193 + return `${resolveWindowsRoot(env)}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
194 }
195
196 function resolveWslPowerShellPath(): string {
@@ -665,18 +670,67 @@ const tryRevealLinuxItem = Effect.fn("externalLauncher.tryRevealLinuxItem")(
670 }),
671 );
672
673 +// Stock Windows answers "none"; any other verb may be a replacement file manager, which only
674 +// the PowerShell reveal honors. Reading stdout is about 600 ms faster than spawner.string here.
675 +const hasStockWindowsFolderVerb = Effect.fn("externalLauncher.hasStockWindowsFolderVerb")(
676 + function* () {
677 + const env = yield* readBrowserLaunchEnv;
678 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
679 + const handle = yield* spawner.spawn(
680 + ChildProcess.make(
681 + `${resolveWindowsRoot(env)}\\System32\\reg.exe`,
682 + ["query", "HKCR\\Directory\\shell", "/ve"],
683 + { stdin: "ignore", stderr: "ignore" },
684 + ),
685 + );
686 + const [stdout, exitCode] = yield* Effect.all(
687 + [handle.stdout.pipe(Stream.decodeText(), Stream.mkString), handle.exitCode],
688 + { concurrency: "unbounded" },
689 + );
690 + return exitCode === 0 && /REG_SZ\s+none\s*$/im.test(stdout);
691 + },
692 + Effect.scoped,
693 + Effect.timeout("2 seconds"),
694 + Effect.orElseSucceed(() => false),
695 +);
696 +
697 +// Explorer parses /select itself, so it needs the argument verbatim rather than Node's quoting.
698 +const revealWindowsItem = Effect.fn("externalLauncher.revealWindowsItem")(function* (
699 + target: string,
700 +) {
701 + const command = `${resolveWindowsRoot(yield* readBrowserLaunchEnv)}\\explorer.exe`;
702 + const args = [`/select,"${normalizeWindowsFileManagerPath(target)}"`];
703 + yield* (yield* WindowsVerbatimSpawn)(command, args).pipe(
704 + Effect.mapError(
705 + (cause) =>
706 + new ExternalLauncherEditorSpawnError({
707 + editor: "file-manager",
708 + target,
709 + command,
710 + args,
711 + cause,
712 + }),
713 + ),
714 + );
715 +});
716 +
717 const launchEditor = Effect.fn("externalLauncher.launchEditor")(function* (
718 input: LaunchEditorInput,
719 + hasStockFolderVerb: Effect.Effect<boolean>,
720 ) {
721 const launch = yield* resolveEditorLaunch(input);
672 - if (
673 - input.editor === "file-manager" &&
674 - input.reveal === true &&
675 - launch.command === "xdg-open" &&
676 - (yield* HostProcess.Platform) === "linux" &&
677 - (yield* tryRevealLinuxItem(launch.target))
678 - ) {
679 - return;
722 + if (input.editor === "file-manager" && input.reveal === true) {
723 + const platform = yield* HostProcess.Platform;
724 + if (
725 + platform === "linux" &&
726 + launch.command === "xdg-open" &&
727 + (yield* tryRevealLinuxItem(launch.target))
728 + ) {
729 + return;
730 + }
731 + if (platform === "win32" && (yield* hasStockFolderVerb)) {
732 + return yield* revealWindowsItem(launch.target);
733 + }
734 }
735 yield* launchEditorProcess(launch);
736 });
@@ -837,6 +891,12 @@ export const make = Effect.gen(function* () {
891 ),
892 EDITOR_DISCOVERY_CACHE_TTL,
893 );
894 + const cachedStockWindowsFolderVerb = yield* makeStaleWhileRevalidate(
895 + hasStockWindowsFolderVerb().pipe(
896 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
897 + ),
898 + EDITOR_DISCOVERY_CACHE_TTL,
899 + );
900
901 // Forwarders retain the underlying launcher and discovery spans.
902 return ExternalLauncher.of({
@@ -854,7 +914,9 @@ export const make = Effect.gen(function* () {
914 ),
915 launchEditor: Effect.fnUntraced(
916 function* (input) {
857 - return yield* provideCommandResolutionServices(launchEditor(input));
917 + return yield* provideCommandResolutionServices(
918 + launchEditor(input, cachedStockWindowsFolderVerb),
919 + );
920 },
921 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
922 ),
apps/server/src/process/windowsVerbatimSpawn.ts new
+29
@@ -0,0 +1,29 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - Effect's spawner has no windowsVerbatimArguments option.
2 +import * as NodeChildProcess from "node:child_process";
3 +
4 +import * as Context from "effect/Context";
5 +import * as Effect from "effect/Effect";
6 +
7 +export type WindowsVerbatimSpawner = (
8 + command: string,
9 + args: ReadonlyArray<string>,
10 +) => Effect.Effect<void, Error>;
11 +
12 +const spawnDetachedVerbatim: WindowsVerbatimSpawner = (command, args) =>
13 + Effect.callback<void, Error>((resume) => {
14 + const child = NodeChildProcess.spawn(command, args, {
15 + detached: true,
16 + stdio: "ignore",
17 + windowsVerbatimArguments: true,
18 + });
19 + child.once("error", (error) => resume(Effect.fail(error)));
20 + child.once("spawn", () => {
21 + child.unref();
22 + resume(Effect.void);
23 + });
24 + });
25 +
26 +export const WindowsVerbatimSpawn = Context.Reference<WindowsVerbatimSpawner>(
27 + "t3/process/WindowsVerbatimSpawn",
28 + { defaultValue: () => spawnDetachedVerbatim },
29 +);