Skip to content

Commit

Permalink
Cherry pick commits #4742, #4760, #4766
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne authored Feb 11, 2021
1 parent 1c32778 commit ed32da6
Show file tree
Hide file tree
Showing 90 changed files with 1,013 additions and 464 deletions.
34 changes: 1 addition & 33 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ module.exports = {
'src/test/datascience/mockCode2ProtocolConverter.ts',
'src/test/datascience/mockFileSystem.ts',
'src/test/datascience/interactive-common/trustService.unit.test.ts',
'src/test/datascience/interactive-common/notebookProvider.unit.test.ts',
'src/test/datascience/interactive-common/notebookServerProvider.unit.test.ts',
'src/test/datascience/interactive-common/',
'src/test/datascience/interactive-common/trustCommandHandler.unit.test.ts',
'src/test/datascience/mockStatusProvider.ts',
'src/test/datascience/extensionapi/exampleextension/ms-toolsai-test/webpack.config.js',
Expand All @@ -253,8 +252,6 @@ module.exports = {
'src/test/datascience/preWarmVariables.unit.test.ts',
'src/test/datascience/remoteTestHelpers.ts',
'src/test/datascience/mockWorkspaceFolder.ts',
'src/test/datascience/mockJupyterSession.ts',
'src/test/datascience/jupyterUriProviderRegistration.functional.test.ts',
'src/test/datascience/mockJupyterRequest.ts',
'src/test/datascience/inputHistory.unit.test.ts',
'src/test/datascience/jupyterHelpers.ts',
Expand All @@ -271,12 +268,10 @@ module.exports = {
'src/test/datascience/mockProtocol2CodeConverter.ts',
'src/test/datascience/editor-integration/helpers.ts',
'src/test/datascience/editor-integration/cellhashprovider.unit.test.ts',
'src/test/datascience/editor-integration/gotocell.functional.test.ts',
'src/test/datascience/editor-integration/codelensprovider.unit.test.ts',
'src/test/datascience/editor-integration/codewatcher.unit.test.ts',
'src/test/datascience/jupyterPasswordConnect.unit.test.ts',
'src/test/datascience/testHelpers.tsx',
'src/test/datascience/notebook.functional.test.ts',
'src/test/datascience/mockLanguageClient.ts',
'src/test/datascience/errorHandler.functional.test.tsx',
'src/test/datascience/notebook/notebookStorage.unit.test.ts',
Expand All @@ -300,8 +295,6 @@ module.exports = {
'src/test/datascience/intellisense.unit.test.ts',
'src/test/datascience/markdownManipulation.unit.test.ts',
'src/test/datascience/interactivePanel.functional.test.tsx',
'src/test/datascience/variableTestHelpers.ts',
'src/test/datascience/activation.unit.test.ts',
'src/test/datascience/testPersistentStateFactory.ts',
'src/test/datascience/jupyter/interpreter/jupyterInterpreterSubCommandExecutionService.unit.test.ts',
'src/test/datascience/jupyter/interpreter/jupyterInterpreterDependencyService.unit.test.ts',
Expand Down Expand Up @@ -759,7 +752,6 @@ module.exports = {
'src/client/common/application/commands/reloadCommand.ts',
'src/client/common/application/terminalManager.ts',
'src/client/common/application/applicationEnvironment.ts',
'src/client/common/errors/moduleNotInstalledError.ts',
'src/client/common/installer/serviceRegistry.ts',
'src/client/common/installer/productNames.ts',
'src/client/common/installer/condaInstaller.ts',
Expand All @@ -776,15 +768,13 @@ module.exports = {
'src/client/common/process/currentProcess.ts',
'src/client/common/process/processFactory.ts',
'src/client/common/process/serviceRegistry.ts',
'src/client/common/process/pythonDaemon.ts',
'src/client/common/process/pythonToolService.ts',
'src/client/common/process/internal/python.ts',
'src/client/common/process/internal/scripts/testing_tools.ts',
'src/client/common/process/internal/scripts/vscode_datascience_helpers.ts',
'src/client/common/process/internal/scripts/index.ts',
'src/client/common/process/pythonDaemonPool.ts',
'src/client/common/process/pythonDaemonFactory.ts',
'src/client/common/process/types.ts',
'src/client/common/process/logger.ts',
'src/client/common/process/constants.ts',
'src/client/common/process/pythonProcess.ts',
Expand Down Expand Up @@ -905,12 +895,10 @@ module.exports = {
'src/client/datascience/kernel-launcher/kernelDaemonPreWarmer.ts',
'src/client/datascience/ipywidgets/localWidgetScriptSourceProvider.ts',
'src/client/datascience/ipywidgets/ipyWidgetScriptSourceProvider.ts',
'src/client/datascience/ipywidgets/ipyWidgetScriptSource.ts',
'src/client/datascience/ipywidgets/cdnWidgetScriptSourceProvider.ts',
'src/client/datascience/ipywidgets/types.ts',
'src/client/datascience/ipywidgets/remoteWidgetScriptSourceProvider.ts',
'src/client/datascience/ipywidgets/constants.ts',
'src/client/datascience/ipywidgets/ipyWidgetMessageDispatcher.ts',
'src/client/datascience/ipywidgets/ipywidgetHandler.ts',
'src/client/datascience/ipywidgets/ipyWidgetMessageDispatcherFactory.ts',
'src/client/datascience/themeFinder.ts',
Expand Down Expand Up @@ -955,7 +943,6 @@ module.exports = {
'src/client/datascience/notebookStorage/nativeEditorStorage.ts',
'src/client/datascience/notebookStorage/factory.ts',
'src/client/datascience/notebookStorage/types.ts',
'src/client/datascience/notebookStorage/nativeEditorProvider.ts',
'src/client/datascience/debugLocationTracker.ts',
'src/client/datascience/plotting/plotViewerMessageListener.ts',
'src/client/datascience/plotting/types.ts',
Expand All @@ -976,7 +963,6 @@ module.exports = {
'src/client/datascience/editor-integration/codelensprovider.ts',
'src/client/datascience/editor-integration/cellhashprovider.ts',
'src/client/datascience/commands/commandLineSelector.ts',
'src/client/datascience/commands/notebookCommands.ts',
'src/client/datascience/commands/exportCommands.ts',
'src/client/datascience/cellFactory.ts',
'src/client/datascience/notebook/notebookEditorCompatibilitySupport.ts',
Expand Down Expand Up @@ -1005,10 +991,8 @@ module.exports = {
'src/client/datascience/jupyter/interpreter/jupyterInterpreterSubCommandExecutionService.ts',
'src/client/datascience/jupyter/interpreter/jupyterInterpreterSelector.ts',
'src/client/datascience/jupyter/interpreter/jupyterInterpreterService.ts',
'src/client/datascience/jupyter/kernels/jupyterKernelPromiseFailedError.ts',
'src/client/datascience/jupyter/kernels/jupyterKernelSpec.ts',
'src/client/datascience/jupyter/jupyterExecutionFactory.ts',
'src/client/datascience/jupyter/serverPreload.ts',
'src/client/datascience/jupyter/jupyterRequest.ts',
'src/client/datascience/jupyter/commandLineSelector.ts',
'src/client/datascience/jupyter/jupyterDebugger.ts',
Expand All @@ -1019,29 +1003,13 @@ module.exports = {
'src/client/datascience/jupyter/liveshare/utils.ts',
'src/client/datascience/jupyter/liveshare/guestJupyterSessionManagerFactory.ts',
'src/client/datascience/jupyter/liveshare/types.ts',
'src/client/datascience/jupyter/liveshare/serverCache.ts',
'src/client/datascience/jupyter/liveshare/guestJupyterExecution.ts',
'src/client/datascience/jupyter/jupyterDebuggerPortBlockedError.ts',
'src/client/datascience/jupyter/jupyterConnectError.ts',
'src/client/datascience/jupyter/debuggerVariableRegistration.ts',
'src/client/datascience/jupyter/jupyterDebuggerRemoteNotSupported.ts',
'src/client/datascience/jupyter/jupyterConnection.ts',
'src/client/datascience/jupyter/jupyterPasswordConnect.ts',
'src/client/datascience/jupyter/jupyterDebuggerNotInstalledError.ts',
'src/client/datascience/jupyter/jupyterSelfCertsError.ts',
'src/client/datascience/jupyter/jupyterDebuggerPortNotAvailableError.ts',
'src/client/datascience/jupyter/jupyterWebSocket.ts',
'src/client/datascience/jupyter/jupyterInvalidKernelError.ts',
'src/client/datascience/jupyter/notebookStarter.ts',
'src/client/datascience/jupyter/jupyterZMQBinariesNotFoundError.ts',
'src/client/datascience/jupyter/jupyterUtils.ts',
'src/client/datascience/jupyter/jupyterDataRateLimitError.ts',
'src/client/datascience/jupyter/variableScriptLoader.ts',
'src/client/datascience/jupyter/jupyterImporter.ts',
'src/client/datascience/jupyter/jupyterInstallError.ts',
'src/client/datascience/jupyter/oldJupyterVariables.ts',
'src/client/datascience/jupyter/jupyterInterruptError.ts',
'src/client/datascience/jupyter/invalidNotebookFileError.ts',
'src/client/datascience/jupyter/jupyterCellOutputMimeTypeTracker.ts',
'src/client/datascience/dataScienceFileSystem.ts',
'src/client/logging/levels.ts',
Expand Down
42 changes: 38 additions & 4 deletions src/client/api/pythonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import { inject, injectable } from 'inversify';
import { CancellationToken, Disposable, Event, EventEmitter, Uri } from 'vscode';
import { IApplicationShell, ICommandManager } from '../common/application/types';
import { InterpreterUri } from '../common/installer/types';
import { IExtensions, InstallerResponse, IPersistentStateFactory, Product, Resource } from '../common/types';
import {
IDisposableRegistry,
IExtensions,
InstallerResponse,
IPersistentStateFactory,
Product,
Resource
} from '../common/types';
import { createDeferred } from '../common/utils/async';
import * as localize from '../common/utils/localize';
import { noop } from '../common/utils/misc';
Expand Down Expand Up @@ -201,14 +208,26 @@ const ProductMapping: { [key in Product]: JupyterProductToInstall } = {
/* eslint-disable max-classes-per-file */
@injectable()
export class PythonInstaller implements IPythonInstaller {
private readonly _onInstalled = new EventEmitter<{ product: Product; resource?: InterpreterUri }>();
public get onInstalled(): Event<{ product: Product; resource?: InterpreterUri }> {
return this._onInstalled.event;
}
constructor(@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider) {}

public install(
product: Product,
resource?: InterpreterUri,
cancel?: CancellationToken
): Promise<InstallerResponse> {
return this.apiProvider.getApi().then((api) => api.install(ProductMapping[product], resource, cancel));
return this.apiProvider
.getApi()
.then((api) => api.install(ProductMapping[product], resource, cancel))
.then((result) => {
if (result === InstallerResponse.Installed) {
this._onInstalled.fire({ product, resource });
}
return result;
});
}
}

Expand Down Expand Up @@ -240,10 +259,25 @@ export class InterpreterSelector implements IInterpreterSelector {
@injectable()
export class InterpreterService implements IInterpreterService {
private readonly didChangeInterpreter = new EventEmitter<void>();

constructor(@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider) {}
private eventHandlerAdded?: boolean;
constructor(
@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider,
@inject(IPythonExtensionChecker) private extensionChecker: IPythonExtensionChecker,
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry
) {}

public get onDidChangeInterpreter(): Event<void> {
if (this.extensionChecker.isPythonExtensionInstalled && !this.eventHandlerAdded) {
this.apiProvider
.getApi()
.then((api) => {
if (!this.eventHandlerAdded) {
this.eventHandlerAdded = true;
api.onDidChangeInterpreter(() => this.didChangeInterpreter.fire(), this, this.disposables);
}
})
.catch(noop);
}
return this.didChangeInterpreter.event;
}

Expand Down
1 change: 1 addition & 0 deletions src/client/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export type PythonApi = {

export const IPythonInstaller = Symbol('IPythonInstaller');
export interface IPythonInstaller {
readonly onInstalled: Event<{ product: Product; resource?: InterpreterUri }>;
install(product: Product, resource?: InterpreterUri, cancel?: CancellationToken): Promise<InstallerResponse>;
}

Expand Down
5 changes: 3 additions & 2 deletions src/client/common/cancellation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
'use strict';

import { CancellationToken, CancellationTokenSource } from 'vscode';
import { BaseError } from './errors/types';
import { createDeferred } from './utils/async';
import * as localize from './utils/localize';

/**
* Error type thrown when canceling.
*/
export class CancellationError extends Error {
export class CancellationError extends BaseError {
constructor() {
super(localize.Common.canceled());
super('cancelled', localize.Common.canceled());
}
}

Expand Down
52 changes: 28 additions & 24 deletions src/client/common/errors/errorUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { EOL } from 'os';

// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class ErrorUtils {
public static outputHasModuleNotInstalledError(moduleName: string, content?: string): boolean {
Expand All @@ -14,20 +12,6 @@ export class ErrorUtils {
}
}

/**
* Wraps an error with a custom error message, retaining the call stack information.
*/
export class WrappedError extends Error {
constructor(message: string, public readonly originalException?: Error) {
super(message);
if (originalException) {
// Retain call stack that trapped the error and rethrows this error.
// Also retain the call stack of the original error.
this.stack = `${new Error('').stack}${EOL}${EOL}${originalException.stack}`;
}
}
}

/**
* Given a python traceback, attempt to get the Python error message.
* Generally Python error messages are at the bottom of the traceback.
Expand All @@ -49,14 +33,34 @@ export function getErrorMessageFromPythonTraceback(traceback: string) {
return lastLine.match(pythonErrorMessageRegExp) ? lastLine : undefined;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Constructor<T> = { new (...args: any[]): T };
export function isErrorType<T>(error: Error, expectedType: Constructor<T>) {
if (error instanceof expectedType) {
return true;
export function getLastFrameFromPythonTraceback(
traceback: string
): { fileName: string; folderName: string; packageName: string } | undefined {
if (!traceback) {
return;
}
// File "/Users/donjayamanne/miniconda3/envs/env3/lib/python3.7/site-packages/appnope/_nope.py", line 38, in C

const lastFrame = traceback
.split('\n')
.map((item) => item.trim().toLowerCase())
.filter((item) => item.length)
.reverse()
.find(
(line) =>
line.startsWith('file ') && line.includes(', line ') && line.includes('.py') && line.includes('.py')
);
if (!lastFrame) {
return;
}
if (error instanceof WrappedError && error.originalException instanceof expectedType) {
return true;
const file = lastFrame.substring(0, lastFrame.lastIndexOf('.py')) + '.py';
const parts = file.replace(/\\/g, '/').split('/');
const indexOfSitePackages = parts.indexOf('site-packages');
let packageName =
indexOfSitePackages >= 0 && parts.length > indexOfSitePackages + 1 ? parts[indexOfSitePackages + 1] : '';
const reversedParts = parts.reverse();
if (reversedParts.length < 2) {
return;
}
return false;
return { fileName: reversedParts[0], folderName: reversedParts[1], packageName };
}
Loading

0 comments on commit ed32da6

Please sign in to comment.