From 4552fccb824d55c625e25bbb4ac43ca9fed8771c Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 31 Aug 2026 02:01:15 -0700 Subject: [PATCH] fix(linux): tighten CLI launch detection --- src/main/startup/cli-launch-redirect.test.ts | 16 ++++++++++++++++ src/main/startup/cli-launch-redirect.ts | 13 +++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/startup/cli-launch-redirect.test.ts b/src/main/startup/cli-launch-redirect.test.ts index 0cb8fd13a89..4ac2394c455 100644 --- a/src/main/startup/cli-launch-redirect.test.ts +++ b/src/main/startup/cli-launch-redirect.test.ts @@ -54,6 +54,16 @@ describe('CLI launch redirect: entry-path form', () => { ) ).toEqual(['status']) }) + + it('does not treat a later positional entrypoint path as the launcher', () => { + expect( + getCliLaunchArgs( + [linux.execPath, 'file', 'open', '--path', linux.cliEntryPath], + linux.cliEntryPath, + linuxOptions + ) + ).toBeNull() + }) }) describe('CLI launch redirect: command form', () => { @@ -110,6 +120,12 @@ describe('CLI launch redirect: command form', () => { ]) }) + it.each(['--version', '-v'])('treats %s as a CLI launch even without a command', (flag) => { + expect(getCliLaunchArgs([linux.execPath, flag], linux.cliEntryPath, linuxOptions)).toEqual([ + flag + ]) + }) + it('leaves a plain desktop launch alone', () => { expect(getCliLaunchArgs([linux.execPath], linux.cliEntryPath, linuxOptions)).toBeNull() expect( diff --git a/src/main/startup/cli-launch-redirect.ts b/src/main/startup/cli-launch-redirect.ts index 4011486d1cc..c033bfa1f59 100644 --- a/src/main/startup/cli-launch-redirect.ts +++ b/src/main/startup/cli-launch-redirect.ts @@ -18,7 +18,7 @@ export type CliLaunchRedirectOptions = { run?: typeof runProcessSync } -const HELP_FLAGS = new Set(['--help', '-h', 'help']) +const CLI_EARLY_EXIT_FLAGS = new Set(['--help', '-h', 'help', '--version', '-v']) const DESKTOP_FLAGS = new Set(['--no-sandbox', '--disable-gpu']) // Fence recursion if a wrapper drops ELECTRON_RUN_AS_NODE again. @@ -95,10 +95,11 @@ function getEntryPathLaunchArgs( platform: NodeJS.Platform ): string[] | null { const expectedCliPath = normalizePathForPlatform(cliEntryPath, platform) - const cliEntryIndex = argv.findIndex( - (arg, index) => index > 0 && normalizePathForPlatform(arg, platform) === expectedCliPath - ) - return cliEntryIndex === -1 ? null : argv.slice(cliEntryIndex + 1) + // The packaged launcher always passes the entrypoint as Electron's first argument. + // Matching later positional arguments can mistake a normal desktop launch for the CLI. + return argv[1] && normalizePathForPlatform(argv[1], platform) === expectedCliPath + ? argv.slice(2) + : null } function getCommandLaunchArgs( @@ -117,7 +118,7 @@ function getCommandLaunchArgs( const cliArgs = args.filter( (arg, index) => (commandIndex !== -1 && index > commandIndex) || !DESKTOP_FLAGS.has(arg) ) - if (cliArgs.some((arg) => HELP_FLAGS.has(arg))) { + if (cliArgs.some((arg) => CLI_EARLY_EXIT_FLAGS.has(arg))) { return cliArgs }