Skip to content

Commit 33af5f7

Browse files
Kartik Rajaeschli
andauthored
Reset stored info if we detect an environment was modified (#20038)
Closes microsoft#20028 Co-authored-by: Martin Aeschlimann <martinae@microsoft.com>
1 parent d3572c5 commit 33af5f7

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

src/client/pythonEnvironments/base/info/environmentInfoService.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
import { Uri } from 'vscode';
45
import { IDisposableRegistry } from '../../../common/types';
56
import { createDeferred, Deferred } from '../../../common/utils/async';
67
import { createRunningWorkerPool, IWorkerPool, QueuePosition } from '../../../common/utils/workerPool';
@@ -20,10 +21,20 @@ export enum EnvironmentInfoServiceQueuePriority {
2021
}
2122

2223
export interface IEnvironmentInfoService {
24+
/**
25+
* Get the interpreter information for the given environment.
26+
* @param env The environment to get the interpreter information for.
27+
* @param priority The priority of the request.
28+
*/
2329
getEnvironmentInfo(
2430
env: PythonEnvInfo,
2531
priority?: EnvironmentInfoServiceQueuePriority,
2632
): Promise<InterpreterInformation | undefined>;
33+
/**
34+
* Reset any stored interpreter information for the given environment.
35+
* @param searchLocation Search location of the environment.
36+
*/
37+
resetInfo(searchLocation: Uri): void;
2738
}
2839

2940
async function buildEnvironmentInfo(env: PythonEnvInfo): Promise<InterpreterInformation | undefined> {
@@ -154,6 +165,16 @@ class EnvironmentInfoService implements IEnvironmentInfoService {
154165
}
155166
return r;
156167
}
168+
169+
public resetInfo(searchLocation: Uri): void {
170+
const searchLocationPath = searchLocation.fsPath;
171+
const keys = Array.from(this.cache.keys());
172+
keys.forEach((key) => {
173+
if (key.startsWith(normCasePath(searchLocationPath))) {
174+
this.cache.delete(key);
175+
}
176+
});
177+
}
157178
}
158179

159180
function addToQueue(

src/client/pythonEnvironments/base/locators/common/resourceBasedLocator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export abstract class LazyResourceBasedLocator extends Locator<BasicEnvInfo> imp
125125
this.watchersReady = createDeferred<void>();
126126

127127
// Don't create any file watchers in a virtual workspace.
128-
if (!(await isVirtualWorkspace())) {
128+
if (!isVirtualWorkspace()) {
129129
await this.initWatchers().catch((ex) => {
130130
traceError(ex);
131131
this.watchersReady?.reject(ex);

src/client/pythonEnvironments/base/locators/composite/envsResolver.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ export class PythonEnvsResolver implements IResolvingLocator {
3737
constructor(
3838
private readonly parentLocator: ICompositeLocator<BasicEnvInfo>,
3939
private readonly environmentInfoService: IEnvironmentInfoService,
40-
) {}
40+
) {
41+
this.parentLocator.onChanged((event) => {
42+
if (event.type && event.searchLocation !== undefined) {
43+
// We detect an environment changed, reset any stored info for it so it can be re-run.
44+
this.environmentInfoService.resetInfo(event.searchLocation);
45+
}
46+
});
47+
}
4148

4249
public async resolveEnv(path: string): Promise<PythonEnvInfo | undefined> {
4350
const [executablePath, envPath] = await getExecutablePathAndEnvPath(path);

src/client/pythonEnvironments/common/externalDependencies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function exec(file: string, args: string[], options: SpawnOptions =
3030

3131
// Workspace
3232

33-
export async function isVirtualWorkspace(): Promise<boolean> {
33+
export function isVirtualWorkspace(): boolean {
3434
const service = internalServiceContainer.get<IWorkspaceService>(IWorkspaceService);
3535
return service.isVirtualWorkspace;
3636
}

src/client/pythonEnvironments/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,15 @@ import { IDisposable } from '../common/types';
3838
* Set up the Python environments component (during extension activation).'
3939
*/
4040
export async function initialize(ext: ExtensionState): Promise<IDiscoveryAPI> {
41-
const api = await createPythonEnvironments(() => createLocator(ext));
42-
43-
// Any other initialization goes here.
44-
41+
// Set up the legacy IOC container before api is created.
4542
initializeLegacyExternalDependencies(ext.legacyIOC.serviceContainer);
43+
44+
const api = await createPythonEnvironments(() => createLocator(ext));
4645
registerNewDiscoveryForIOC(
4746
// These are what get wrapped in the legacy adapter.
4847
ext.legacyIOC.serviceManager,
4948
api,
5049
);
51-
5250
return api;
5351
}
5452

0 commit comments

Comments
 (0)