Skip to content

Commit 5bfc6d5

Browse files
author
Kartik Raj
authored
Add more logging and only retry once if fetching interpreter information fails (#20180)
For microsoft/vscode-python#20147
1 parent 08417ad commit 5bfc6d5

5 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/client/common/process/rawProcessApis.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { DEFAULT_ENCODING } from './constants';
1111
import { ExecutionResult, ObservableExecutionResult, Output, ShellOptions, SpawnOptions, StdErrError } from './types';
1212
import { noop } from '../utils/misc';
1313
import { decodeBuffer } from './decoder';
14+
import { traceVerbose } from '../../logging';
1415

1516
function getDefaultOptions<T extends ShellOptions | SpawnOptions>(options: T, defaultEnv?: EnvironmentVariables): T {
1617
const defaultOptions = { ...options };
@@ -51,6 +52,7 @@ export function shellExec(
5152
disposables?: Set<IDisposable>,
5253
): Promise<ExecutionResult<string>> {
5354
const shellOptions = getDefaultOptions(options, defaultEnv);
55+
traceVerbose(`Shell Exec: ${command} with options: ${JSON.stringify(shellOptions, null, 4)}`);
5456
return new Promise((resolve, reject) => {
5557
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5658
const callback = (e: any, stdout: any, stderr: any) => {

src/client/proposedApi.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ export function buildProposedApi(
274274

275275
async function resolveEnvironment(path: string, discoveryApi: IDiscoveryAPI): Promise<ResolvedEnvironment | undefined> {
276276
const env = await discoveryApi.resolveEnv(path);
277+
if (env?.version.major === -1 || env?.version.minor === -1 || env?.version.micro === -1) {
278+
traceError(`Invalid version for ${path}: ${JSON.stringify(env)}`);
279+
}
277280
if (!env) {
278281
return undefined;
279282
}

src/client/pythonEnvironments/base/info/environmentInfoService.ts

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

44
import { Uri } from 'vscode';
55
import { IDisposableRegistry } from '../../../common/types';
6-
import { createDeferred, Deferred } from '../../../common/utils/async';
6+
import { createDeferred, Deferred, sleep } from '../../../common/utils/async';
77
import { createRunningWorkerPool, IWorkerPool, QueuePosition } from '../../../common/utils/workerPool';
88
import { getInterpreterInfo, InterpreterInformation } from './interpreter';
99
import { buildPythonExecInfo } from '../../exec';
@@ -102,9 +102,6 @@ class EnvironmentInfoService implements IEnvironmentInfoService {
102102
this._getEnvironmentInfo(env, priority)
103103
.then((r) => {
104104
deferred.resolve(r);
105-
if (r === undefined) {
106-
this.cache.delete(normCasePath(interpreterPath));
107-
}
108105
})
109106
.catch((ex) => {
110107
deferred.reject(ex);
@@ -115,6 +112,7 @@ class EnvironmentInfoService implements IEnvironmentInfoService {
115112
public async _getEnvironmentInfo(
116113
env: PythonEnvInfo,
117114
priority?: EnvironmentInfoServiceQueuePriority,
115+
retryOnce = true,
118116
): Promise<InterpreterInformation | undefined> {
119117
if (env.kind === PythonEnvKind.Conda && env.executable.filename === 'python') {
120118
const emptyInterpreterInfo: InterpreterInformation = {
@@ -166,6 +164,12 @@ class EnvironmentInfoService implements IEnvironmentInfoService {
166164
traceError(reason);
167165
}
168166
}
167+
if (r === undefined && retryOnce) {
168+
// Retry once, in case the environment was not fully populated. Also observed in CI:
169+
// https://github.com/microsoft/vscode-python/issues/20147 where running environment the first time
170+
// failed due to unknown reasons.
171+
return sleep(2000).then(() => this._getEnvironmentInfo(env, priority, false));
172+
}
169173
return r;
170174
}
171175

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

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

44
import { Event } from 'vscode';
55
import { isTestExecution } from '../../../../common/constants';
6-
import { traceInfo } from '../../../../logging';
6+
import { traceInfo, traceVerbose } from '../../../../logging';
77
import { arePathsSame, getFileInfo, pathExists } from '../../../common/externalDependencies';
88
import { PythonEnvInfo } from '../../info';
99
import { areEnvsDeepEqual, areSameEnv, getEnvPath } from '../../info/env';
@@ -141,6 +141,7 @@ export class PythonEnvInfoCache extends PythonEnvsWatcher<PythonEnvCollectionCha
141141
public addEnv(env: PythonEnvInfo, hasLatestInfo?: boolean): void {
142142
const found = this.envs.find((e) => areSameEnv(e, env));
143143
if (hasLatestInfo) {
144+
traceVerbose(`Adding env to cache ${env.id}`);
144145
this.validatedEnvs.add(env.id!);
145146
this.flush(env).ignoreErrors(); // If we have latest info, flush it so it can be saved.
146147
}
@@ -172,13 +173,16 @@ export class PythonEnvInfoCache extends PythonEnvsWatcher<PythonEnvCollectionCha
172173
const env = this.envs.find((e) => arePathsSame(e.location, path)) ?? this.envs.find((e) => areSameEnv(e, path));
173174
if (env) {
174175
if (this.validatedEnvs.has(env.id!)) {
176+
traceVerbose(`Found cached env for ${path}`);
175177
return env;
176178
}
177179
if (await validateInfo(env)) {
180+
traceVerbose(`Needed to validate ${path} with latest info`);
178181
this.validatedEnvs.add(env.id!);
179182
return env;
180183
}
181184
}
185+
traceVerbose(`No cached env found for ${path}`);
182186
return undefined;
183187
}
184188

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
8888
// only use cache if it has complete info on an environment.
8989
const cachedEnv = await this.cache.getLatestInfo(path);
9090
if (cachedEnv) {
91+
traceVerbose(`Resolved ${path} from cache: ${JSON.stringify(cachedEnv)}`);
9192
return cachedEnv;
9293
}
9394
const resolved = await this.locator.resolveEnv(path).catch((ex) => {

0 commit comments

Comments
 (0)