Skip to content

Commit 375b839

Browse files
committed
fix(server): restore Windows file-manager actions
1 parent 53510d4 commit 375b839

2 files changed

Lines changed: 88 additions & 9 deletions

File tree

‎apps/server/src/process/externalLauncher.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,80 @@ it.effect("reveals a file in File Explorer through PowerShell on Windows", () =>
338338
"$ProgressPreference = 'SilentlyContinue'; Start-Process 'explorer.exe' -ArgumentList ('/select,\"' + 'C:\\workspace with spaces\\media\\author''s clip.mp4' + '\"')",
339339
);
340340
assert.equal(spawned.options.shell, false);
341+
342+
yield* Effect.gen(function* () {
343+
const launcher = yield* ExternalLauncher.ExternalLauncher;
344+
yield* launcher.launchEditor({
345+
editor: "file-manager",
346+
cwd: "C:/workspace with spaces/media/author's clip.mp4",
347+
});
348+
}).pipe(
349+
Effect.provide(
350+
testLayer({
351+
platform: "win32",
352+
env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
353+
onSpawn: (command) => {
354+
spawned = command;
355+
},
356+
}),
357+
),
358+
);
359+
assert.deepEqual(spawned.args, ["C:\\workspace with spaces\\media\\author's clip.mp4"]);
341360
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
342361
);
343362

363+
it.effect.skipIf(!windowsHost)(
364+
"executes the Windows reveal helper with the launcher's process options",
365+
() =>
366+
Effect.gen(function* () {
367+
const fileSystem = yield* FileSystem.FileSystem;
368+
const path = yield* Path.Path;
369+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
370+
const tempDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-reveal-launch-" });
371+
const recorderPath = path.join(tempDir, "recorder.cmd");
372+
const outputPath = path.join(tempDir, "argv.txt");
373+
yield* fileSystem.writeFileString(recorderPath, `@echo off\r\n>"${outputPath}" echo(%*\r\n`);
374+
let helper: ChildProcessSpawner.ChildProcessHandle | undefined;
375+
const recordingSpawner = ChildProcessSpawner.make((command) =>
376+
Effect.gen(function* () {
377+
assert.ok(ChildProcess.isStandardCommand(command));
378+
if (!ChildProcess.isStandardCommand(command))
379+
throw new Error("Expected a standard command");
380+
const source = Buffer.from(command.args.at(-1) ?? "", "base64").toString("utf16le");
381+
// Substitute only Explorer, preserving the production launch options.
382+
// Wait for the recorder so its output is complete when the helper exits.
383+
const recordingSource =
384+
source.replace("'explorer.exe'", `'${recorderPath.replaceAll("'", "''")}'`) + " -Wait";
385+
helper = yield* spawner.spawn(
386+
ChildProcess.make(
387+
command.command,
388+
[
389+
...command.args.slice(0, -1),
390+
Buffer.from(recordingSource, "utf16le").toString("base64"),
391+
],
392+
command.options,
393+
),
394+
);
395+
return helper;
396+
}),
397+
);
398+
const launcher = yield* ExternalLauncher.make.pipe(
399+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, recordingSpawner),
400+
);
401+
yield* launcher.launchEditor({
402+
editor: "file-manager",
403+
cwd: "C:/workspace with spaces/author's file.md",
404+
reveal: true,
405+
});
406+
assert.ok(helper);
407+
assert.equal(yield* helper.exitCode, 0);
408+
assert.equal(
409+
(yield* fileSystem.readFileString(outputPath)).trim(),
410+
`/select,"C:\\workspace with spaces\\author's file.md"`,
411+
);
412+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
413+
);
414+
344415
// Real-chain smoke check for the Explorer selection contract: runs the exact
345416
// PowerShell source the reveal launch encodes, against a stub that records
346417
// the raw argument tail it receives, and asserts a spaced path arrives as the

‎apps/server/src/process/externalLauncher.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ interface EditorLaunch {
5252
readonly target: string;
5353
readonly command: string;
5454
readonly args: ReadonlyArray<string>;
55+
readonly detached?: boolean;
5556
}
5657

5758
interface ProcessLaunch {
@@ -568,9 +569,11 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
568569
target: input.cwd,
569570
command,
570571
args:
571-
command === "explorer.exe" && env.WSL_DISTRO_NAME !== undefined
572-
? [resolveWslFileManagerPath(input.cwd, env.WSL_DISTRO_NAME)]
573-
: [input.cwd],
572+
platform === "win32"
573+
? [normalizeWindowsFileManagerPath(input.cwd)]
574+
: command === "explorer.exe" && env.WSL_DISTRO_NAME !== undefined
575+
? [resolveWslFileManagerPath(input.cwd, env.WSL_DISTRO_NAME)]
576+
: [input.cwd],
574577
};
575578
});
576579

@@ -629,11 +632,16 @@ const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch
629632
}
630633

631634
if (platform === "win32") {
632-
return fileExplorerRevealLaunch(
633-
target,
634-
normalizeWindowsFileManagerPath(target),
635-
resolvePowerShellPath(env),
636-
);
635+
return {
636+
...fileExplorerRevealLaunch(
637+
target,
638+
normalizeWindowsFileManagerPath(target),
639+
resolvePowerShellPath(env),
640+
),
641+
// Windows PowerShell can exit successfully without running its command
642+
// when detached with ignored stdio. The helper is still unref'ed below.
643+
detached: false,
644+
};
637645
}
638646

639647
if (
@@ -734,7 +742,7 @@ const launchEditorProcess = Effect.fn("externalLauncher.launchEditorProcess")(fu
734742
command: spawnCommand.command,
735743
args: spawnCommand.args,
736744
options: {
737-
detached: true,
745+
detached: launch.detached ?? true,
738746
shell: spawnCommand.shell,
739747
stdin: "ignore",
740748
stdout: "ignore",

0 commit comments

Comments
 (0)