|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, named } from 'inversify'; |
| 7 | +import { ConfigurationTarget, DiagnosticSeverity } from 'vscode'; |
| 8 | +import { IWorkspaceService } from '../../../common/application/types'; |
| 9 | +import { DeprecatePythonPath } from '../../../common/experimentGroups'; |
| 10 | +import { IDisposableRegistry, IExperimentsManager, Resource } from '../../../common/types'; |
| 11 | +import { Common, Diagnostics } from '../../../common/utils/localize'; |
| 12 | +import { learnMoreOnInterpreterSecurityURI } from '../../../interpreter/autoSelection/constants'; |
| 13 | +import { IServiceContainer } from '../../../ioc/types'; |
| 14 | +import { BaseDiagnostic, BaseDiagnosticsService } from '../base'; |
| 15 | +import { IDiagnosticsCommandFactory } from '../commands/types'; |
| 16 | +import { DiagnosticCodes } from '../constants'; |
| 17 | +import { DiagnosticCommandPromptHandlerServiceId, MessageCommandPrompt } from '../promptHandler'; |
| 18 | +import { DiagnosticScope, IDiagnostic, IDiagnosticHandlerService } from '../types'; |
| 19 | + |
| 20 | +export class PythonPathDeprecatedDiagnostic extends BaseDiagnostic { |
| 21 | + constructor(message: string, resource: Resource) { |
| 22 | + super( |
| 23 | + DiagnosticCodes.PythonPathDeprecatedDiagnostic, |
| 24 | + message, |
| 25 | + DiagnosticSeverity.Information, |
| 26 | + DiagnosticScope.WorkspaceFolder, |
| 27 | + resource |
| 28 | + ); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export const PythonPathDeprecatedDiagnosticServiceId = 'PythonPathDeprecatedDiagnosticServiceId'; |
| 33 | + |
| 34 | +export class PythonPathDeprecatedDiagnosticService extends BaseDiagnosticsService { |
| 35 | + private workspaceService: IWorkspaceService; |
| 36 | + constructor( |
| 37 | + @inject(IServiceContainer) serviceContainer: IServiceContainer, |
| 38 | + @inject(IDiagnosticHandlerService) |
| 39 | + @named(DiagnosticCommandPromptHandlerServiceId) |
| 40 | + protected readonly messageService: IDiagnosticHandlerService<MessageCommandPrompt>, |
| 41 | + @inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry |
| 42 | + ) { |
| 43 | + super([DiagnosticCodes.PythonPathDeprecatedDiagnostic], serviceContainer, disposableRegistry, true); |
| 44 | + this.workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService); |
| 45 | + } |
| 46 | + public async diagnose(resource: Resource): Promise<IDiagnostic[]> { |
| 47 | + const experiments = this.serviceContainer.get<IExperimentsManager>(IExperimentsManager); |
| 48 | + experiments.sendTelemetryIfInExperiment(DeprecatePythonPath.control); |
| 49 | + if (!experiments.inExperiment(DeprecatePythonPath.experiment)) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + const setting = this.workspaceService.getConfiguration('python', resource).inspect<string>('pythonPath'); |
| 53 | + if (!setting) { |
| 54 | + return []; |
| 55 | + } |
| 56 | + const isCodeWorkspaceSettingSet = this.workspaceService.workspaceFile && setting.workspaceValue !== undefined; |
| 57 | + const isSettingsJsonSettingSet = setting.workspaceFolderValue !== undefined; |
| 58 | + if (isCodeWorkspaceSettingSet && isSettingsJsonSettingSet) { |
| 59 | + return [ |
| 60 | + new PythonPathDeprecatedDiagnostic(Diagnostics.removePythonPathCodeWorkspaceAndSettingsJson(), resource) |
| 61 | + ]; |
| 62 | + } else if (isSettingsJsonSettingSet) { |
| 63 | + return [new PythonPathDeprecatedDiagnostic(Diagnostics.removePythonPathSettingsJson(), resource)]; |
| 64 | + } else if (isCodeWorkspaceSettingSet) { |
| 65 | + return [new PythonPathDeprecatedDiagnostic(Diagnostics.removePythonPathCodeWorkspace(), resource)]; |
| 66 | + } |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + public async _removePythonPathFromWorkspaceSettings(resource: Resource) { |
| 71 | + const workspaceConfig = this.workspaceService.getConfiguration('python', resource); |
| 72 | + await Promise.all([ |
| 73 | + workspaceConfig.update('pythonPath', undefined, ConfigurationTarget.Workspace), |
| 74 | + workspaceConfig.update('pythonPath', undefined, ConfigurationTarget.WorkspaceFolder) |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + protected async onHandle(diagnostics: IDiagnostic[]): Promise<void> { |
| 79 | + if (diagnostics.length === 0 || !(await this.canHandle(diagnostics[0]))) { |
| 80 | + return; |
| 81 | + } |
| 82 | + const diagnostic = diagnostics[0]; |
| 83 | + if (await this.filterService.shouldIgnoreDiagnostic(diagnostic.code)) { |
| 84 | + return; |
| 85 | + } |
| 86 | + const commandFactory = this.serviceContainer.get<IDiagnosticsCommandFactory>(IDiagnosticsCommandFactory); |
| 87 | + const options = [ |
| 88 | + { |
| 89 | + prompt: Common.yesPlease(), |
| 90 | + command: { |
| 91 | + diagnostic, |
| 92 | + invoke: async (): Promise<void> => { |
| 93 | + return this._removePythonPathFromWorkspaceSettings(diagnostic.resource); |
| 94 | + } |
| 95 | + } |
| 96 | + }, |
| 97 | + { |
| 98 | + prompt: Common.noIWillDoItLater() |
| 99 | + }, |
| 100 | + { |
| 101 | + prompt: Common.moreInfo(), |
| 102 | + command: commandFactory.createCommand(diagnostic, { |
| 103 | + type: 'launch', |
| 104 | + options: learnMoreOnInterpreterSecurityURI |
| 105 | + }) |
| 106 | + }, |
| 107 | + { |
| 108 | + prompt: Common.doNotShowAgain(), |
| 109 | + command: commandFactory.createCommand(diagnostic, { type: 'ignore', options: DiagnosticScope.Global }) |
| 110 | + } |
| 111 | + ]; |
| 112 | + |
| 113 | + await this.messageService.handle(diagnostic, { commandPrompts: options }); |
| 114 | + } |
| 115 | +} |
0 commit comments