1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
4
+ import * as path from 'path' ;
4
5
import { 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' ;
6
8
import { IExtensionActivationService } from '../../activation/types' ;
7
9
import { IApplicationShell , IApplicationEnvironment , IWorkspaceService } from '../../common/application/types' ;
8
10
import { inTerminalEnvVarExperiment } from '../../common/experiments/helpers' ;
@@ -22,6 +24,7 @@ import { traceDecoratorVerbose, traceVerbose } from '../../logging';
22
24
import { IInterpreterService } from '../contracts' ;
23
25
import { defaultShells } from './service' ;
24
26
import { IEnvironmentActivationService } from './types' ;
27
+ import { EnvironmentType } from '../../pythonEnvironments/info' ;
25
28
26
29
@injectable ( )
27
30
export class TerminalEnvVarCollectionService implements IExtensionActivationService {
@@ -53,6 +56,17 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
53
56
public async activate ( resource : Resource ) : Promise < void > {
54
57
if ( ! inTerminalEnvVarExperiment ( this . experimentService ) ) {
55
58
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
+ }
56
70
return ;
57
71
}
58
72
if ( ! this . registeredOnce ) {
@@ -82,14 +96,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
82
96
}
83
97
84
98
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 ) ;
93
100
const settings = this . configurationService . getSettings ( resource ) ;
94
101
if ( ! settings . terminal . activateEnvironment ) {
95
102
traceVerbose ( 'Activating environments in terminal is disabled for' , resource ?. fsPath ) ;
@@ -143,6 +150,37 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
143
150
} ) ;
144
151
}
145
152
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
+
146
184
@traceDecoratorVerbose ( 'Display activating terminals' )
147
185
private showProgress ( ) : void {
148
186
if ( ! this . deferred ) {
0 commit comments