Skip to content

Commit

Permalink
Fix problem with python extension upgrade. Fix pipInstaller ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
rchiodo committed Mar 22, 2022
1 parent 03a3162 commit ab43307
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/kernels/installer/pipInstaller.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { injectable } from 'inversify';
import { inject, injectable } from 'inversify';
import { ExecutionInstallArgs, ModuleInstaller, translateProductToModule } from './moduleInstaller';
import * as path from 'path';
import { IWorkspaceService } from '../../platform/common/application/types';
import { _SCRIPTS_DIR } from '../../platform/common/process/internal/scripts';
import { IPythonExecutionFactory } from '../../platform/common/process/types';
import { ModuleInstallerType, ModuleInstallFlags, Product, IInstaller } from './types';
import { EnvironmentType, PythonEnvironment } from '../../platform/pythonEnvironments/info';
import { IServiceContainer } from '../../platform/ioc/types';

@injectable()
export class PipInstaller extends ModuleInstaller {
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) {
super(serviceContainer);
}

public get name(): string {
return 'Pip';
}
Expand Down
27 changes: 23 additions & 4 deletions src/platform/api/pythonApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// eslint-disable-next-line
/* eslint-disable comma-dangle */
// eslint-disable-next-line
Expand Down Expand Up @@ -35,7 +36,8 @@ import {
IPythonDebuggerPathProvider,
IPythonExtensionChecker,
IPythonProposedApi,
PythonApi
PythonApi,
RefreshInterpretersOptions
} from './types';

/* eslint-disable max-classes-per-file */
Expand Down Expand Up @@ -89,6 +91,17 @@ export class PythonApiProvider implements IPythonApiProvider {
const unifiedApi: PythonApi = {} as any;
Object.assign(unifiedApi, pythonProposedApi.environment);
Object.assign(unifiedApi, api);

// Workaround API name changes (only used in test code at the moment)
if (!unifiedApi.refreshInterpreters && (unifiedApi as any).refreshEnvironment) {
unifiedApi.refreshInterpreters = (options: RefreshInterpretersOptions) => {
return (unifiedApi as any).refreshEnvironment(options);
};
unifiedApi.setActiveInterpreter = (path: string, resource?: Resource) => {
return (unifiedApi as any).setActiveEnvironment(path, resource);
};
}

this.api.resolve(unifiedApi);

// Log experiment status here. Python extension is definitely loaded at this point.
Expand Down Expand Up @@ -278,9 +291,15 @@ export class InterpreterService implements IInterpreterService {
public async refreshInterpreters() {
const api = await this.apiProvider.getApi();
try {
const newItems = await api.refreshInterpreters({ clearCache: false });
this.interpreterListCachePromise = undefined;
traceVerbose(`Refreshed Environments and got ${newItems}`);
if (api.refreshInterpreters) {
const newItems = await api.refreshInterpreters({ clearCache: false });
this.interpreterListCachePromise = undefined;
traceVerbose(`Refreshed Environments and got ${newItems}`);
} else if ((api as any).refreshEnvironment) {
const newItems = await (api as any).refreshEnvironment({ clearCache: false });
this.interpreterListCachePromise = undefined;
traceVerbose(`Refreshed Environments and got ${newItems}`);
}
} catch (ex) {
traceError(`Failed to refresh the list of interpreters`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export type PythonApi = {
setActiveInterpreter(interpreterPath: string, resource?: Resource): Promise<void>;
};

type RefreshInterpretersOptions = {
export type RefreshInterpretersOptions = {
clearCache?: boolean;
};
export type IPythonProposedApi = {
Expand Down
6 changes: 3 additions & 3 deletions src/platform/ioc/serviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ type identifier<T> = string | symbol | Newable<T> | Abstract<T>;
@injectable()
export class ServiceManager implements IServiceManager {
@testOnlyMethod()
public get container() {
return this._container;
public getContainer() {
return this.container;
}
constructor(private readonly _container: Container) {}
constructor(private readonly container: Container) {}
public add<T>(
serviceIdentifier: identifier<T>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
2 changes: 1 addition & 1 deletion src/platform/ioc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface IServiceManager extends IDisposable {
instance: T,
name?: string | number | symbol
): void;
readonly container: Container; // For testing
getContainer(): Container; // For testing
}

export const IServiceContainer = Symbol('IServiceContainer');
Expand Down
2 changes: 1 addition & 1 deletion src/test/extension.serviceRegistry.vscode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ suite('DataScience - Verify serviceRegistry is correct', function () {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS
});
const map = (api.serviceManager.container as any)._bindingDictionary._map as Map<
const map = (api.serviceManager.getContainer() as any)._bindingDictionary._map as Map<
number,
Array<interfaces.Binding<any>>
>;
Expand Down

0 comments on commit ab43307

Please sign in to comment.