Skip to content
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

support setting custom $ZDOTDIR in ~/.zshenv when shell integration is enabled #148635

Merged
merged 11 commits into from
May 4, 2022
3 changes: 2 additions & 1 deletion build/gulpfile.reh.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ const serverResources = [
'out-build/vs/workbench/contrib/terminal/browser/media/shellIntegration-bash.sh',
'out-build/vs/workbench/contrib/terminal/browser/media/shellIntegration-env.zsh',
'out-build/vs/workbench/contrib/terminal/browser/media/shellIntegration-profile.zsh',
'out-build/vs/workbench/contrib/terminal/browser/media/shellIntegration.zsh',
'out-build/vs/workbench/contrib/terminal/browser/media/shellIntegration-rc.zsh',
'out-build/vs/workbench/contrib/terminal/browser/media/shellIntegration-login.zsh',

'!**/test/**'
];
Expand Down
6 changes: 5 additions & 1 deletion src/vs/platform/terminal/node/terminalEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export function getShellIntegrationInjection(
envMixin['ZDOTDIR'] = zdotdir;
const filesToCopy: IShellIntegrationConfigInjection['filesToCopy'] = [];
filesToCopy.push({
source: path.join(appRoot, 'out/vs/workbench/contrib/terminal/browser/media/shellIntegration.zsh'),
source: path.join(appRoot, 'out/vs/workbench/contrib/terminal/browser/media/shellIntegration-rc.zsh'),
dest: path.join(zdotdir, '.zshrc')
});
filesToCopy.push({
Expand All @@ -204,6 +204,10 @@ export function getShellIntegrationInjection(
source: path.join(appRoot, 'out/vs/workbench/contrib/terminal/browser/media/shellIntegration-env.zsh'),
dest: path.join(zdotdir, '.zshenv')
});
filesToCopy.push({
source: path.join(appRoot, 'out/vs/workbench/contrib/terminal/browser/media/shellIntegration-login.zsh'),
dest: path.join(zdotdir, '.zlogin')
});
if (!options.showWelcome) {
envMixin['VSCODE_SHELL_HIDE_WELCOME'] = '1';
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/platform/terminal/node/terminalProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class TerminalProcess extends Disposable implements ITerminalChildProcess
if (this.shellLaunchConfig.env?.['_ZDOTDIR'] === '1') {
const zdotdir = path.join(tmpdir(), 'vscode-zsh');
await fs.mkdir(zdotdir, { recursive: true });
const source = path.join(path.dirname(FileAccess.asFileUri('', require).fsPath), 'out/vs/workbench/contrib/terminal/browser/media/shellIntegration.zsh');
const source = path.join(path.dirname(FileAccess.asFileUri('', require).fsPath), 'out/vs/workbench/contrib/terminal/browser/media/shellIntegration-rc.zsh');
await fs.copyFile(source, path.join(zdotdir, '.zshrc'));
this._ptyOptions.env = this._ptyOptions.env || {};
this._ptyOptions.env['ZDOTDIR'] = zdotdir;
Expand Down
11 changes: 7 additions & 4 deletions src/vs/platform/terminal/test/node/terminalEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,25 @@ suite('platform - terminalEnvironment', () => {
suite('zsh', () => {
suite('should override args', () => {
const expectedDir = /.+\/vscode-zsh/;
const expectedDests = [/.+\/vscode-zsh\/.zshrc/, /.+\/vscode-zsh\/.zprofile/, /.+\/vscode-zsh\/.zshenv/];
const expectedDests = [/.+\/vscode-zsh\/.zshrc/, /.+\/vscode-zsh\/.zprofile/, /.+\/vscode-zsh\/.zshenv/, /.+\/vscode-zsh\/.zlogin/];
const expectedSources = [
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration.zsh/,
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration-rc.zsh/,
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration-profile.zsh/,
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration-env.zsh/
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration-env.zsh/,
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration-login.zsh/
];
function assertIsEnabled(result: IShellIntegrationConfigInjection) {
strictEqual(Object.keys(result.envMixin!).length, 1);
ok(result.envMixin!['ZDOTDIR']?.match(expectedDir));
strictEqual(result.filesToCopy?.length, 3);
strictEqual(result.filesToCopy?.length, 4);
ok(result.filesToCopy[0].dest.match(expectedDests[0]));
ok(result.filesToCopy[1].dest.match(expectedDests[1]));
ok(result.filesToCopy[2].dest.match(expectedDests[2]));
ok(result.filesToCopy[3].dest.match(expectedDests[3]));
ok(result.filesToCopy[0].source.match(expectedSources[0]));
ok(result.filesToCopy[1].source.match(expectedSources[1]));
ok(result.filesToCopy[2].source.match(expectedSources[2]));
ok(result.filesToCopy[3].source.match(expectedSources[3]));
}
test('when undefined, []', () => {
const result1 = getShellIntegrationInjection({ executable: 'zsh', args: [] }, enabledProcessOptions, logService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# ---------------------------------------------------------------------------------------------

ORIGINAL_ZDOTDIR=$ZDOTDIR
if [[ -f ~/.zshenv ]]; then
. ~/.zshenv
fi
if [[ "$ZDOTDIR" != "$ORIGINAL_ZDOTDIR" ]]; then
USER_ZDOTDIR=$ZDOTDIR
ZDOTDIR=$ORIGINAL_ZDOTDIR
else
USER_ZDOTDIR=$HOME
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ---------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# ---------------------------------------------------------------------------------------------
if [[ $options[norcs] = off && -o "login" && -f $USER_ZDOTDIR/.zlogin ]]; then
. $USER_ZDOTDIR/.zlogin
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
fi

ZDOTDIR=$USER_ZDOTDIR
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# ---------------------------------------------------------------------------------------------

if [[ $options[norcs] = off && -o "login" && -f ~/.zprofile ]]; then
. ~/.zprofile
if [[ $options[norcs] = off && -o "login" && -f $USER_ZDOTDIR/.zprofile ]]; then
. $USER_ZDOTDIR/.zprofile
fi
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
# ---------------------------------------------------------------------------------------------
builtin autoload -Uz add-zsh-hook

# Now that the init script is running, unset ZDOTDIR to ensure ~/.zlogout runs as expected as well
# as prevent problems that may occur if the user's init scripts depend on ZDOTDIR not being set.
builtin unset ZDOTDIR

# This variable allows the shell to both detect that VS Code's shell integration is enabled as well
# as disable it by unsetting the variable.
VSCODE_SHELL_INTEGRATION=1


if [[ $options[norcs] = off && -f ~/.zshrc ]]; then
. ~/.zshrc
if [[ $options[norcs] = off && -f $USER_ZDOTDIR/.zshrc ]]; then
. $USER_ZDOTDIR/.zshrc
fi

# Shell integration was disabled by the shell, exit without warning assuming either the shell has
Expand Down