Skip to content

Commit aab1c36

Browse files
authored
Add environment discovery logging and messages when using Python Environments extension (#25830)
should help users find that the python environments extension is installed and may be the item causing problems for those who don't know we are moving to the envs extension for handling discovery
1 parent 3b9c6e9 commit aab1c36

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

src/client/application/diagnostics/checks/pythonInterpreter.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
IDiagnosticHandlerService,
2121
IDiagnosticMessageOnCloseHandler,
2222
} from '../types';
23-
import { Common } from '../../../common/utils/localize';
23+
import { Common, Interpreters } from '../../../common/utils/localize';
2424
import { Commands } from '../../../common/constants';
2525
import { ICommandManager, IWorkspaceService } from '../../../common/application/types';
2626
import { sendTelemetryEvent } from '../../../telemetry';
@@ -30,11 +30,12 @@ import { cache } from '../../../common/utils/decorators';
3030
import { noop } from '../../../common/utils/misc';
3131
import { getEnvironmentVariable, getOSType, OSType } from '../../../common/utils/platform';
3232
import { IFileSystem } from '../../../common/platform/types';
33-
import { traceError } from '../../../logging';
33+
import { traceError, traceWarn } from '../../../logging';
3434
import { getExecutable } from '../../../common/process/internal/python';
3535
import { getSearchPathEnvVarNames } from '../../../common/utils/exec';
3636
import { IProcessServiceFactory } from '../../../common/process/types';
3737
import { normCasePath } from '../../../common/platform/fs-paths';
38+
import { useEnvExtension } from '../../../envExt/api.internal';
3839

3940
const messages = {
4041
[DiagnosticCodes.NoPythonInterpretersDiagnostic]: l10n.t(
@@ -144,6 +145,9 @@ export class InvalidPythonInterpreterService extends BaseDiagnosticsService
144145
const isInterpreterSetToDefault = interpreterPathService.get(resource) === 'python';
145146

146147
if (!hasInterpreters && isInterpreterSetToDefault) {
148+
if (useEnvExtension()) {
149+
traceWarn(Interpreters.envExtDiscoveryNoEnvironments);
150+
}
147151
return [
148152
new InvalidPythonInterpreterDiagnostic(
149153
DiagnosticCodes.NoPythonInterpretersDiagnostic,
@@ -156,6 +160,9 @@ export class InvalidPythonInterpreterService extends BaseDiagnosticsService
156160

157161
const currentInterpreter = await interpreterService.getActiveInterpreter(resource);
158162
if (!currentInterpreter) {
163+
if (useEnvExtension()) {
164+
traceWarn(Interpreters.envExtNoActiveEnvironment);
165+
}
159166
return [
160167
new InvalidPythonInterpreterDiagnostic(
161168
DiagnosticCodes.InvalidPythonInterpreterDiagnostic,

src/client/common/utils/localize.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ export namespace Interpreters {
191191
export const installingPython = l10n.t('Installing Python into Environment...');
192192
export const discovering = l10n.t('Discovering Python Interpreters');
193193
export const refreshing = l10n.t('Refreshing Python Interpreters');
194+
export const envExtDiscoveryAttribution = l10n.t(
195+
'Environment discovery is managed by the Python Environments extension (ms-python.vscode-python-envs). Check the "Python Environments" output channel for environment-specific logs.',
196+
);
197+
export const envExtDiscoveryFailed = l10n.t(
198+
'Environment discovery failed. Check the "Python Environments" output channel for details. The Python Environments extension (ms-python.vscode-python-envs) manages environment discovery.',
199+
);
200+
export const envExtDiscoverySlow = l10n.t(
201+
'Environment discovery is taking longer than expected. Check the "Python Environments" output channel for progress. The Python Environments extension (ms-python.vscode-python-envs) manages environment discovery.',
202+
);
203+
export const envExtActivationFailed = l10n.t(
204+
'Failed to activate the Python Environments extension (ms-python.vscode-python-envs), which is required for environment discovery. Please ensure it is installed and enabled.',
205+
);
206+
export const envExtDiscoveryNoEnvironments = l10n.t(
207+
'Environment discovery completed but no Python environments were found. Check the "Python Environments" output channel for details.',
208+
);
209+
export const envExtNoActiveEnvironment = l10n.t(
210+
'No Python environment is set for this resource. Check the "Python Environments" output channel for details, or select an interpreter.',
211+
);
194212
export const condaInheritEnvMessage = l10n.t(
195213
'We noticed you\'re using a conda environment. If you are experiencing issues with this environment in the integrated terminal, we recommend that you let the Python extension change "terminal.integrated.inheritEnv" to false in your user settings. [Learn more](https://aka.ms/AA66i8f).',
196214
);

src/client/envExt/api.internal.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
} from './types';
1515
import { executeCommand } from '../common/vscodeApis/commandApis';
1616
import { getConfiguration } from '../common/vscodeApis/workspaceApis';
17+
import { traceError, traceLog } from '../logging';
18+
import { Interpreters } from '../common/utils/localize';
1719

1820
export const ENVS_EXTENSION_ID = 'ms-python.vscode-python-envs';
1921

@@ -22,7 +24,8 @@ export function useEnvExtension(): boolean {
2224
if (_useExt !== undefined) {
2325
return _useExt;
2426
}
25-
const inExpSetting = getConfiguration('python').get<boolean>('useEnvironmentsExtension', false);
27+
const config = getConfiguration('python');
28+
const inExpSetting = config?.get<boolean>('useEnvironmentsExtension', false) ?? false;
2629
// If extension is installed and in experiment, then use it.
2730
_useExt = !!getExtension(ENVS_EXTENSION_ID) && inExpSetting;
2831
return _useExt;
@@ -46,12 +49,20 @@ export async function getEnvExtApi(): Promise<PythonEnvironmentApi> {
4649
}
4750
const extension = getExtension(ENVS_EXTENSION_ID);
4851
if (!extension) {
52+
traceError(Interpreters.envExtActivationFailed);
4953
throw new Error('Python Environments extension not found.');
5054
}
5155
if (!extension?.isActive) {
52-
await extension.activate();
56+
try {
57+
await extension.activate();
58+
} catch (ex) {
59+
traceError(Interpreters.envExtActivationFailed, ex);
60+
throw ex;
61+
}
5362
}
5463

64+
traceLog(Interpreters.envExtDiscoveryAttribution);
65+
5566
_extApi = extension.exports as PythonEnvironmentApi;
5667
_extApi.onDidChangeEnvironment((e) => {
5768
onDidChangeEnvironmentEnvExtEmitter.fire(e);
@@ -70,7 +81,11 @@ export async function runInBackground(
7081

7182
export async function getEnvironment(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
7283
const envExtApi = await getEnvExtApi();
73-
return envExtApi.getEnvironment(scope);
84+
const env = await envExtApi.getEnvironment(scope);
85+
if (!env) {
86+
traceLog(Interpreters.envExtNoActiveEnvironment);
87+
}
88+
return env;
7489
}
7590

7691
export async function resolveEnvironment(pythonPath: string): Promise<PythonEnvironment | undefined> {

src/client/envExt/envExtApi.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { PythonEnvCollectionChangedEvent } from '../pythonEnvironments/base/watc
1717
import { getEnvExtApi } from './api.internal';
1818
import { createDeferred, Deferred } from '../common/utils/async';
1919
import { StopWatch } from '../common/utils/stopWatch';
20-
import { traceLog } from '../logging';
20+
import { traceError, traceLog, traceWarn } from '../logging';
2121
import {
2222
DidChangeEnvironmentsEventArgs,
2323
EnvironmentChangeKind,
@@ -27,6 +27,7 @@ import {
2727
import { FileChangeType } from '../common/platform/fileSystemWatcher';
2828
import { Architecture, isWindows } from '../common/utils/platform';
2929
import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
30+
import { Interpreters } from '../common/utils/localize';
3031

3132
function getKind(pythonEnv: PythonEnvironment): PythonEnvKind {
3233
if (pythonEnv.envId.managerId.toLowerCase().endsWith('system')) {
@@ -242,13 +243,23 @@ class EnvExtApis implements IDiscoveryAPI, Disposable {
242243
this._onProgress.fire({ stage: this.refreshState });
243244
this._refreshPromise = createDeferred();
244245

246+
const SLOW_DISCOVERY_THRESHOLD_MS = 25_000;
247+
const slowDiscoveryTimer = setTimeout(() => {
248+
traceWarn(Interpreters.envExtDiscoverySlow);
249+
}, SLOW_DISCOVERY_THRESHOLD_MS);
250+
245251
setImmediate(async () => {
246252
try {
247253
await this.envExtApi.refreshEnvironments(undefined);
254+
if (this._envs.length === 0) {
255+
traceWarn(Interpreters.envExtDiscoveryNoEnvironments);
256+
}
248257
this._refreshPromise?.resolve();
249258
} catch (error) {
259+
traceError(Interpreters.envExtDiscoveryFailed, error);
250260
this._refreshPromise?.reject(error);
251261
} finally {
262+
clearTimeout(slowDiscoveryTimer);
252263
traceLog(`Native locator: Refresh finished in ${stopwatch.elapsedTime} ms`);
253264
this.refreshState = ProgressReportStage.discoveryFinished;
254265
this._refreshPromise = undefined;
@@ -297,9 +308,16 @@ class EnvExtApis implements IDiscoveryAPI, Disposable {
297308
if (envPath === undefined) {
298309
return undefined;
299310
}
300-
const pythonEnv = await this.envExtApi.resolveEnvironment(Uri.file(envPath));
301-
if (pythonEnv) {
302-
return this.addEnv(pythonEnv);
311+
try {
312+
const pythonEnv = await this.envExtApi.resolveEnvironment(Uri.file(envPath));
313+
if (pythonEnv) {
314+
return this.addEnv(pythonEnv);
315+
}
316+
} catch (error) {
317+
traceError(
318+
`Failed to resolve environment "${envPath}" via the Python Environments extension (ms-python.vscode-python-envs). Check the "Python Environments" output channel for details.`,
319+
error,
320+
);
303321
}
304322
return undefined;
305323
}

0 commit comments

Comments
 (0)