@@ -31,7 +31,7 @@ import { traceDecoratorVerbose, traceError, traceVerbose, traceWarn } from '../.
31
31
import { IInterpreterService } from '../contracts' ;
32
32
import { defaultShells } from './service' ;
33
33
import { IEnvironmentActivationService , ITerminalEnvVarCollectionService } from './types' ;
34
- import { EnvironmentType } from '../../pythonEnvironments/info' ;
34
+ import { EnvironmentType , PythonEnvironment } from '../../pythonEnvironments/info' ;
35
35
import { getSearchPathEnvVarNames } from '../../common/utils/exec' ;
36
36
import { EnvironmentVariables } from '../../common/variables/types' ;
37
37
import { TerminalShellType } from '../../common/terminal/types' ;
@@ -44,6 +44,15 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
44
44
virtualWorkspace : false ,
45
45
} ;
46
46
47
+ /**
48
+ * Prompts for these shells cannot be set reliably using variables
49
+ */
50
+ private noPromptVariableShells = [
51
+ TerminalShellType . powershell ,
52
+ TerminalShellType . powershellCore ,
53
+ TerminalShellType . fish ,
54
+ ] ;
55
+
47
56
private deferred : Deferred < void > | undefined ;
48
57
49
58
private registeredOnce = false ;
@@ -150,6 +159,10 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
150
159
) ;
151
160
}
152
161
const processEnv = this . processEnvVars ;
162
+
163
+ // PS1 in some cases is a shell variable (not an env variable) so "env" might not contain it, calculate it in that case.
164
+ env . PS1 = await this . getPS1 ( shell , resource , env ) ;
165
+
153
166
Object . keys ( env ) . forEach ( ( key ) => {
154
167
if ( shouldSkip ( key ) ) {
155
168
return ;
@@ -213,15 +226,8 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
213
226
this . terminalPromptIsCorrect ( resource ) ;
214
227
return ;
215
228
}
216
- // Prompts for these shells cannot be set reliably using variables
217
- const exceptionShells = [
218
- TerminalShellType . powershell ,
219
- TerminalShellType . powershellCore ,
220
- TerminalShellType . fish ,
221
- TerminalShellType . zsh , // TODO: Remove this once https://github.com/microsoft/vscode/issues/188875 is fixed
222
- ] ;
223
229
const customShellType = identifyShellFromShellPath ( shell ) ;
224
- if ( exceptionShells . includes ( customShellType ) ) {
230
+ if ( this . noPromptVariableShells . includes ( customShellType ) ) {
225
231
return ;
226
232
}
227
233
if ( this . platform . osType !== OSType . Windows ) {
@@ -243,6 +249,26 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
243
249
this . terminalPromptIsCorrect ( resource ) ;
244
250
}
245
251
252
+ private async getPS1 ( shell : string , resource : Resource , env : EnvironmentVariables ) {
253
+ if ( env . PS1 ) {
254
+ return env . PS1 ;
255
+ }
256
+ const customShellType = identifyShellFromShellPath ( shell ) ;
257
+ if ( this . noPromptVariableShells . includes ( customShellType ) ) {
258
+ return undefined ;
259
+ }
260
+ if ( this . platform . osType !== OSType . Windows ) {
261
+ // These shells are expected to set PS1 variable for terminal prompt for virtual/conda environments.
262
+ const interpreter = await this . interpreterService . getActiveInterpreter ( resource ) ;
263
+ const shouldPS1BeSet = interpreter ?. type !== undefined ;
264
+ if ( shouldPS1BeSet && ! env . PS1 ) {
265
+ // PS1 should be set but no PS1 was set.
266
+ return getPromptForEnv ( interpreter ) ;
267
+ }
268
+ }
269
+ return undefined ;
270
+ }
271
+
246
272
private async handleMicroVenv ( resource : Resource ) {
247
273
try {
248
274
const workspaceFolder = this . getWorkspaceFolder ( resource ) ;
@@ -313,3 +339,16 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
313
339
function shouldSkip ( env : string ) {
314
340
return [ '_' , 'SHLVL' ] . includes ( env ) ;
315
341
}
342
+
343
+ function getPromptForEnv ( interpreter : PythonEnvironment | undefined ) {
344
+ if ( ! interpreter ) {
345
+ return undefined ;
346
+ }
347
+ if ( interpreter . envName ) {
348
+ return `(${ interpreter . envName } ) ` ;
349
+ }
350
+ if ( interpreter . envPath ) {
351
+ return `(${ path . basename ( interpreter . envPath ) } ) ` ;
352
+ }
353
+ return undefined ;
354
+ }
0 commit comments