Skip to content

Add -Login startup option #2400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 10, 2020
Merged
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
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,16 @@
"description": "Specifies whether you should be prompted to update your version of PowerShell.",
"default": true
},
"powershell.startAsLoginShell.osx": {
"type": "boolean",
"default": true,
"description": "Starts the PowerShell extension's underlying PowerShell process as a login shell, if applicable."
},
"powershell.startAsLoginShell.linux": {
"type": "boolean",
"default": false,
"description": "Starts the PowerShell extension's underlying PowerShell process as a login shell, if applicable."
},
"powershell.startAutomatically": {
"type": "boolean",
"default": true,
Expand Down Expand Up @@ -735,11 +745,6 @@
"type": "array",
"default": null,
"description": "An array of strings that enable experimental features in the PowerShell extension."
},
"powershell.developer.powerShellExeIsWindowsDevBuild": {
"type": "boolean",
"default": false,
"description": "Indicates that the powerShellExePath points to a developer build of Windows PowerShell and configures it for development."
}
}
},
Expand Down
55 changes: 31 additions & 24 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,19 @@ export class PowerShellProcess {
this.startArgs += "-UseLegacyReadLine";
}

const powerShellArgs = [
"-NoProfile",
"-NonInteractive",
];
const powerShellArgs = [];

const useLoginShell: boolean =
(utils.isMacOS && this.sessionSettings.startAsLoginShell.osx)
|| (utils.isLinux && this.sessionSettings.startAsLoginShell.linux);

if (useLoginShell && this.isLoginShell(this.exePath)) {
// This MUST be the first argument.
powerShellArgs.push("-Login");
}

powerShellArgs.push("-NoProfile");
powerShellArgs.push("-NonInteractive");

// Only add ExecutionPolicy param on Windows
if (utils.isWindows) {
Expand All @@ -88,27 +97,11 @@ export class PowerShellProcess {
Buffer.from(startEditorServices, "utf16le").toString("base64"));
}

let powerShellExePath = this.exePath;

if (this.sessionSettings.developer.powerShellExeIsWindowsDevBuild) {
// Windows PowerShell development builds need the DEVPATH environment
// variable set to the folder where development binaries are held

// NOTE: This batch file approach is needed temporarily until VS Code's
// createTerminal API gets an argument for setting environment variables
// on the launched process.
const batScriptPath = path.resolve(__dirname, "../../sessions/powershell.bat");
fs.writeFileSync(
batScriptPath,
`@set DEVPATH=${path.dirname(powerShellExePath)}\r\n@${powerShellExePath} %*`);

powerShellExePath = batScriptPath;
}

this.log.write(
"Language server starting --",
" exe: " + powerShellExePath,
" args: " + startEditorServices);
" PowerShell executable: " + this.exePath,
" PowerShell args: " + powerShellArgs.join(" "),
" PowerShell Editor Services args: " + startEditorServices);

// Make sure no old session file exists
utils.deleteSessionFile(this.sessionFilePath);
Expand All @@ -117,7 +110,7 @@ export class PowerShellProcess {
this.consoleTerminal =
vscode.window.createTerminal(
this.title,
powerShellExePath,
this.exePath,
powerShellArgs);

if (this.sessionSettings.integratedConsole.showOnStartup) {
Expand Down Expand Up @@ -178,4 +171,18 @@ export class PowerShellProcess {
this.consoleTerminal = undefined;
}
}

private isLoginShell(pwshPath: string): boolean {
try {
// We can't know what version of PowerShell we have without running it
// So we try to start PowerShell with -Login
// If it exits successfully, we return true
// If it exits unsuccessfully, node throws, we catch, and return false
cp.execFileSync(pwshPath, ["-Login", "-NoProfile", "-NoLogo", "-Command", "exit 0"]);
} catch {
return false;
}

return true;
}
}
20 changes: 18 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export interface IDeveloperSettings {
bundledModulesPath?: string;
editorServicesLogLevel?: string;
editorServicesWaitForDebugger?: boolean;
powerShellExeIsWindowsDevBuild?: boolean;
}

export interface ISettings {
Expand All @@ -82,6 +81,7 @@ export interface ISettings {
powerShellExePath?: string;
promptToUpdatePowerShell?: boolean;
bundledModulesPath?: string;
startAsLoginShell?: IStartAsLoginShellSettings;
startAutomatically?: boolean;
useX86Host?: boolean;
enableProfileLoading?: boolean;
Expand All @@ -96,6 +96,11 @@ export interface ISettings {
sideBar?: ISideBarSettings;
}

export interface IStartAsLoginShellSettings {
osx?: boolean;
linux?: boolean;
}

export interface IIntegratedConsoleSettings {
showOnStartup?: boolean;
focusConsoleOnExecute?: boolean;
Expand Down Expand Up @@ -130,7 +135,6 @@ export function load(): ISettings {
bundledModulesPath: "../../../PowerShellEditorServices/module",
editorServicesLogLevel: "Normal",
editorServicesWaitForDebugger: false,
powerShellExeIsWindowsDevBuild: false,
};

const defaultCodeFoldingSettings: ICodeFoldingSettings = {
Expand All @@ -156,6 +160,11 @@ export function load(): ISettings {
useCorrectCasing: false,
};

const defaultStartAsLoginShellSettings: IStartAsLoginShellSettings = {
osx: true,
linux: false,
};

const defaultIntegratedConsoleSettings: IIntegratedConsoleSettings = {
showOnStartup: true,
focusConsoleOnExecute: true,
Expand Down Expand Up @@ -202,6 +211,13 @@ export function load(): ISettings {
configuration.get<IBugReportingSettings>("bugReporting", defaultBugReportingSettings),
sideBar:
configuration.get<ISideBarSettings>("sideBar", defaultSideBarSettings),
startAsLoginShell:
// tslint:disable-next-line
// We follow the same convention as VS Code - https://github.com/microsoft/vscode/blob/ff00badd955d6cfcb8eab5f25f3edc86b762f49f/src/vs/workbench/contrib/terminal/browser/terminal.contribution.ts#L105-L107
// "Unlike on Linux, ~/.profile is not sourced when logging into a macOS session. This
// is the reason terminals on macOS typically run login shells by default which set up
// the environment. See http://unix.stackexchange.com/a/119675/115410"
configuration.get<IStartAsLoginShellSettings>("startAsLoginShell", defaultStartAsLoginShellSettings),
};
}

Expand Down