Skip to content

Commit 1558336

Browse files
authored
feat: enhance environment discovery logging and troubleshooting guidance (#1294)
1 parent 0c7429b commit 1558336

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

src/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const ENVS_EXTENSION_ID = 'ms-python.vscode-python-envs';
44
export const PYTHON_EXTENSION_ID = 'ms-python.python';
55
export const JUPYTER_EXTENSION_ID = 'ms-toolsai.jupyter';
66
export const EXTENSION_ROOT_DIR = path.dirname(__dirname);
7+
export const ISSUES_URL = 'https://github.com/microsoft/vscode-python-environments/issues';
78

89
export const DEFAULT_PACKAGE_MANAGER_ID = 'ms-python.python:pip';
910
export const DEFAULT_ENV_MANAGER_ID = 'ms-python.python:venv';

src/common/telemetry/helpers.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { getDefaultEnvManagerSetting, getDefaultPkgManagerSetting } from '../../features/settings/settingHelpers';
22
import { EnvironmentManagers, PythonProjectManager } from '../../internal.api';
33
import { getUvEnvironments } from '../../managers/builtin/uvEnvironments';
4-
import { traceVerbose } from '../logging';
4+
import { ISSUES_URL } from '../constants';
5+
import { traceInfo, traceVerbose, traceWarn } from '../logging';
56
import { getWorkspaceFolders } from '../workspace.apis';
67
import { EventNames } from './constants';
78
import { sendTelemetryEvent } from './sender';
@@ -154,3 +155,37 @@ export async function sendEnvironmentToolUsageTelemetry(
154155
traceVerbose('Failed to send environment tool usage telemetry:', error);
155156
}
156157
}
158+
159+
/**
160+
* Logs a summary of environment discovery results after startup.
161+
* If no environments are found, logs guidance to help users troubleshoot.
162+
*/
163+
export async function logDiscoverySummary(envManagers: EnvironmentManagers): Promise<void> {
164+
const managers = envManagers.managers;
165+
let totalEnvCount = 0;
166+
const managerSummaries: string[] = [];
167+
168+
for (const manager of managers) {
169+
try {
170+
const envs = await manager.getEnvironments('all');
171+
totalEnvCount += envs.length;
172+
if (envs.length > 0) {
173+
managerSummaries.push(`${manager.displayName}: ${envs.length}`);
174+
}
175+
} catch {
176+
// Discovery errors are already logged by InternalEnvironmentManager.refresh()
177+
}
178+
}
179+
180+
if (totalEnvCount === 0) {
181+
traceWarn(
182+
`No Python environments were found. ` +
183+
`Try running "Python Environments: Run Python Environment Tool (PET) in Terminal..." from the Command Palette to diagnose. ` +
184+
`If environments should be detected, please report this: ${ISSUES_URL}/new`,
185+
);
186+
} else {
187+
traceInfo(
188+
`Environment discovery complete: ${totalEnvCount} environments found (${managerSummaries.join(', ')})`,
189+
);
190+
}
191+
}

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { newProjectSelection } from './common/pickers/managers';
1818
import { StopWatch } from './common/stopWatch';
1919
import { EventNames } from './common/telemetry/constants';
2020
import {
21+
logDiscoverySummary,
2122
sendEnvironmentToolUsageTelemetry,
2223
sendManagerSelectionTelemetry,
2324
sendProjectStructureTelemetry,
@@ -550,6 +551,9 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
550551
sendManagerSelectionTelemetry(projectManager);
551552
await sendProjectStructureTelemetry(projectManager, envManagers);
552553
await sendEnvironmentToolUsageTelemetry(projectManager, envManagers);
554+
555+
// Log discovery summary to help users troubleshoot environment detection issues
556+
await logDiscoverySummary(envManagers);
553557
} catch (error) {
554558
traceError('Failed to initialize environment managers:', error);
555559
// Show a user-friendly error message

src/internal.api.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import {
2727
ResolveEnvironmentContext,
2828
SetEnvironmentScope,
2929
} from './api';
30+
import { ISSUES_URL } from './common/constants';
3031
import { CreateEnvironmentNotSupported, RemoveEnvironmentNotSupported } from './common/errors/NotSupportedError';
32+
import { traceWarn } from './common/logging';
3133
import { StopWatch } from './common/stopWatch';
3234
import { EventNames } from './common/telemetry/constants';
3335
import { sendTelemetryEvent } from './common/telemetry/sender';
@@ -204,26 +206,48 @@ export class InternalEnvironmentManager implements EnvironmentManager {
204206

205207
async refresh(options: RefreshEnvironmentsScope): Promise<void> {
206208
const sw = new StopWatch();
209+
const SLOW_DISCOVERY_THRESHOLD_MS = 15000;
207210
try {
208211
await this.manager.refresh(options);
209212
const envs = await this.manager.getEnvironments('all').catch(() => []);
210-
sendTelemetryEvent(EventNames.ENVIRONMENT_DISCOVERY, sw.elapsedTime, {
213+
const duration = sw.elapsedTime;
214+
sendTelemetryEvent(EventNames.ENVIRONMENT_DISCOVERY, duration, {
211215
managerId: this.id,
212216
result: 'success',
213217
envCount: envs.length,
214218
});
219+
220+
// Log warning for slow discovery
221+
if (duration > SLOW_DISCOVERY_THRESHOLD_MS) {
222+
traceWarn(
223+
`[${this.displayName}] Environment discovery took ${(duration / 1000).toFixed(1)}s (found ${envs.length} environments). ` +
224+
`If this is causing problems, please report it: ${ISSUES_URL}/new`,
225+
);
226+
}
215227
} catch (ex) {
228+
const duration = sw.elapsedTime;
216229
const isTimeout = ex instanceof Error && ex.message.includes('timed out');
230+
const errorType = ex instanceof Error ? ex.name : 'unknown';
217231
sendTelemetryEvent(
218232
EventNames.ENVIRONMENT_DISCOVERY,
219-
sw.elapsedTime,
233+
duration,
220234
{
221235
managerId: this.id,
222236
result: isTimeout ? 'timeout' : 'error',
223-
errorType: ex instanceof Error ? ex.name : 'unknown',
237+
errorType,
224238
},
225239
ex instanceof Error ? ex : undefined,
226240
);
241+
242+
// Log verbose failure message to help users report issues
243+
const errorMessage = ex instanceof Error ? ex.message : String(ex);
244+
traceWarn(
245+
`[${this.displayName}] Environment discovery failed after ${(duration / 1000).toFixed(1)}s.\n` +
246+
` Error: ${errorType} - ${errorMessage}\n` +
247+
` If environments are not being detected correctly, please report this issue:\n` +
248+
` ${ISSUES_URL}/new`,
249+
);
250+
227251
throw ex;
228252
}
229253
}

0 commit comments

Comments
 (0)