|
1 | 1 | import * as fs from 'fs-extra'; |
2 | 2 | import path from 'path'; |
3 | | -import { PythonCommandRunConfiguration, PythonEnvironment } from '../../api'; |
| 3 | +import { commands, ConfigurationTarget, l10n, window, workspace } from 'vscode'; |
| 4 | +import { PythonCommandRunConfiguration, PythonEnvironment, PythonEnvironmentApi } from '../../api'; |
| 5 | +import { traceLog, traceVerbose } from '../../common/logging'; |
4 | 6 | import { isWindows } from '../../common/utils/platformUtils'; |
5 | 7 | import { ShellConstants } from '../../features/common/shellConstants'; |
| 8 | +import { getDefaultEnvManagerSetting, setDefaultEnvManagerBroken } from '../../features/settings/settingHelpers'; |
| 9 | +import { PythonProjectManager } from '../../internal.api'; |
6 | 10 | import { Installable } from './types'; |
7 | 11 |
|
8 | 12 | export function noop() { |
@@ -194,3 +198,104 @@ export async function getShellActivationCommands(binDir: string): Promise<{ |
194 | 198 | shellDeactivation, |
195 | 199 | }; |
196 | 200 | } |
| 201 | + |
| 202 | +// Tracks if the broken defaultEnvManager error message has been shown this session |
| 203 | +let hasShownBrokenDefaultEnvManagerError = false; |
| 204 | + |
| 205 | +/** |
| 206 | + * Checks if the given managerId is set as the default environment manager for the project. |
| 207 | + * If so, marks the default manager as broken, refreshes environments, and shows an error message to the user. |
| 208 | + * The error message offers to reset the setting, view the setting, or close. |
| 209 | + * The error message is only shown once per session. |
| 210 | + * |
| 211 | + * @param managerId The environment manager id to check. |
| 212 | + * @param projectManager The Python project manager instance. |
| 213 | + * @param api The Python environment API instance. |
| 214 | + */ |
| 215 | +export async function notifyMissingManagerIfDefault( |
| 216 | + managerId: string, |
| 217 | + projectManager: PythonProjectManager, |
| 218 | + api: PythonEnvironmentApi, |
| 219 | +) { |
| 220 | + const defaultEnvManager = getDefaultEnvManagerSetting(projectManager); |
| 221 | + if (defaultEnvManager === managerId) { |
| 222 | + if (hasShownBrokenDefaultEnvManagerError) { |
| 223 | + return; |
| 224 | + } |
| 225 | + hasShownBrokenDefaultEnvManagerError = true; |
| 226 | + setDefaultEnvManagerBroken(true); |
| 227 | + await api.refreshEnvironments(undefined); |
| 228 | + window |
| 229 | + .showErrorMessage( |
| 230 | + l10n.t( |
| 231 | + "The default environment manager is set to '{0}', but the {1} executable could not be found.", |
| 232 | + defaultEnvManager, |
| 233 | + managerId.split(':')[1], |
| 234 | + ), |
| 235 | + l10n.t('Reset setting'), |
| 236 | + l10n.t('View setting'), |
| 237 | + l10n.t('Close'), |
| 238 | + ) |
| 239 | + .then(async (selection) => { |
| 240 | + if (selection === 'Reset setting') { |
| 241 | + const result = await removeFirstDefaultEnvManagerSettingDetailed(managerId); |
| 242 | + if (!result.found) { |
| 243 | + window |
| 244 | + .showErrorMessage( |
| 245 | + l10n.t( |
| 246 | + "Could not find a setting for 'defaultEnvManager' set to '{0}' to reset.", |
| 247 | + managerId, |
| 248 | + ), |
| 249 | + l10n.t('Open settings'), |
| 250 | + l10n.t('Close'), |
| 251 | + ) |
| 252 | + .then((sel) => { |
| 253 | + if (sel === 'Open settings') { |
| 254 | + commands.executeCommand( |
| 255 | + 'workbench.action.openSettings', |
| 256 | + 'python-envs.defaultEnvManager', |
| 257 | + ); |
| 258 | + } |
| 259 | + }); |
| 260 | + } |
| 261 | + } |
| 262 | + if (selection === 'View setting') { |
| 263 | + commands.executeCommand('workbench.action.openSettings', 'python-envs.defaultEnvManager'); |
| 264 | + } |
| 265 | + }); |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +/** |
| 270 | + * Removes the first occurrence of 'defaultEnvManager' set to managerId, returns where it was removed, and logs the action. |
| 271 | + * @param managerId The manager id to match and remove. |
| 272 | + * @returns { found: boolean, scope?: string } |
| 273 | + */ |
| 274 | +export async function removeFirstDefaultEnvManagerSettingDetailed( |
| 275 | + managerId: string, |
| 276 | +): Promise<{ found: boolean; scope?: string }> { |
| 277 | + const config = workspace.getConfiguration('python-envs'); |
| 278 | + const inspect = config.inspect('defaultEnvManager'); |
| 279 | + |
| 280 | + // Workspace folder settings (multi-root) |
| 281 | + if (inspect?.workspaceFolderValue !== undefined && inspect.workspaceFolderValue === managerId) { |
| 282 | + await config.update('defaultEnvManager', undefined, ConfigurationTarget.WorkspaceFolder); |
| 283 | + traceLog("[python-envs] Removed 'defaultEnvManager' from Workspace Folder settings."); |
| 284 | + return { found: true, scope: 'Workspace Folder' }; |
| 285 | + } |
| 286 | + // Workspace settings |
| 287 | + if (inspect?.workspaceValue !== undefined && inspect.workspaceValue === managerId) { |
| 288 | + await config.update('defaultEnvManager', undefined, ConfigurationTarget.Workspace); |
| 289 | + traceLog("[python-envs] Removed 'defaultEnvManager' from Workspace settings."); |
| 290 | + return { found: true, scope: 'Workspace' }; |
| 291 | + } |
| 292 | + // User/global settings |
| 293 | + if (inspect?.globalValue !== undefined && inspect.globalValue === managerId) { |
| 294 | + await config.update('defaultEnvManager', undefined, ConfigurationTarget.Global); |
| 295 | + traceLog("[python-envs] Removed 'defaultEnvManager' from User/Global settings."); |
| 296 | + return { found: true, scope: 'User/Global' }; |
| 297 | + } |
| 298 | + // No matching setting found |
| 299 | + traceVerbose(`[python-envs] Could not find 'defaultEnvManager' set to '${managerId}' in any scope.`); |
| 300 | + return { found: false }; |
| 301 | +} |
0 commit comments