Fix desktop CLI environment boundary

This commit is contained in:
Mohamed Boudra
2026-04-23 07:08:24 +07:00
parent f65f82bac9
commit 9c5974735e
4 changed files with 38 additions and 8 deletions

View File

@@ -0,0 +1,18 @@
import { describe, expect, it } from "vitest";
import { sanitizeNodeEntrypointEnv } from "./node-entrypoint-runner";
describe("node-entrypoint-runner", () => {
it("removes Electron node-mode env before loading the target entrypoint", () => {
const env = {
ELECTRON_RUN_AS_NODE: "1",
ELECTRON_NO_ATTACH_CONSOLE: "1",
PATH: "/usr/bin",
};
sanitizeNodeEntrypointEnv(env);
expect(env).toEqual({
PATH: "/usr/bin",
});
});
});

View File

@@ -1,6 +1,11 @@
import { pathToFileURL } from "node:url";
async function main(): Promise<void> {
export function sanitizeNodeEntrypointEnv(env: NodeJS.ProcessEnv = process.env): void {
delete env.ELECTRON_RUN_AS_NODE;
delete env.ELECTRON_NO_ATTACH_CONSOLE;
}
export async function main(): Promise<void> {
const [argvMode, entryPath, ...args] = process.argv.slice(2);
if (argvMode !== "bare" && argvMode !== "node-script") {
throw new Error(`Unsupported node entrypoint argv mode: ${argvMode ?? "<missing>"}`);
@@ -13,11 +18,14 @@ async function main(): Promise<void> {
argvMode === "bare"
? [process.argv[0] ?? "node", ...args]
: [process.argv[0] ?? "node", entryPath, ...args];
sanitizeNodeEntrypointEnv();
await import(pathToFileURL(entryPath).href);
}
void main().catch((error) => {
const message = error instanceof Error ? (error.stack ?? error.message) : String(error);
process.stderr.write(`${message}\n`);
process.exit(1);
});
if (require.main === module) {
void main().catch((error) => {
const message = error instanceof Error ? (error.stack ?? error.message) : String(error);
process.stderr.write(`${message}\n`);
process.exit(1);
});
}

View File

@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
import { resolveCliInstallSourcePath } from "./cli-install-path";
describe("cli-install-path", () => {
it("uses the packaged executable on supported unix platforms", () => {
it("uses the bundled shim for packaged macOS installs", () => {
expect(
resolveCliInstallSourcePath({
platform: "darwin",
@@ -10,7 +10,7 @@ describe("cli-install-path", () => {
executablePath: "/Applications/Paseo.app/Contents/MacOS/Paseo",
shimPath: "/Applications/Paseo.app/Contents/Resources/bin/paseo",
}),
).toBe("/Applications/Paseo.app/Contents/MacOS/Paseo");
).toBe("/Applications/Paseo.app/Contents/Resources/bin/paseo");
});
it("prefers the original AppImage path on linux", () => {

View File

@@ -13,6 +13,10 @@ export function resolveCliInstallSourcePath(input: {
return input.shimPath;
}
if (input.platform === "darwin") {
return input.shimPath;
}
if (input.platform === "linux") {
const appImagePath = input.appImagePath?.trim();
if (appImagePath) {