Skip to content

Commit b2e03ae

Browse files
committed
bug: surface warning for broken defaultEnvManager and resolve
1 parent 1816c28 commit b2e03ae

7 files changed

Lines changed: 83 additions & 9 deletions

File tree

src/extension.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,10 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
563563
sysPythonManager.resolve(sysMgr);
564564
await Promise.all([
565565
registerSystemPythonFeatures(nativeFinder, context.subscriptions, outputChannel, sysMgr),
566-
registerCondaFeatures(nativeFinder, context.subscriptions, outputChannel),
567-
registerPyenvFeatures(nativeFinder, context.subscriptions),
568-
registerPipenvFeatures(nativeFinder, context.subscriptions),
569-
registerPoetryFeatures(nativeFinder, context.subscriptions, outputChannel),
566+
registerCondaFeatures(nativeFinder, context.subscriptions, outputChannel, projectManager),
567+
registerPyenvFeatures(nativeFinder, context.subscriptions, projectManager),
568+
registerPipenvFeatures(nativeFinder, context.subscriptions, projectManager),
569+
registerPoetryFeatures(nativeFinder, context.subscriptions, outputChannel, projectManager),
570570
shellStartupVarsMgr.initialize(),
571571
]);
572572

@@ -616,7 +616,7 @@ async function resolveDefaultInterpreter(
616616

617617
if (defaultInterpreterPath) {
618618
const defaultManager = getConfiguration('python-envs').get<string>('defaultEnvManager', 'undefined');
619-
traceInfo(`resolveDefaultInterpreter setting exists; found defaultEnvManager: ${defaultManager}`);
619+
traceInfo(`resolveDefaultInterpreter setting exists; found defaultEnvManager: ${defaultManager}. `);
620620
if (!defaultManager || defaultManager === 'ms-python.python:venv') {
621621
try {
622622
const resolved: NativeEnvInfo = await nativeFinder.resolve(defaultInterpreterPath);

src/features/settings/settingHelpers.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,25 @@ function getSettings(
3131
return undefined;
3232
}
3333

34+
let DEFAULT_ENV_MANAGER_BROKEN = false;
35+
36+
export function setDefaultEnvManagerBroken(broken: boolean) {
37+
DEFAULT_ENV_MANAGER_BROKEN = broken;
38+
}
39+
export function isDefaultEnvManagerBroken(): boolean {
40+
return DEFAULT_ENV_MANAGER_BROKEN;
41+
}
42+
3443
export function getDefaultEnvManagerSetting(wm: PythonProjectManager, scope?: Uri): string {
3544
const config = workspace.getConfiguration('python-envs', scope);
3645
const settings = getSettings(wm, config, scope);
3746
if (settings && settings.envManager.length > 0) {
3847
return settings.envManager;
3948
}
40-
49+
if (isDefaultEnvManagerBroken()) {
50+
traceInfo(`Default environment manager is broken, using system default: ${DEFAULT_ENV_MANAGER_ID}`);
51+
return DEFAULT_ENV_MANAGER_ID;
52+
}
4153
const defaultManager = config.get<string>('defaultEnvManager');
4254
if (defaultManager === undefined || defaultManager === null || defaultManager === '') {
4355
traceError('No default environment manager set. Check setting python-envs.defaultEnvManager');

src/managers/common/utils.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import * as fs from 'fs-extra';
22
import path from 'path';
3-
import { PythonCommandRunConfiguration, PythonEnvironment } from '../../api';
3+
import { commands, ConfigurationTarget, window, workspace } from 'vscode';
4+
import { PythonCommandRunConfiguration, PythonEnvironment, PythonEnvironmentApi } from '../../api';
45
import { isWindows } from '../../common/utils/platformUtils';
56
import { ShellConstants } from '../../features/common/shellConstants';
7+
import { getDefaultEnvManagerSetting, setDefaultEnvManagerBroken } from '../../features/settings/settingHelpers';
8+
import { PythonProjectManager } from '../../internal.api';
69
import { Installable } from './types';
710

811
export function noop() {
@@ -194,3 +197,41 @@ export async function getShellActivationCommands(binDir: string): Promise<{
194197
shellDeactivation,
195198
};
196199
}
200+
201+
export async function notifyMissingManagerIfDefault(
202+
managerId: string,
203+
projectManager: PythonProjectManager,
204+
api: PythonEnvironmentApi,
205+
) {
206+
const defaultEnvManager = getDefaultEnvManagerSetting(projectManager);
207+
if (defaultEnvManager === managerId) {
208+
setDefaultEnvManagerBroken(true);
209+
await api.refreshEnvironments(undefined);
210+
window
211+
.showErrorMessage(
212+
`The default environment manager is set to '${defaultEnvManager}', but the ${
213+
managerId.split(':')[1]
214+
} executable could not be found.`,
215+
'Reset setting',
216+
'View setting',
217+
'Close',
218+
)
219+
.then((selection) => {
220+
if (selection === 'Reset setting') {
221+
// Remove the setting from all scopes
222+
const config = workspace.getConfiguration('python-envs');
223+
const inspect = config.inspect('defaultEnvManager');
224+
if (inspect?.workspaceValue !== undefined) {
225+
// Remove from workspace settings
226+
config.update('defaultEnvManager', undefined, ConfigurationTarget.Workspace);
227+
} else if (inspect?.globalValue !== undefined) {
228+
// Remove from user settings
229+
config.update('defaultEnvManager', undefined, ConfigurationTarget.Global);
230+
}
231+
}
232+
if (selection === 'View setting') {
233+
commands.executeCommand('workbench.action.openSettings', 'python-envs.defaultEnvManager');
234+
}
235+
});
236+
}
237+
}

src/managers/conda/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Disposable, LogOutputChannel } from 'vscode';
22
import { PythonEnvironmentApi } from '../../api';
33
import { traceInfo } from '../../common/logging';
44
import { getPythonApi } from '../../features/pythonApi';
5+
import { PythonProjectManager } from '../../internal.api';
56
import { NativePythonFinder } from '../common/nativePythonFinder';
7+
import { notifyMissingManagerIfDefault } from '../common/utils';
68
import { CondaEnvManager } from './condaEnvManager';
79
import { CondaPackageManager } from './condaPackageManager';
810
import { CondaSourcingStatus, constructCondaSourcingStatus } from './condaSourcingUtils';
@@ -12,6 +14,7 @@ export async function registerCondaFeatures(
1214
nativeFinder: NativePythonFinder,
1315
disposables: Disposable[],
1416
log: LogOutputChannel,
17+
projectManager: PythonProjectManager,
1518
): Promise<void> {
1619
const api: PythonEnvironmentApi = await getPythonApi();
1720

@@ -34,5 +37,6 @@ export async function registerCondaFeatures(
3437
);
3538
} catch (ex) {
3639
traceInfo('Conda not found, turning off conda features.', ex);
40+
await notifyMissingManagerIfDefault('ms-python.python:conda', projectManager, api);
3741
}
3842
}

src/managers/pipenv/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import { Disposable } from 'vscode';
22
import { PythonEnvironmentApi } from '../../api';
33
import { traceInfo } from '../../common/logging';
44
import { getPythonApi } from '../../features/pythonApi';
5+
import { PythonProjectManager } from '../../internal.api';
56
import { NativePythonFinder } from '../common/nativePythonFinder';
67
import { PipenvManager } from './pipenvManager';
78
import { getPipenv } from './pipenvUtils';
89

10+
import { notifyMissingManagerIfDefault } from '../common/utils';
11+
912
export async function registerPipenvFeatures(
1013
nativeFinder: NativePythonFinder,
1114
disposables: Disposable[],
15+
projectManager: PythonProjectManager,
1216
): Promise<void> {
1317
const api: PythonEnvironmentApi = await getPythonApi();
1418

@@ -17,12 +21,13 @@ export async function registerPipenvFeatures(
1721

1822
if (pipenv) {
1923
const mgr = new PipenvManager(nativeFinder, api);
20-
2124
disposables.push(mgr, api.registerEnvironmentManager(mgr));
2225
} else {
2326
traceInfo('Pipenv not found, turning off pipenv features.');
27+
await notifyMissingManagerIfDefault('ms-python.python:pipenv', projectManager, api);
2428
}
2529
} catch (ex) {
2630
traceInfo('Pipenv not found, turning off pipenv features.', ex);
31+
await notifyMissingManagerIfDefault('ms-python.python:pipenv', projectManager, api);
2732
}
2833
}

src/managers/poetry/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Disposable, LogOutputChannel } from 'vscode';
22
import { PythonEnvironmentApi } from '../../api';
33
import { traceInfo } from '../../common/logging';
44
import { getPythonApi } from '../../features/pythonApi';
5+
import { PythonProjectManager } from '../../internal.api';
56
import { NativePythonFinder } from '../common/nativePythonFinder';
7+
import { notifyMissingManagerIfDefault } from '../common/utils';
68
import { PoetryManager } from './poetryManager';
79
import { PoetryPackageManager } from './poetryPackageManager';
810
import { getPoetry, getPoetryVersion } from './poetryUtils';
@@ -11,6 +13,7 @@ export async function registerPoetryFeatures(
1113
nativeFinder: NativePythonFinder,
1214
disposables: Disposable[],
1315
outputChannel: LogOutputChannel,
16+
projectManager: PythonProjectManager,
1417
): Promise<void> {
1518
const api: PythonEnvironmentApi = await getPythonApi();
1619

@@ -31,8 +34,12 @@ export async function registerPoetryFeatures(
3134
api.registerEnvironmentManager(envManager),
3235
api.registerPackageManager(pkgManager),
3336
);
37+
} else {
38+
traceInfo('Poetry not found, turning off poetry features.');
39+
await notifyMissingManagerIfDefault('ms-python.python:poetry', projectManager, api);
3440
}
3541
} catch (ex) {
3642
traceInfo('Poetry not found, turning off poetry features.', ex);
43+
await notifyMissingManagerIfDefault('ms-python.python:poetry', projectManager, api);
3744
}
3845
}

src/managers/pyenv/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@ import { Disposable } from 'vscode';
22
import { PythonEnvironmentApi } from '../../api';
33
import { traceInfo } from '../../common/logging';
44
import { getPythonApi } from '../../features/pythonApi';
5+
import { PythonProjectManager } from '../../internal.api';
56
import { NativePythonFinder } from '../common/nativePythonFinder';
7+
import { notifyMissingManagerIfDefault } from '../common/utils';
68
import { PyEnvManager } from './pyenvManager';
79
import { getPyenv } from './pyenvUtils';
810

911
export async function registerPyenvFeatures(
1012
nativeFinder: NativePythonFinder,
1113
disposables: Disposable[],
14+
projectManager: PythonProjectManager,
1215
): Promise<void> {
1316
const api: PythonEnvironmentApi = await getPythonApi();
1417

1518
try {
1619
const pyenv = await getPyenv(nativeFinder);
17-
20+
1821
if (pyenv) {
1922
const mgr = new PyEnvManager(nativeFinder, api);
2023
disposables.push(mgr, api.registerEnvironmentManager(mgr));
2124
} else {
2225
traceInfo('Pyenv not found, turning off pyenv features.');
26+
await notifyMissingManagerIfDefault('ms-python.python:pyenv', projectManager, api);
2327
}
2428
} catch (ex) {
2529
traceInfo('Pyenv not found, turning off pyenv features.', ex);
30+
await notifyMissingManagerIfDefault('ms-python.python:pyenv', projectManager, api);
2631
}
2732
}

0 commit comments

Comments
 (0)