Skip to content
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

Support new Python API types in installers #14060

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/kernels/errors/kernelErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { PackageNotInstalledWindowsLongPathNotEnabledError } from '../../platfor
import { JupyterNotebookNotInstalled } from '../../platform/errors/jupyterNotebookNotInstalled';
import { fileToCommandArgument } from '../../platform/common/helpers';
import { getJupyterDisplayName } from '../jupyter/connection/jupyterUriProviderRegistration';
import { getPythonEnvDisplayName } from '../../platform/interpreter/helpers';

/***
* Common code for handling errors.
Expand Down Expand Up @@ -160,8 +161,10 @@ export abstract class DataScienceErrorHandler implements IDataScienceErrorHandle
typeof error.product === 'string'
? error.product
: ProductNames.get(error.product) || `${error.product}`;
const interpreterDisplayName = error.interpreter.displayName || error.interpreter.envName || '';
const displayPath = getDisplayPath(error.interpreter.uri);
const interpreterDisplayName = getPythonEnvDisplayName(error.interpreter) || error.interpreter.id || '';
const displayPath = getDisplayPath(
'executable' in error.interpreter ? error.interpreter.executable.uri : error.interpreter.uri
);
let displayName = interpreterDisplayName ? ` ${interpreterDisplayName} (${displayPath})` : displayPath;
return DataScience.packageNotInstalledWindowsLongPathNotEnabledError(packageName, displayName);
} else if (
Expand Down
39 changes: 14 additions & 25 deletions src/platform/api/pythonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PythonExtensionActicationFailedError } from '../errors/pythonExtActivat
import { PythonExtensionApiNotExportedError } from '../errors/pythonExtApiNotExportedError';
import { getOSType, OSType } from '../common/utils/platform';
import { SemVer } from 'semver';
import { getEnvironmentType } from '../interpreter/helpers';

export function deserializePythonEnvironment(
pythonVersion: Partial<PythonEnvironment_PythonApi> | undefined,
Expand Down Expand Up @@ -123,31 +124,7 @@ export function resolvedPythonEnvToJupyterEnv(
};
}
export function pythonEnvToJupyterEnv(env: Environment, supportsEmptyCondaEnv: boolean): PythonEnvironment | undefined {
const envTools = env.tools as KnownEnvironmentTools[];
// Map the Python env tool to a Jupyter environment type.
const orderOrEnvs: [pythonEnvTool: KnownEnvironmentTools, JupyterEnv: EnvironmentType][] = [
['Conda', EnvironmentType.Conda],
['Pyenv', EnvironmentType.Pyenv],
['Pipenv', EnvironmentType.Pipenv],
['Poetry', EnvironmentType.Poetry],
['VirtualEnvWrapper', EnvironmentType.VirtualEnvWrapper],
['VirtualEnv', EnvironmentType.VirtualEnv],
['Venv', EnvironmentType.Venv]
];
let envType = envTools.length ? (envTools[0] as EnvironmentType) : EnvironmentType.Unknown;
if (env.environment?.type === 'Conda') {
envType = EnvironmentType.Conda;
} else {
for (const [pythonEnvTool, JupyterEnv] of orderOrEnvs) {
if (envTools.includes(pythonEnvTool)) {
envType = JupyterEnv;
break;
}
}
if (envType === EnvironmentType.Unknown && env.environment?.type === 'VirtualEnvironment') {
envType = EnvironmentType.VirtualEnv;
}
}
const envType = getEnvironmentType(env);
let isCondaEnvWithoutPython = false;
let uri: Uri;
let id = env.id;
Expand Down Expand Up @@ -429,6 +406,9 @@ export class InterpreterService implements IInterpreterService {
private refreshPromises = new PromiseMonitor();
private pauseEnvDetection = false;
private readonly onResumeEnvDetection = new EventEmitter<void>();
public get known() {
return this.api?.environments.known || [];
}
constructor(
@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider,
@inject(IPythonExtensionChecker) private extensionChecker: IPythonExtensionChecker,
Expand Down Expand Up @@ -465,6 +445,15 @@ export class InterpreterService implements IInterpreterService {
this.disposables
);
}
public async resolveEnvironment(id: string | Environment): Promise<ResolvedEnvironment | undefined> {
return this.getApi().then((api) => {
if (!api) {
return;
}
const env = typeof id === 'string' ? api.environments.known.find((e) => e.id === id || e.path === id) : id;
return api.environments.resolveEnvironment(env || id);
});
}
public get onDidChangeInterpreter(): Event<PythonEnvironment | undefined> {
this.hookupOnDidChangeInterpreterEvent();
return this.didChangeInterpreter.event;
Expand Down
3 changes: 2 additions & 1 deletion src/platform/common/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NotebookCellScheme } from '../constants';
import { InterpreterUri, Resource } from '../types';
import { isPromise } from './async';
import { StopWatch } from './stopWatch';
import { Environment } from '../../api/pythonApiTypes';

// eslint-disable-next-line no-empty,@typescript-eslint/no-empty-function
export function noop() {}
Expand Down Expand Up @@ -105,7 +106,7 @@ export function tracing<T>(log: (t: TraceInfo) => void, run: () => T, logBeforeC
* @param {InterpreterUri} [resource]
* @returns {resource is Resource}
*/
export function isResource(resource?: InterpreterUri): resource is Resource {
export function isResource(resource?: InterpreterUri | Environment): resource is Resource {
if (!resource) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import { BaseError } from './types';
import { PythonEnvironment } from '../pythonEnvironments/info';
import { Product } from '../interpreter/installer/types';
import { Environment } from '../api/pythonApiTypes';

/**
* Thrown when we fail to install a Package due to long path not being enabled on Windows. */
export class PackageNotInstalledWindowsLongPathNotEnabledError extends BaseError {
constructor(
public readonly product: Product | string,
public readonly interpreter: PythonEnvironment,
public readonly interpreter: PythonEnvironment | Environment,
public readonly originalMessage: string
) {
super('windowsLongPathNotEnabled', originalMessage);
Expand Down
3 changes: 1 addition & 2 deletions src/platform/interpreter/activation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

import { CancellationToken } from 'vscode';
import { Resource } from '../../common/types';
import { PythonEnvironment } from '../../pythonEnvironments/info';

export const IEnvironmentActivationService = Symbol('IEnvironmentActivationService');
export interface IEnvironmentActivationService {
getActivatedEnvironmentVariables(
resource: Resource,
interpreter: PythonEnvironment,
interpreter: { id: string },
token?: CancellationToken
): Promise<NodeJS.ProcessEnv | undefined>;
}
8 changes: 8 additions & 0 deletions src/platform/interpreter/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@

import { Event, Uri, CancellationToken } from 'vscode';
import { PythonEnvironment } from '../pythonEnvironments/info';
import { Environment, ResolvedEnvironment } from '../api/pythonApiTypes';
type InterpreterId = string;
export const IInterpreterService = Symbol('IInterpreterService');
export interface IInterpreterService {
// #region New API
readonly known: readonly Environment[];
resolveEnvironment(id: string | Environment): Promise<ResolvedEnvironment | undefined>;
// #endregion

// #region Old API
readonly status: 'refreshing' | 'idle';
readonly onDidChangeStatus: Event<void>;
readonly onDidEnvironmentVariablesChange: Event<void>;
Expand Down Expand Up @@ -39,4 +46,5 @@ export interface IInterpreterService {
token?: CancellationToken
): Promise<undefined | PythonEnvironment>;
getInterpreterHash(id: string): string | undefined;
// #endregion
}
Loading