From fe229588a6626800e26299aa0c27d0be03698016 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Wed, 25 Oct 2023 16:22:19 -0700 Subject: [PATCH] Fix powershell core activation if executing scripts is not supported (#22350) Closes https://github.com/microsoft/vscode-python/issues/22252 --- .../envCollectionActivation/service.ts | 21 +++++++++---------- .../shellIntegration.ts | 1 - 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index b5ce0e5715be..46e70d60d922 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -120,8 +120,10 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ this, this.disposables, ); - const isActive = this.isShellIntegrationActive(); - if (!isActive) { + const { shell } = this.applicationEnvironment; + const isActive = this.isShellIntegrationActive(shell); + const shellType = identifyShellFromShellPath(shell); + if (!isActive && shellType !== TerminalShellType.commandPrompt) { traceWarn(`Shell integration is not active, environment activated maybe overriden by the shell.`); } this.registeredOnce = true; @@ -182,7 +184,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ // PS1 in some cases is a shell variable (not an env variable) so "env" might not contain it, calculate it in that case. env.PS1 = await this.getPS1(shell, resource, env); - const prependOptions = this.getPrependOptions(); + const prependOptions = this.getPrependOptions(shell); // Clear any previously set env vars from collection envVarCollection.clear(); @@ -275,7 +277,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ // PS1 should be set but no PS1 was set. return; } - const config = this.isShellIntegrationActive(); + const config = this.isShellIntegrationActive(shell); if (!config) { traceVerbose('PS1 is not set when shell integration is disabled.'); return; @@ -330,8 +332,8 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ } } - private getPrependOptions(): EnvironmentVariableMutatorOptions { - const isActive = this.isShellIntegrationActive(); + private getPrependOptions(shell: string): EnvironmentVariableMutatorOptions { + const isActive = this.isShellIntegrationActive(shell); // Ideally we would want to prepend exactly once, either at shell integration or process creation. // TODO: Stop prepending altogether once https://github.com/microsoft/vscode/issues/145234 is available. return isActive @@ -345,14 +347,11 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ }; } - private isShellIntegrationActive(): boolean { + private isShellIntegrationActive(shell: string): boolean { const isEnabled = this.workspaceService .getConfiguration('terminal') .get('integrated.shellIntegration.enabled')!; - if ( - isEnabled && - ShellIntegrationShells.includes(identifyShellFromShellPath(this.applicationEnvironment.shell)) - ) { + if (isEnabled && ShellIntegrationShells.includes(identifyShellFromShellPath(shell))) { // Unfortunately shell integration could still've failed in remote scenarios, we can't know for sure: // https://code.visualstudio.com/docs/terminal/shell-integration#_automatic-script-injection return true; diff --git a/src/client/terminals/envCollectionActivation/shellIntegration.ts b/src/client/terminals/envCollectionActivation/shellIntegration.ts index 6b9c25a681a1..1be2501595a4 100644 --- a/src/client/terminals/envCollectionActivation/shellIntegration.ts +++ b/src/client/terminals/envCollectionActivation/shellIntegration.ts @@ -5,7 +5,6 @@ import { TerminalShellType } from '../../common/terminal/types'; * https://code.visualstudio.com/docs/terminal/shell-integration */ export const ShellIntegrationShells = [ - TerminalShellType.commandPrompt, // Shell integration is not supported, but is also not needed to activate the env. TerminalShellType.powershell, TerminalShellType.powershellCore, TerminalShellType.bash,