Skip to content

Commit

Permalink
Preserve per-window environment variables between workspace changes (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Nov 20, 2020
1 parent 7d1cd1f commit ff1887b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/vs/code/electron-main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import { IKeyboardLayoutMainService, KeyboardLayoutMainService } from 'vs/platfo
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { NativeParsedArgs } from 'vs/platform/environment/common/argv';
import { DisplayMainService, IDisplayMainService } from 'vs/platform/display/electron-main/displayMainService';
import { isLaunchedFromCli } from 'vs/platform/environment/node/argvHelper';

export class CodeApplication extends Disposable {
private windowsMainService: IWindowsMainService | undefined;
Expand Down Expand Up @@ -703,7 +704,7 @@ export class CodeApplication extends Disposable {
// Open our first window
const args = this.environmentService.args;
const macOpenFiles: string[] = (<any>global).macOpenFiles;
const context = !!process.env['VSCODE_CLI'] ? OpenContext.CLI : OpenContext.DESKTOP;
const context = isLaunchedFromCli(process.env) ? OpenContext.CLI : OpenContext.DESKTOP;
const hasCliArgs = args._.length;
const hasFolderURIs = !!args['folder-uri'];
const hasFileURIs = !!args['file-uri'];
Expand Down
11 changes: 11 additions & 0 deletions src/vs/code/electron-main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { ILifecycleMainService } from 'vs/platform/lifecycle/electron-main/lifec
import { IStorageMainService } from 'vs/platform/storage/node/storageMainService';
import { ByteSize, IFileService } from 'vs/platform/files/common/files';
import { FileAccess, Schemas } from 'vs/base/common/network';
import { isLaunchedFromCli } from 'vs/platform/environment/node/argvHelper';

export interface IWindowCreationOptions {
state: IWindowState;
Expand Down Expand Up @@ -677,6 +678,16 @@ export class CodeWindow extends Disposable implements ICodeWindow {

load(config: INativeWindowConfiguration, isReload?: boolean, disableExtensions?: boolean): void {

// If this window was loaded before from the command line
// (as indicated by VSCODE_CLI environment), make sure to
// preserve that user environment in subsequent loads,
// unless the new configuration context was also a CLI
// (for https://github.com/microsoft/vscode/issues/108571)
const currentUserEnv = (this.currentConfig ?? this.pendingLoadConfig)?.userEnv;
if (currentUserEnv && isLaunchedFromCli(currentUserEnv) && !isLaunchedFromCli(config.userEnv)) {
config.userEnv = currentUserEnv;
}

// If this is the first time the window is loaded, we associate the paths
// directly with the window because we assume the loading will just work
if (this._readyState === ReadyState.NONE) {
Expand Down
5 changes: 3 additions & 2 deletions src/vs/code/node/shellEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { generateUuid } from 'vs/base/common/uuid';
import { isWindows } from 'vs/base/common/platform';
import { ILogService } from 'vs/platform/log/common/log';
import { NativeParsedArgs } from 'vs/platform/environment/common/argv';
import { isLaunchedFromCli } from 'vs/platform/environment/node/argvHelper';

/**
* We need to get the environment from a user's shell.
Expand All @@ -31,15 +32,15 @@ export async function resolveShellEnv(logService: ILogService, args: NativeParse
}

// Skip if running from CLI already
else if (env['VSCODE_CLI'] === '1' && !args['force-user-env']) {
else if (isLaunchedFromCli(env) && !args['force-user-env']) {
logService.trace('resolveShellEnv(): skipped (VSCODE_CLI is set)');

return {};
}

// Otherwise resolve (macOS, Linux)
else {
if (env['VSCODE_CLI'] === '1') {
if (isLaunchedFromCli(env)) {
logService.trace('resolveShellEnv(): running (--force-user-env)');
} else {
logService.trace('resolveShellEnv(): running (macOS/Linux)');
Expand Down
8 changes: 6 additions & 2 deletions src/vs/platform/environment/node/argvHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export function parseMainProcessArgv(processArgv: string[]): NativeParsedArgs {
}

// If called from CLI, don't report warnings as they are already reported.
let reportWarnings = !process.env['VSCODE_CLI'];
const reportWarnings = !isLaunchedFromCli(process.env);
return parseAndValidate(args, reportWarnings);
}

/**
* Use this to parse raw code CLI process.argv such as: `Electron cli.js . --verbose --wait`
*/
export function parseCLIProcessArgv(processArgv: string[]): NativeParsedArgs {
let [, , ...args] = processArgv; // remove the first non-option argument: it's always the app location
const [, , ...args] = processArgv; // remove the first non-option argument: it's always the app location

return parseAndValidate(args, true);
}
Expand All @@ -78,3 +78,7 @@ export function addArg(argv: string[], ...args: string[]): string[] {

return argv;
}

export function isLaunchedFromCli(env: NodeJS.ProcessEnv): boolean {
return env['VSCODE_CLI'] === '1';
}
2 changes: 1 addition & 1 deletion src/vs/platform/environment/node/environmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as os from 'os';
import { IDebugParams, IExtensionHostDebugParams, INativeEnvironmentService } from 'vs/platform/environment/common/environment';
import { NativeParsedArgs } from 'vs/platform/environment/common/argv';
import * as paths from 'vs/base/node/paths';
import * as os from 'os';
import * as path from 'vs/base/common/path';
import * as resources from 'vs/base/common/resources';
import { memoize } from 'vs/base/common/decorators';
Expand Down
3 changes: 2 additions & 1 deletion src/vs/platform/launch/electron-main/launchMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { BrowserWindow, ipcMain, Event as IpcEvent, app } from 'electron';
import { coalesce } from 'vs/base/common/arrays';
import { IDiagnosticInfoOptions, IDiagnosticInfo, IRemoteDiagnosticInfo, IRemoteDiagnosticError } from 'vs/platform/diagnostics/common/diagnostics';
import { IMainProcessInfo, IWindowInfo } from 'vs/platform/launch/node/launch';
import { isLaunchedFromCli } from 'vs/platform/environment/node/argvHelper';

export const ID = 'launchMainService';
export const ILaunchMainService = createDecorator<ILaunchMainService>(ID);
Expand Down Expand Up @@ -112,7 +113,7 @@ export class LaunchMainService implements ILaunchMainService {
}

private startOpenWindow(args: NativeParsedArgs, userEnv: IProcessEnvironment): Promise<void> {
const context = !!userEnv['VSCODE_CLI'] ? OpenContext.CLI : OpenContext.DESKTOP;
const context = isLaunchedFromCli(userEnv) ? OpenContext.CLI : OpenContext.DESKTOP;
let usedWindows: ICodeWindow[] = [];

const waitMarkerFileURI = args.wait && args.waitMarkerFilePath ? URI.file(args.waitMarkerFilePath) : undefined;
Expand Down

0 comments on commit ff1887b

Please sign in to comment.