Skip to content

Commit a45df21

Browse files
authored
Add Cache dir for native locator (microsoft#23853)
1 parent 82015bc commit a45df21

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

src/client/common/persistentState.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
'use strict';
55

6-
import { inject, injectable, named } from 'inversify';
6+
import { inject, injectable, named, optional } from 'inversify';
77
import { Memento } from 'vscode';
88
import { IExtensionSingleActivationService } from '../activation/types';
99
import { traceError } from '../logging';
@@ -19,6 +19,7 @@ import {
1919
} from './types';
2020
import { cache } from './utils/decorators';
2121
import { noop } from './utils/misc';
22+
import { clearCacheDirectory } from '../pythonEnvironments/base/locators/common/nativePythonFinder';
2223

2324
let _workspaceState: Memento | undefined;
2425
const _workspaceKeys: string[] = [];
@@ -126,6 +127,7 @@ export class PersistentStateFactory implements IPersistentStateFactory, IExtensi
126127
@inject(IMemento) @named(GLOBAL_MEMENTO) private globalState: Memento,
127128
@inject(IMemento) @named(WORKSPACE_MEMENTO) private workspaceState: Memento,
128129
@inject(ICommandManager) private cmdManager?: ICommandManager,
130+
@inject(IExtensionContext) @optional() private context?: IExtensionContext,
129131
) {}
130132

131133
public async activate(): Promise<void> {
@@ -180,6 +182,7 @@ export class PersistentStateFactory implements IPersistentStateFactory, IExtensi
180182
}
181183

182184
private async cleanAllPersistentStates(): Promise<void> {
185+
const clearCacheDirPromise = this.context ? clearCacheDirectory(this.context).catch() : Promise.resolve();
183186
await Promise.all(
184187
this._globalKeysStorage.value.map(async (keyContent) => {
185188
const storage = this.createGlobalPersistentState(keyContent.key);
@@ -194,6 +197,7 @@ export class PersistentStateFactory implements IPersistentStateFactory, IExtensi
194197
);
195198
await this._globalKeysStorage.updateValue([]);
196199
await this._workspaceKeysStorage.updateValue([]);
200+
await clearCacheDirPromise;
197201
this.cmdManager?.executeCommand('workbench.action.reloadWindow').then(noop);
198202
}
199203
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import { Disposable, EventEmitter, Event, Uri } from 'vscode';
55
import * as ch from 'child_process';
6+
import * as fs from 'fs-extra';
67
import * as path from 'path';
78
import * as rpc from 'vscode-jsonrpc/node';
89
import { PassThrough } from 'stream';
@@ -18,6 +19,7 @@ import { getUserHomeDir } from '../../../../common/utils/platform';
1819
import { createLogOutputChannel } from '../../../../common/vscodeApis/windowApis';
1920
import { sendNativeTelemetry, NativePythonTelemetry } from './nativePythonTelemetry';
2021
import { NativePythonEnvironmentKind } from './nativePythonUtils';
22+
import type { IExtensionContext } from '../../../../common/types';
2123

2224
const untildify = require('untildify');
2325

@@ -98,7 +100,7 @@ class NativePythonFinderImpl extends DisposableBase implements NativePythonFinde
98100

99101
private readonly outputChannel = this._register(createLogOutputChannel('Python Locator', { log: true }));
100102

101-
constructor() {
103+
constructor(private readonly cacheDirectory?: Uri) {
102104
super();
103105
this.connection = this.start();
104106
void this.configure();
@@ -362,7 +364,7 @@ class NativePythonFinderImpl extends DisposableBase implements NativePythonFinde
362364
environmentDirectories: getCustomVirtualEnvDirs(),
363365
condaExecutable: getPythonSettingAndUntildify<string>(CONDAPATH_SETTING_KEY),
364366
poetryExecutable: getPythonSettingAndUntildify<string>('poetryPath'),
365-
// We don't use pipenvPath as it is not used for discovery
367+
cacheDirectory: this.cacheDirectory?.fsPath,
366368
};
367369
// No need to send a configuration request, is there are no changes.
368370
if (JSON.stringify(options) === JSON.stringify(this.lastConfiguration || {})) {
@@ -418,9 +420,19 @@ function getPythonSettingAndUntildify<T>(name: string, scope?: Uri): T | undefin
418420
}
419421

420422
let _finder: NativePythonFinder | undefined;
421-
export function getNativePythonFinder(): NativePythonFinder {
423+
export function getNativePythonFinder(context?: IExtensionContext): NativePythonFinder {
422424
if (!_finder) {
423-
_finder = new NativePythonFinderImpl();
425+
const cacheDirectory = context ? getCacheDirectory(context) : undefined;
426+
_finder = new NativePythonFinderImpl(cacheDirectory);
424427
}
425428
return _finder;
426429
}
430+
431+
export function getCacheDirectory(context: IExtensionContext): Uri {
432+
return Uri.joinPath(context.globalStorageUri, 'pythonLocator');
433+
}
434+
435+
export async function clearCacheDirectory(context: IExtensionContext): Promise<void> {
436+
const cacheDirectory = getCacheDirectory(context);
437+
await fs.emptyDir(cacheDirectory.fsPath).catch(noop);
438+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { Conda, CONDAPATH_SETTING_KEY, isCondaEnvironment } from '../../../commo
3838
import { getConfiguration } from '../../../../common/vscodeApis/workspaceApis';
3939
import { getUserHomeDir } from '../../../../common/utils/platform';
4040
import { categoryToKind } from '../common/nativePythonUtils';
41+
import type { IExtensionContext } from '../../../../common/types';
4142

4243
/**
4344
* A service which maintains the collection of known environments.
@@ -57,7 +58,7 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
5758

5859
private readonly progress = new EventEmitter<ProgressNotificationEvent>();
5960

60-
private nativeFinder = getNativePythonFinder();
61+
private readonly nativeFinder: NativePythonFinder;
6162

6263
public refreshState = ProgressReportStage.discoveryFinished;
6364

@@ -70,8 +71,13 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
7071
return this.progressPromises.get(stage)?.promise;
7172
}
7273

73-
constructor(private readonly cache: IEnvsCollectionCache, private readonly locator: IResolvingLocator) {
74+
constructor(
75+
private readonly cache: IEnvsCollectionCache,
76+
private readonly locator: IResolvingLocator,
77+
context?: IExtensionContext,
78+
) {
7479
super();
80+
this.nativeFinder = getNativePythonFinder(context);
7581
this.locator.onChanged((event) => {
7682
const query: PythonLocatorQuery | undefined = event.providerId
7783
? { providerId: event.providerId, envPath: event.envPath }

src/client/pythonEnvironments/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function initialize(ext: ExtensionState): Promise<IDiscoveryAPI> {
5959
initializeLegacyExternalDependencies(ext.legacyIOC.serviceContainer);
6060

6161
if (shouldUseNativeLocator()) {
62-
const finder = getNativePythonFinder();
62+
const finder = getNativePythonFinder(ext.context);
6363
ext.disposables.push(finder);
6464
const api = createNativeEnvironmentsApi(finder);
6565
ext.disposables.push(api);
@@ -153,6 +153,7 @@ async function createLocator(
153153
await createCollectionCache(ext),
154154
// This is shared.
155155
resolvingLocator,
156+
ext.context,
156157
);
157158
return caching;
158159
}

0 commit comments

Comments
 (0)