33
44import { Disposable , EventEmitter , Event , Uri } from 'vscode' ;
55import * as ch from 'child_process' ;
6+ import * as fs from 'fs-extra' ;
67import * as path from 'path' ;
78import * as rpc from 'vscode-jsonrpc/node' ;
89import { PassThrough } from 'stream' ;
@@ -18,6 +19,7 @@ import { getUserHomeDir } from '../../../../common/utils/platform';
1819import { createLogOutputChannel } from '../../../../common/vscodeApis/windowApis' ;
1920import { sendNativeTelemetry , NativePythonTelemetry } from './nativePythonTelemetry' ;
2021import { NativePythonEnvironmentKind } from './nativePythonUtils' ;
22+ import type { IExtensionContext } from '../../../../common/types' ;
2123
2224const 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
420422let _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+ }
0 commit comments