Skip to content

Commit d311bc9

Browse files
authored
Remove cli.js argument when running as administrator on Windows (microsoft#296690)
* fix: handle stray cli.js argument when running as administrator on Windows * Handle version subdirectory.
1 parent db8faee commit d311bc9

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/vs/platform/environment/node/argvHelper.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import assert from 'assert';
7-
import { IProcessEnvironment } from '../../../base/common/platform.js';
7+
import { dirname, resolve } from '../../../base/common/path.js';
8+
import { IProcessEnvironment, isWindows } from '../../../base/common/platform.js';
89
import { localize } from '../../../nls.js';
910
import { NativeParsedArgs } from '../common/argv.js';
1011
import { ErrorReporter, NATIVE_CLI_COMMANDS, OPTIONS, parseArgs } from './argv.js';
@@ -63,6 +64,17 @@ function stripAppPath(argv: string[]): string[] | undefined {
6364
export function parseMainProcessArgv(processArgv: string[]): NativeParsedArgs {
6465
let [, ...args] = processArgv;
6566

67+
// When code.exe is configured to 'Run as administrator' on Windows, the CLI launcher (code.cmd) sets ELECTRON_RUN_AS_NODE=1 and passes
68+
// cli.js as the first argument. The elevated process does not inherit the environment variable so Electron starts as a GUI app with cli.js
69+
// as a stray positional argument. Detect and strip it. The path may include a version subdirectory (e.g., 2ca3b2734b\resources\app\out\cli.js).
70+
if (isWindows && args.length > 0) {
71+
const resolvedArg = resolve(args[0]).toLowerCase();
72+
const installDir = dirname(process.execPath).toLowerCase() + '\\';
73+
if (resolvedArg.startsWith(installDir) && resolvedArg.endsWith('\\resources\\app\\out\\cli.js')) {
74+
args.shift();
75+
}
76+
}
77+
6678
// If dev, remove the first non-option argument: it's the app location
6779
if (process.env['VSCODE_DEV']) {
6880
args = stripAppPath(args) || [];

0 commit comments

Comments
 (0)