forked from DonJayamanne/pythonVSCode
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate Mac Interpreters in the background #3909
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Validate Mac Interpreters in the background. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/client/application/diagnostics/checks/macPythonInterpreter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import { inject, injectable } from 'inversify'; | ||
import { ConfigurationChangeEvent, DiagnosticSeverity, Uri } from 'vscode'; | ||
import { IWorkspaceService } from '../../../common/application/types'; | ||
import '../../../common/extensions'; | ||
import { IPlatformService } from '../../../common/platform/types'; | ||
import { IConfigurationService, IDisposableRegistry } from '../../../common/types'; | ||
import { IInterpreterHelper, IInterpreterService, InterpreterType } from '../../../interpreter/contracts'; | ||
import { IServiceContainer } from '../../../ioc/types'; | ||
import { BaseDiagnostic, BaseDiagnosticsService } from '../base'; | ||
import { IDiagnosticsCommandFactory } from '../commands/types'; | ||
import { DiagnosticCodes } from '../constants'; | ||
import { DiagnosticCommandPromptHandlerServiceId, MessageCommandPrompt } from '../promptHandler'; | ||
import { DiagnosticScope, IDiagnostic, IDiagnosticCommand, IDiagnosticHandlerService } from '../types'; | ||
|
||
const messages = { | ||
[DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic]: 'You have selected the macOS system install of Python, which is not recommended for use with the Python extension. Some functionality will be limited, please select a different interpreter.', | ||
[DiagnosticCodes.MacInterpreterSelectedAndNoOtherInterpretersDiagnostic]: 'The macOS system install of Python is not recommended, some functionality in the extension will be limited. Install another version of Python for the best experience.' | ||
}; | ||
|
||
export class InvalidMacPythonInterpreterDiagnostic extends BaseDiagnostic { | ||
constructor(code: DiagnosticCodes) { | ||
super(code, messages[code], DiagnosticSeverity.Error, DiagnosticScope.WorkspaceFolder); | ||
} | ||
} | ||
|
||
export const InvalidMacPythonInterpreterServiceId = 'InvalidMacPythonInterpreterServiceId'; | ||
|
||
@injectable() | ||
export class InvalidMacPythonInterpreterService extends BaseDiagnosticsService { | ||
protected changeThrottleTimeout = 1000; | ||
DonJayamanne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private timeOut?: NodeJS.Timer; | ||
constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer, | ||
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService, | ||
@inject(IPlatformService) private readonly platform: IPlatformService, | ||
@inject(IInterpreterHelper) private readonly helper: IInterpreterHelper) { | ||
super( | ||
[ | ||
DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic, | ||
DiagnosticCodes.MacInterpreterSelectedAndNoOtherInterpretersDiagnostic | ||
], serviceContainer); | ||
this.addPythonPathChangedHandler(); | ||
} | ||
public async diagnose(): Promise<IDiagnostic[]> { | ||
if (!this.platform.isMac) { | ||
return []; | ||
} | ||
const configurationService = this.serviceContainer.get<IConfigurationService>(IConfigurationService); | ||
const settings = configurationService.getSettings(); | ||
if (settings.disableInstallationChecks === true) { | ||
return []; | ||
} | ||
|
||
const hasInterpreters = await this.interpreterService.hasInterpreters; | ||
if (!hasInterpreters) { | ||
return []; | ||
} | ||
|
||
const currentInterpreter = await this.interpreterService.getActiveInterpreter(); | ||
if (!currentInterpreter) { | ||
return []; | ||
} | ||
|
||
if (!this.helper.isMacDefaultPythonPath(settings.pythonPath)) { | ||
return []; | ||
} | ||
if (!currentInterpreter || currentInterpreter.type !== InterpreterType.Unknown) { | ||
return []; | ||
} | ||
|
||
const interpreters = await this.interpreterService.getInterpreters(); | ||
if (interpreters.filter(i => !this.helper.isMacDefaultPythonPath(i.path)).length === 0) { | ||
return [new InvalidMacPythonInterpreterDiagnostic(DiagnosticCodes.MacInterpreterSelectedAndNoOtherInterpretersDiagnostic)]; | ||
} | ||
|
||
return [new InvalidMacPythonInterpreterDiagnostic(DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic)]; | ||
} | ||
public async handle(diagnostics: IDiagnostic[]): Promise<void> { | ||
if (diagnostics.length === 0) { | ||
return; | ||
} | ||
const messageService = this.serviceContainer.get<IDiagnosticHandlerService<MessageCommandPrompt>>(IDiagnosticHandlerService, DiagnosticCommandPromptHandlerServiceId); | ||
await Promise.all(diagnostics.map(async diagnostic => { | ||
if (!this.canHandle(diagnostic)) { | ||
return; | ||
} | ||
const commandPrompts = this.getCommandPrompts(diagnostic); | ||
return messageService.handle(diagnostic, { commandPrompts, message: diagnostic.message }); | ||
})); | ||
} | ||
protected addPythonPathChangedHandler() { | ||
const workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService); | ||
const disposables = this.serviceContainer.get<IDisposableRegistry>(IDisposableRegistry); | ||
disposables.push(workspaceService.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this))); | ||
} | ||
protected async onDidChangeConfiguration(event: ConfigurationChangeEvent) { | ||
const workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService); | ||
const workspacesUris: (Uri | undefined)[] = workspaceService.hasWorkspaceFolders ? workspaceService.workspaceFolders!.map(workspace => workspace.uri) : [undefined]; | ||
if (workspacesUris.findIndex(uri => event.affectsConfiguration('python.pythonPath', uri)) === -1) { | ||
return; | ||
} | ||
// Lets wait, for more changes, dirty simple throttling. | ||
if (this.timeOut) { | ||
clearTimeout(this.timeOut); | ||
this.timeOut = undefined; | ||
} | ||
this.timeOut = setTimeout(() => { | ||
this.timeOut = undefined; | ||
this.diagnose().then(dianostics => this.handle(dianostics)).ignoreErrors(); | ||
}, this.changeThrottleTimeout); | ||
} | ||
private getCommandPrompts(diagnostic: IDiagnostic): { prompt: string; command?: IDiagnosticCommand }[] { | ||
const commandFactory = this.serviceContainer.get<IDiagnosticsCommandFactory>(IDiagnosticsCommandFactory); | ||
switch (diagnostic.code) { | ||
case DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic: { | ||
return [{ | ||
prompt: 'Select Python Interpreter', | ||
command: commandFactory.createCommand(diagnostic, { type: 'executeVSCCommand', options: 'python.setInterpreter' }) | ||
}]; | ||
} | ||
case DiagnosticCodes.MacInterpreterSelectedAndNoOtherInterpretersDiagnostic: { | ||
return [{ | ||
prompt: 'Learn more', | ||
command: commandFactory.createCommand(diagnostic, { type: 'launch', options: 'https://code.visualstudio.com/docs/python/python-tutorial#_prerequisites' }) | ||
}, | ||
{ | ||
prompt: 'Download', | ||
command: commandFactory.createCommand(diagnostic, { type: 'launch', options: 'https://www.python.org/downloads' }) | ||
}]; | ||
} | ||
default: { | ||
throw new Error('Invalid diagnostic for \'InvalidMacPythonInterpreterService\''); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DonJayamanne Perhaps I'm being dense here, but you are already validating the Mac interpreter (not in the background) in the above lines 23 to 30.
In order to improve loading times of Python Extension, we should remove validating Mac interpreter in lines 23 to 30, and only validate it in the background.