Skip to content

Commit 580b526

Browse files
fix(server): respect the default Windows file manager (#28205)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 6bc105f commit 580b526

2 files changed

Lines changed: 134 additions & 12 deletions

File tree

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

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,111 @@ it.effect("launches an installed editor with platform-safe arguments", () =>
339339
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
340340
);
341341

342+
it.live.skipIf(!windowsHost)("opens Windows folders through the default shell handler", () =>
343+
Effect.gen(function* () {
344+
let spawned: ChildProcess.StandardCommand | undefined;
345+
yield* Effect.gen(function* () {
346+
const launcher = yield* ExternalLauncher.ExternalLauncher;
347+
yield* launcher.launchEditor({
348+
editor: "file-manager",
349+
cwd: "C:/workspace [draft]/author's ‘left’ ‚low‛ $media/’; Write-Output injected; #",
350+
});
351+
}).pipe(
352+
Effect.provide(
353+
layerTest({
354+
platform: "win32",
355+
env: { PATH: "C:\\Windows", PATHEXT: ".EXE" },
356+
onSpawn: (command) => {
357+
spawned = command;
358+
},
359+
}),
360+
),
361+
);
362+
assert.ok(spawned);
363+
assert.equal(spawned.options.detached, false);
364+
const source = Buffer.from(String(spawned.args.at(-1) ?? ""), "base64").toString("utf16le");
365+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
366+
const output = yield* spawner.string(
367+
ChildProcess.make(spawned.command, [
368+
...spawned.args.slice(0, -1),
369+
Buffer.from(
370+
`[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false); function Invoke-Item { param($LiteralPath) Write-Output $LiteralPath }; ${source}`,
371+
"utf16le",
372+
).toString("base64"),
373+
]),
374+
);
375+
assert.equal(
376+
output.trim(),
377+
"C:\\workspace [draft]\\author's ‘left’ ‚low‛ $media\\’; Write-Output injected; #",
378+
);
379+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
380+
);
381+
382+
for (const [replacement, verb, handler, target, expected] of [
383+
["none", "none", "", "C:/repo/file.txt", 'explorer.exe /select,"C:\\repo\\file.txt"'],
384+
["Directory", "openindopus", "dopusrt.exe", "C:/repo/file.txt", "shell:C:\\repo"],
385+
["Folder", "openindopus", "dopusrt.exe", "C:/repo/file.txt", "shell:C:\\repo"],
386+
[
387+
"Folder",
388+
"open",
389+
'"C:\\Windows\\explorer.exe"',
390+
"C:/repo/file.txt",
391+
'explorer.exe /select,"C:\\repo\\file.txt"',
392+
],
393+
[
394+
"Folder",
395+
"open",
396+
"ExplorerDelegate",
397+
"C:/repo/file.txt",
398+
'explorer.exe /select,"C:\\repo\\file.txt"',
399+
],
400+
[
401+
"Drive",
402+
"openindopus",
403+
"dopusrt.exe",
404+
"C:/repo/file.txt",
405+
'explorer.exe /select,"C:\\repo\\file.txt"',
406+
],
407+
["Drive", "openindopus", "dopusrt.exe", "C:/file.txt", "shell:C:\\"],
408+
] as const) {
409+
it.live.skipIf(!windowsHost)(
410+
`reveals ${target} with ${replacement}/${verb} handled by ${handler}`,
411+
() =>
412+
Effect.gen(function* () {
413+
let spawned: ChildProcess.StandardCommand | undefined;
414+
yield* Effect.gen(function* () {
415+
const launcher = yield* ExternalLauncher.ExternalLauncher;
416+
yield* launcher.launchEditor({
417+
editor: "file-manager",
418+
cwd: target,
419+
reveal: true,
420+
});
421+
}).pipe(
422+
Effect.provide(
423+
layerTest({
424+
platform: "win32",
425+
env: { PATH: "C:\\Windows", PATHEXT: ".EXE" },
426+
onSpawn: (command) => {
427+
spawned = command;
428+
},
429+
}),
430+
),
431+
);
432+
assert.ok(spawned);
433+
const source = Buffer.from(String(spawned.args.at(-1) ?? ""), "base64").toString("utf16le");
434+
const stubs = `function Get-ItemProperty { param($LiteralPath, $ErrorAction) @{ '(default)' = if ($LiteralPath -eq 'Registry::HKEY_CLASSES_ROOT\\${replacement}\\shell') { '${verb}' } elseif ($LiteralPath -eq 'Registry::HKEY_CLASSES_ROOT\\${replacement}\\shell\\${verb}\\command') { '${handler}' } else { 'none' }; DelegateExecute = if ('${handler}' -eq 'ExplorerDelegate') { '{11dbb47c-a525-400b-9e80-a54615a090c0}' } else { '' } } }; function Invoke-Item { param($LiteralPath) Write-Output "shell:$LiteralPath" }; function Start-Process { param($FilePath, $ArgumentList) Write-Output "$FilePath $ArgumentList" }; `;
435+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
436+
const output = yield* spawner.string(
437+
ChildProcess.make(spawned.command, [
438+
...spawned.args.slice(0, -1),
439+
Buffer.from(stubs + source, "utf16le").toString("base64"),
440+
]),
441+
);
442+
assert.equal(output.trim(), expected);
443+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
444+
);
445+
}
446+
342447
it.effect.skipIf(windowsHost)(
343448
"reveals a WSL file in Windows File Explorer through its UNC path",
344449
() =>
@@ -359,7 +464,7 @@ it.effect.skipIf(windowsHost)(
359464
const editors = yield* launcher.resolveAvailableEditors();
360465
yield* launcher.launchEditor({
361466
editor: "file-manager",
362-
cwd: "/home/t3/workspace [draft]/author's $media/clip|draft.mp4",
467+
cwd: "/home/t3/workspace [draft]/author's ‘$media/clip|draft.mp4",
363468
reveal: true,
364469
});
365470
return { kind, editors };
@@ -386,9 +491,14 @@ it.effect.skipIf(windowsHost)(
386491
assert.equal(spawned.options.detached, true);
387492
const encodedCommand = String(spawned.args[spawned.args.length - 1] ?? "");
388493
const decodedCommand = Buffer.from(encodedCommand, "base64").toString("utf16le");
389-
assert.equal(
494+
assert.include(decodedCommand, "'Directory', 'Folder'");
495+
assert.include(
496+
decodedCommand,
497+
"Invoke-Item -LiteralPath '\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\workspace [draft]\\author''s ‘‘$media'",
498+
);
499+
assert.include(
390500
decodedCommand,
391-
"$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' + '\"') }",
501+
"'\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\workspace [draft]\\author''s ‘‘$media\\clip|draft.mp4'",
392502
);
393503
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
394504
);

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ function encodeUtf16LeBase64(input: string): string {
182182
}
183183

184184
function escapePowerShellStringLiteral(input: string): string {
185-
return `'${input.replaceAll("'", "''")}'`;
185+
// PowerShell also recognises typographic apostrophes as single-quote delimiters.
186+
return `'${input.replace(/['\u2018-\u201b]/g, "$&$&")}'`;
186187
}
187188

188189
function resolveWindowsRoot(env: NodeJS.ProcessEnv): string {
@@ -527,26 +528,37 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
527528
return yield* resolveFileManagerRevealLaunch(target, platform, env, command);
528529
}
529530

531+
if (platform === "win32") {
532+
// ShellExecute honours replacement folder handlers that explorer.exe bypasses.
533+
return {
534+
...powerShellFileManagerLaunch(
535+
target,
536+
`Invoke-Item -LiteralPath ${escapePowerShellStringLiteral(normalizeWindowsFileManagerPath(target))}`,
537+
resolvePowerShellPath(env),
538+
),
539+
detached: WINDOWS_POWERSHELL_DETACHED,
540+
};
541+
}
542+
530543
return {
531544
editor: editorDef.id,
532545
target,
533546
command,
534547
args:
535548
command === "explorer.exe" && env.WSL_DISTRO_NAME !== undefined
536549
? [resolveWslFileManagerPath(target, env.WSL_DISTRO_NAME)]
537-
: platform === "win32"
538-
? // Explorer interprets / as a switch and silently opens a fallback folder.
539-
[normalizeWindowsFileManagerPath(target)]
540-
: [target],
550+
: [target],
541551
};
542552
});
543553

544-
// A replacement manager registers a default Directory shell verb; stock Windows
545-
// uses "none". Explorer needs its /select argument passed verbatim through
546-
// PowerShell because Node's argument quoting changes how Explorer parses it.
554+
// Replacement managers can register under Directory, Drive, or Folder. Explorer
555+
// needs its /select argument passed verbatim through PowerShell because Node's
556+
// argument quoting changes how Explorer parses it.
547557
function buildFileManagerRevealPowerShellSource(target: string, parent: string): string {
548558
const literal = escapePowerShellStringLiteral(target);
549-
return `$ProgressPreference = 'SilentlyContinue'; $folderVerb = (Get-ItemProperty -LiteralPath 'Registry::HKEY_CLASSES_ROOT\\Directory\\shell' -ErrorAction SilentlyContinue).'(default)'; if ($folderVerb -and $folderVerb -ne 'none') { Invoke-Item -LiteralPath ${escapePowerShellStringLiteral(parent)} } else { Start-Process 'explorer.exe' -ArgumentList ('/select,"' + ${literal} + '"') }`;
559+
const folderClass = /^[a-z]:\\$/i.test(parent) ? "Drive" : "Directory";
560+
// Explorer's ordinary open/explore handlers may also be explicit defaults.
561+
return String.raw`$ProgressPreference = 'SilentlyContinue'; $folderVerb = '${folderClass}', 'Folder' | ForEach-Object { $key = "Registry::HKEY_CLASSES_ROOT\$_\shell"; $verb = (Get-ItemProperty -LiteralPath $key -ErrorAction SilentlyContinue).'(default)'; if ($verb -and $verb -ne 'none') { $handler = Get-ItemProperty -LiteralPath "$key\$verb\command" -ErrorAction SilentlyContinue; if ($handler.DelegateExecute -ne '{11dbb47c-a525-400b-9e80-a54615a090c0}' -and $handler.'(default)' -notmatch '^\s*"?(?:[^"]*[\\/])?explorer\.exe(?:["\s]|$)') { $verb } } }; if ($folderVerb) { Invoke-Item -LiteralPath ${escapePowerShellStringLiteral(parent)} } else { Start-Process 'explorer.exe' -ArgumentList ('/select,"' + ${literal} + '"') }`;
550562
}
551563

552564
function powerShellFileManagerLaunch(

0 commit comments

Comments
 (0)