11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4+ import * as path from 'path' ;
45import { inject , injectable } from 'inversify' ;
5- import { ProgressOptions , ProgressLocation , MarkdownString } from 'vscode' ;
6+ import { ProgressOptions , ProgressLocation , MarkdownString , WorkspaceFolder } from 'vscode' ;
7+ import { pathExists } from 'fs-extra' ;
68import { IExtensionActivationService } from '../../activation/types' ;
79import { IApplicationShell , IApplicationEnvironment , IWorkspaceService } from '../../common/application/types' ;
810import { inTerminalEnvVarExperiment } from '../../common/experiments/helpers' ;
@@ -22,6 +24,7 @@ import { traceDecoratorVerbose, traceVerbose } from '../../logging';
2224import { IInterpreterService } from '../contracts' ;
2325import { defaultShells } from './service' ;
2426import { IEnvironmentActivationService } from './types' ;
27+ import { EnvironmentType } from '../../pythonEnvironments/info' ;
2528
2629@injectable ( )
2730export class TerminalEnvVarCollectionService implements IExtensionActivationService {
@@ -53,6 +56,17 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
5356 public async activate ( resource : Resource ) : Promise < void > {
5457 if ( ! inTerminalEnvVarExperiment ( this . experimentService ) ) {
5558 this . context . environmentVariableCollection . clear ( ) ;
59+ await this . handleMicroVenv ( resource ) ;
60+ if ( ! this . registeredOnce ) {
61+ this . interpreterService . onDidChangeInterpreter (
62+ async ( r ) => {
63+ await this . handleMicroVenv ( r ) ;
64+ } ,
65+ this ,
66+ this . disposables ,
67+ ) ;
68+ this . registeredOnce = true ;
69+ }
5670 return ;
5771 }
5872 if ( ! this . registeredOnce ) {
@@ -82,14 +96,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
8296 }
8397
8498 public async _applyCollection ( resource : Resource , shell = this . applicationEnvironment . shell ) : Promise < void > {
85- let workspaceFolder = this . workspaceService . getWorkspaceFolder ( resource ) ;
86- if (
87- ! workspaceFolder &&
88- Array . isArray ( this . workspaceService . workspaceFolders ) &&
89- this . workspaceService . workspaceFolders . length > 0
90- ) {
91- [ workspaceFolder ] = this . workspaceService . workspaceFolders ;
92- }
99+ const workspaceFolder = this . getWorkspaceFolder ( resource ) ;
93100 const settings = this . configurationService . getSettings ( resource ) ;
94101 if ( ! settings . terminal . activateEnvironment ) {
95102 traceVerbose ( 'Activating environments in terminal is disabled for' , resource ?. fsPath ) ;
@@ -143,6 +150,37 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
143150 } ) ;
144151 }
145152
153+ private async handleMicroVenv ( resource : Resource ) {
154+ const workspaceFolder = this . getWorkspaceFolder ( resource ) ;
155+ const interpreter = await this . interpreterService . getActiveInterpreter ( resource ) ;
156+ if ( interpreter ?. envType === EnvironmentType . Venv ) {
157+ const activatePath = path . join ( path . dirname ( interpreter . path ) , 'activate' ) ;
158+ if ( ! ( await pathExists ( activatePath ) ) ) {
159+ this . context . environmentVariableCollection . replace (
160+ 'PATH' ,
161+ `${ path . dirname ( interpreter . path ) } ${ path . delimiter } ${ process . env . Path } ` ,
162+ {
163+ workspaceFolder,
164+ } ,
165+ ) ;
166+ return ;
167+ }
168+ }
169+ this . context . environmentVariableCollection . clear ( ) ;
170+ }
171+
172+ private getWorkspaceFolder ( resource : Resource ) : WorkspaceFolder | undefined {
173+ let workspaceFolder = this . workspaceService . getWorkspaceFolder ( resource ) ;
174+ if (
175+ ! workspaceFolder &&
176+ Array . isArray ( this . workspaceService . workspaceFolders ) &&
177+ this . workspaceService . workspaceFolders . length > 0
178+ ) {
179+ [ workspaceFolder ] = this . workspaceService . workspaceFolders ;
180+ }
181+ return workspaceFolder ;
182+ }
183+
146184 @traceDecoratorVerbose ( 'Display activating terminals' )
147185 private showProgress ( ) : void {
148186 if ( ! this . deferred ) {
0 commit comments