Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions apps/server/src/device/AgentDeviceShim.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// @effect-diagnostics nodeBuiltinImport:off - Executes the emitted Node launcher with a local spawn fixture.
import * as NodeChildProcess from "node:child_process";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import {
HostProcessExecutablePath,
HostProcessIsExecutable,
HostProcessPlatform,
} from "@t3tools/shared/hostProcess";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";

import { ensureAgentDeviceShim } from "./AgentDeviceShim.ts";

it.effect(
"sets windowsHide and forwards arguments and exit status in the emitted device launcher",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-shim-" });
const entryPath = path.join(root, "entry with spaces.mjs");
const shimDir = yield* ensureAgentDeviceShim({ entryPath, stateDir: root });
const preload = path.join(root, "spawn-fixture.cjs");
yield* fs.writeFileString(
preload,
`
const { EventEmitter } = require("node:events");
require("node:child_process").spawn = (command, args, options) => {
console.log(JSON.stringify({ command, args, windowsHide: options.windowsHide, stdio: options.stdio,
inheritedConfig: options.env.AGENT_DEVICE_CONFIG }));
const child = new EventEmitter();
queueMicrotask(() => child.emit("exit", 23));
return child;
};
require("node:module").syncBuiltinESMExports();
`,
);
const result = NodeChildProcess.spawnSync(
process.execPath,
["--require", preload, path.join(shimDir, "agent-device-launcher.mjs"), "--help"],
{
encoding: "utf8",
windowsHide: true,
env: { ...process.env, AGENT_DEVICE_CONFIG: "fixture-config" },
},
);
assert.isUndefined(result.error);
assert.equal(result.status, 23);
assert.deepEqual(JSON.parse(result.stdout), {
command: process.execPath,
args: [entryPath, "--help"],
windowsHide: true,
stdio: "inherit",
});
assert.include(
yield* fs.readFileString(path.join(shimDir, "agent-device.cmd")),
"agent-device-launcher.mjs",
);
}).pipe(
Effect.scoped,
Effect.provideService(HostProcessPlatform, "win32"),
Effect.provideService(HostProcessExecutablePath, process.execPath),
Effect.provideService(HostProcessIsExecutable, false),
Effect.provide(NodeServices.layer),
),
);
2 changes: 1 addition & 1 deletion apps/server/src/device/AgentDeviceShim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const env = { ...process.env };
delete env.AGENT_DEVICE_DAEMON_BASE_URL;
delete env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;
delete env.AGENT_DEVICE_CONFIG;
const child = spawn(${JSON.stringify(node)}, [${JSON.stringify(entryPath)}, ...args], { stdio: "inherit", env });
const child = spawn(${JSON.stringify(node)}, [${JSON.stringify(entryPath)}, ...args], { stdio: "inherit", env, windowsHide: true });
child.on("error", error => { console.error(error.message); process.exitCode = 1; });
child.on("exit", code => { process.exitCode = code ?? 1; });
`,
Expand Down
26 changes: 26 additions & 0 deletions apps/server/src/process/externalLauncher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ it.effect("launches the default browser through the platform command", () => {
);
});

it.effect("sets windowsHide for the Windows browser helper", () => {
let spawned: ChildProcess.StandardCommand | undefined;
return Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
yield* launcher.launchBrowser("https://example.com");
assert.ok(spawned);
assert.equal(spawned.options.windowsHide, true);
assert.equal(spawned.options.detached, true);
assert.equal(spawned.options.shell, false);
assert.include(
Buffer.from(spawned.args.at(-1) ?? "", "base64").toString("utf16le"),
"https://example.com",
);
}).pipe(
Effect.provide(
testLayer({
platform: "win32",
onSpawn: (command) => {
spawned = command;
},
}),
),
);
});

it.effect("launches an installed editor with platform-safe arguments", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
Expand Down Expand Up @@ -338,6 +363,7 @@ it.effect("reveals a file in File Explorer through PowerShell on Windows", () =>
"$ProgressPreference = 'SilentlyContinue'; Start-Process 'explorer.exe' -ArgumentList ('/select,\"' + 'C:\\workspace with spaces\\media\\author''s clip.mp4' + '\"')",
);
assert.equal(spawned.options.shell, false);
assert.equal(spawned.options.windowsHide, true);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);

Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/process/externalLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ interface EditorLaunch {
readonly target: string;
readonly command: string;
readonly args: ReadonlyArray<string>;
readonly hideConsole?: true;
}

interface ProcessLaunch {
Expand Down Expand Up @@ -215,6 +216,7 @@ function resolveWindowsBrowserLaunch(target: string, command: string): ProcessLa
args: [...POWERSHELL_ARGUMENTS_PREFIX, encodedCommand],
options: {
detached: true,
windowsHide: true,
shell: false,
stdin: "ignore",
stdout: "ignore",
Expand Down Expand Up @@ -599,6 +601,7 @@ function fileExplorerRevealLaunch(
...POWERSHELL_ARGUMENTS_PREFIX,
encodeUtf16LeBase64(buildFileExplorerRevealPowerShellSource("explorer.exe", explorerTarget)),
],
hideConsole: true,
};
}

Expand Down Expand Up @@ -730,6 +733,7 @@ const launchEditorProcess = Effect.fn("externalLauncher.launchEditorProcess")(fu
options: {
detached: true,
shell: spawnCommand.shell,
...(launch.hideConsole === true ? { windowsHide: true } : {}),
stdin: "ignore",
stdout: "ignore",
stderr: "ignore",
Expand Down
44 changes: 44 additions & 0 deletions packages/shared/src/legacyCliLauncher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,50 @@ const hostPlatform = NodeOS.platform();
// oxlint-disable-next-line t3code/no-global-process-runtime -- Match the real executable used by the subprocess.
const hostArch = NodeOS.arch();

it("sets windowsHide and forwards arguments and exit status in the generated launcher", async () => {
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-legacy-launcher-"));
try {
const entry = NodePath.join(root, "node_modules/t3/dist/bin.mjs");
const packageDir = NodePath.join(root, `node_modules/@t3code/t3-${hostPlatform}-${hostArch}`);
const preload = NodePath.join(root, "spawn-fixture.cjs");
await NodeFSP.mkdir(NodePath.dirname(entry), { recursive: true });
await NodeFSP.mkdir(packageDir, { recursive: true });
await NodeFSP.writeFile(NodePath.join(packageDir, "package.json"), "{}");
await NodeFSP.writeFile(entry, legacyCliLauncherScript());
await NodeFSP.writeFile(
preload,
`
const { EventEmitter } = require("node:events");
require("node:child_process").spawn = (command, args, options) => {
const record = { command, args, options };
const child = new EventEmitter();
console.log(JSON.stringify(record));
queueMicrotask(() => child.emit("exit", 23));
return child;
};
require("node:module").syncBuiltinESMExports();
`,
);
const result = NodeChildProcess.spawnSync(
process.execPath,
["--require", preload, entry, "serve", "a path with spaces"],
{
encoding: "utf8",
windowsHide: true,
},
);
expect(result.error).toBeUndefined();
expect(result.status).toBe(23);
expect(JSON.parse(result.stdout)).toEqual({
command: NodePath.join(packageDir, hostPlatform === "win32" ? "t3.exe" : "t3"),
args: ["serve", "a path with spaces"],
options: { stdio: "inherit", windowsHide: true },
});
} finally {
await NodeFSP.rm(root, { recursive: true, force: true });
}
});

// The fixture executable uses a POSIX shebang. The wrapper itself also runs on Windows.
it.skipIf(hostPlatform === "win32")(
"keeps service IPC, arguments, and termination connected",
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/legacyCliLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const executable = join(dirname(require.resolve("@t3code/t3-" + process.platform
const ipc = process.send !== undefined;
const child = spawn(executable, process.argv.slice(2), {
stdio: ipc ? ["inherit", "inherit", "inherit", "ipc"] : "inherit",
windowsHide: true,
});
const fail = (error) => {
if (!error) return;
Expand Down
6 changes: 4 additions & 2 deletions packages/shared/src/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe("readEnvironmentFromWindowsShell", () => {
expect(execFile).toHaveBeenCalledWith(
"pwsh.exe",
expect.arrayContaining(["-NoLogo", "-NoProfile", "-NonInteractive", "-Command"]),
{ encoding: "utf8", timeout: 5000 },
{ encoding: "utf8", timeout: 5000, windowsHide: true },
);
});

Expand Down Expand Up @@ -252,7 +252,7 @@ describe("readEnvironmentFromWindowsShell", () => {
expect(execFile).toHaveBeenCalledWith(
"pwsh.exe",
expect.arrayContaining(["-NoLogo", "-NonInteractive", "-Command"]),
{ encoding: "utf8", timeout: 5000 },
{ encoding: "utf8", timeout: 5000, windowsHide: true },
);
expect(execFile.mock.calls[0]?.[1]).not.toContain("-NoProfile");
});
Expand All @@ -277,10 +277,12 @@ describe("readEnvironmentFromWindowsShell", () => {
expect(execFile).toHaveBeenNthCalledWith(1, "pwsh.exe", expect.any(Array), {
encoding: "utf8",
timeout: 5000,
windowsHide: true,
});
expect(execFile).toHaveBeenNthCalledWith(2, "powershell.exe", expect.any(Array), {
encoding: "utf8",
timeout: 5000,
windowsHide: true,
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const WINDOWS_SHELL_CANDIDATES = ["pwsh.exe", "powershell.exe"] as const;
type ExecFileSyncLike = (
file: string,
args: ReadonlyArray<string>,
options: { encoding: "utf8"; timeout: number },
options: { encoding: "utf8"; timeout: number; windowsHide?: true },
) => string;

function canExecuteFile(filePath: string): boolean {
Expand Down Expand Up @@ -370,7 +370,7 @@ export function readEnvironmentFromWindowsShell(
];
for (const shell of WINDOWS_SHELL_CANDIDATES) {
try {
const output = execFile(shell, args, { encoding: "utf8", timeout: 5000 });
const output = execFile(shell, args, { encoding: "utf8", timeout: 5000, windowsHide: true });

const environment: Partial<Record<string, string>> = {};
for (const name of names) {
Expand Down
Loading