Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 193b929

Browse files
Use Native Python Finder as the main locator (#23823)
Closes #23719 --------- Co-authored-by: Don Jayamanne <[email protected]>
1 parent 5d8f514 commit 193b929

File tree

10 files changed

+687
-23
lines changed

10 files changed

+687
-23
lines changed

src/client/environmentApi.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Architecture } from './common/utils/platform';
99
import { IServiceContainer } from './ioc/types';
1010
import { PythonEnvInfo, PythonEnvKind, PythonEnvType } from './pythonEnvironments/base/info';
1111
import { getEnvPath } from './pythonEnvironments/base/info/env';
12-
import { IDiscoveryAPI } from './pythonEnvironments/base/locator';
12+
import { IDiscoveryAPI, ProgressReportStage } from './pythonEnvironments/base/locator';
1313
import { IPythonExecutionFactory } from './common/process/types';
1414
import { traceError, traceVerbose } from './logging';
1515
import { isParentPath, normCasePath } from './common/platform/fs-paths';
@@ -147,6 +147,11 @@ export function buildEnvironmentApi(
147147
.ignoreErrors();
148148
}
149149
disposables.push(
150+
discoveryApi.onProgress((e) => {
151+
if (e.stage === ProgressReportStage.discoveryFinished) {
152+
knownCache = initKnownCache();
153+
}
154+
}),
150155
discoveryApi.onChanged((e) => {
151156
const env = e.new ?? e.old;
152157
if (!env || !filterUsingVSCodeContext(env)) {

src/client/interpreter/display/progressDisplay.ts

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export class InterpreterLocatorProgressStatubarHandler implements IExtensionSing
3939
if (refreshPromise) {
4040
refreshPromise.then(() => this.hideProgress());
4141
}
42+
} else if (event.stage === ProgressReportStage.discoveryFinished) {
43+
this.hideProgress();
4244
}
4345
},
4446
this,

src/client/pythonEnvironments/base/locator.ts

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export interface IPythonEnvsIterator<I = PythonEnvInfo> extends IAsyncIterableIt
6868
}
6969

7070
export enum ProgressReportStage {
71+
idle = 'idle',
7172
discoveryStarted = 'discoveryStarted',
7273
allPathsDiscovered = 'allPathsDiscovered',
7374
discoveryFinished = 'discoveryFinished',

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { Disposable, EventEmitter, Event, Uri } from 'vscode';
4+
import { Disposable, EventEmitter, Event, Uri, LogOutputChannel } from 'vscode';
55
import * as ch from 'child_process';
66
import * as path from 'path';
77
import * as rpc from 'vscode-jsonrpc/node';
@@ -29,7 +29,7 @@ export interface NativeEnvInfo {
2929
displayName?: string;
3030
name?: string;
3131
executable?: string;
32-
kind: string;
32+
kind?: string;
3333
version?: string;
3434
prefix?: string;
3535
manager?: NativeEnvManagerInfo;
@@ -57,10 +57,11 @@ export type NativeCondaInfo = {
5757
environmentsFromTxt: string[];
5858
};
5959

60-
export interface NativeGlobalPythonFinder extends Disposable {
60+
export interface NativePythonFinder extends Disposable {
6161
resolve(executable: string): Promise<NativeEnvInfo>;
6262
refresh(): AsyncIterable<NativeEnvInfo>;
6363
categoryToKind(category?: string): PythonEnvKind;
64+
logger(): LogOutputChannel;
6465
getCondaInfo(): Promise<NativeCondaInfo>;
6566
find(searchPath: string): Promise<NativeEnvInfo[]>;
6667
}
@@ -70,7 +71,7 @@ interface NativeLog {
7071
message: string;
7172
}
7273

73-
class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGlobalPythonFinder {
74+
class NativeGlobalPythonFinderImpl extends DisposableBase implements NativePythonFinder {
7475
private readonly connection: rpc.MessageConnection;
7576

7677
private firstRefreshResults: undefined | (() => AsyncGenerator<NativeEnvInfo, void, unknown>);
@@ -172,6 +173,10 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
172173
}
173174
}
174175

176+
logger(): LogOutputChannel {
177+
return this.outputChannel;
178+
}
179+
175180
refreshFirstTime() {
176181
const result = this.doRefresh();
177182
const completed = createDeferredFrom(result.completed);
@@ -304,7 +309,6 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
304309
disposable.add(
305310
this.connection.onNotification('environment', (data: NativeEnvInfo) => {
306311
this.outputChannel.info(`Discovered env: ${data.executable || data.prefix}`);
307-
this.outputChannel.trace(`Discovered env info:\n ${JSON.stringify(data, undefined, 4)}`);
308312
// We know that in the Python extension if either Version of Prefix is not provided by locator
309313
// Then we end up resolving the information.
310314
// Lets do that here,
@@ -321,7 +325,6 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
321325
})
322326
.then((environment) => {
323327
this.outputChannel.info(`Resolved ${environment.executable}`);
324-
this.outputChannel.trace(`Environment resolved:\n ${JSON.stringify(data, undefined, 4)}`);
325328
discovered.fire(environment);
326329
})
327330
.catch((ex) => this.outputChannel.error(`Error in Resolving ${JSON.stringify(data)}`, ex));
@@ -419,6 +422,10 @@ function getPythonSettingAndUntildify<T>(name: string, scope?: Uri): T | undefin
419422
return value;
420423
}
421424

422-
export function createNativeGlobalPythonFinder(): NativeGlobalPythonFinder {
423-
return new NativeGlobalPythonFinderImpl();
425+
let _finder: NativePythonFinder | undefined;
426+
export function getNativePythonFinder(): NativePythonFinder {
427+
if (!_finder) {
428+
_finder = new NativeGlobalPythonFinderImpl();
429+
}
430+
return _finder;
424431
}

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ import {
2525
import { getQueryFilter } from '../../locatorUtils';
2626
import { PythonEnvCollectionChangedEvent, PythonEnvsWatcher } from '../../watcher';
2727
import { IEnvsCollectionCache } from './envsCollectionCache';
28-
import {
29-
createNativeGlobalPythonFinder,
30-
NativeEnvInfo,
31-
NativeGlobalPythonFinder as NativePythonFinder,
32-
} from '../common/nativePythonFinder';
28+
import { getNativePythonFinder, NativeEnvInfo, NativePythonFinder } from '../common/nativePythonFinder';
3329
import { pathExists } from '../../../../common/platform/fs-paths';
3430
import { noop } from '../../../../common/utils/misc';
3531
import { parseVersion } from '../../info/pythonVersion';
@@ -55,7 +51,7 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
5551

5652
private readonly progress = new EventEmitter<ProgressNotificationEvent>();
5753

58-
private nativeFinder = createNativeGlobalPythonFinder();
54+
private nativeFinder = getNativePythonFinder();
5955

6056
public refreshState = ProgressReportStage.discoveryFinished;
6157

src/client/pythonEnvironments/base/locators/lowLevel/nativeLocator.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Conda } from '../../../common/environmentManagers/conda';
1010
import { traceError } from '../../../../logging';
1111
import type { KnownEnvironmentTools } from '../../../../api/types';
1212
import { setPyEnvBinary } from '../../../common/environmentManagers/pyenv';
13-
import { NativeGlobalPythonFinder, createNativeGlobalPythonFinder } from '../common/nativePythonFinder';
13+
import { NativePythonFinder, getNativePythonFinder } from '../common/nativePythonFinder';
1414
import { disposeAll } from '../../../../common/utils/resourceLifecycle';
1515
import { Architecture } from '../../../../common/utils/platform';
1616

@@ -54,11 +54,11 @@ export class NativeLocator implements ILocator<BasicEnvInfo>, IDisposable {
5454

5555
private readonly disposables: IDisposable[] = [];
5656

57-
private readonly finder: NativeGlobalPythonFinder;
57+
private readonly finder: NativePythonFinder;
5858

5959
constructor() {
6060
this.onChanged = this.onChangedEmitter.event;
61-
this.finder = createNativeGlobalPythonFinder();
61+
this.finder = getNativePythonFinder();
6262
this.disposables.push(this.onChangedEmitter, this.finder);
6363
}
6464

src/client/pythonEnvironments/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,34 @@ import { traceError } from '../logging';
4040
import { ActiveStateLocator } from './base/locators/lowLevel/activeStateLocator';
4141
import { CustomWorkspaceLocator } from './base/locators/lowLevel/customWorkspaceLocator';
4242
import { PixiLocator } from './base/locators/lowLevel/pixiLocator';
43+
import { getConfiguration } from '../common/vscodeApis/workspaceApis';
44+
import { getNativePythonFinder } from './base/locators/common/nativePythonFinder';
45+
import { createNativeEnvironmentsApi } from './nativeAPI';
4346

4447
const PYTHON_ENV_INFO_CACHE_KEY = 'PYTHON_ENV_INFO_CACHEv2';
4548

49+
export function shouldUseNativeLocator(): boolean {
50+
const config = getConfiguration('python');
51+
return config.get<string>('locator', 'js') === 'native';
52+
}
53+
4654
/**
4755
* Set up the Python environments component (during extension activation).'
4856
*/
4957
export async function initialize(ext: ExtensionState): Promise<IDiscoveryAPI> {
5058
// Set up the legacy IOC container before api is created.
5159
initializeLegacyExternalDependencies(ext.legacyIOC.serviceContainer);
5260

61+
if (shouldUseNativeLocator()) {
62+
const finder = getNativePythonFinder();
63+
const api = createNativeEnvironmentsApi(finder);
64+
registerNewDiscoveryForIOC(
65+
// These are what get wrapped in the legacy adapter.
66+
ext.legacyIOC.serviceManager,
67+
api,
68+
);
69+
return api;
70+
}
5371
const api = await createPythonEnvironments(() => createLocator(ext));
5472
registerNewDiscoveryForIOC(
5573
// These are what get wrapped in the legacy adapter.

0 commit comments

Comments
 (0)