Skip to content
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
9 changes: 6 additions & 3 deletions src/client/envExt/api.legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { getEnvExtApi, getEnvironment } from './api.internal';
import { EnvironmentType, PythonEnvironment as PythonEnvironmentLegacy } from '../pythonEnvironments/info';
import { PythonEnvironment, PythonTerminalCreateOptions } from './types';
import { Architecture } from '../common/utils/platform';
import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
import { PythonEnvType } from '../pythonEnvironments/base/info';
import { traceError } from '../logging';
import { reportActiveInterpreterChanged } from '../environmentApi';
import { getWorkspaceFolder, getWorkspaceFolders } from '../common/vscodeApis/workspaceApis';
import { parsePythonEnvironmentVersion } from './utils';

function toEnvironmentType(pythonEnv: PythonEnvironment): EnvironmentType {
if (pythonEnv.envId.managerId.toLowerCase().endsWith('system')) {
Expand Down Expand Up @@ -73,8 +73,11 @@ function getEnvType(kind: EnvironmentType): PythonEnvType | undefined {
}
}

function toLegacyType(env: PythonEnvironment): PythonEnvironmentLegacy {
const ver = parseVersion(env.version);
function toLegacyType(env: PythonEnvironment): PythonEnvironmentLegacy | undefined {
const ver = parsePythonEnvironmentVersion(env);
if (!ver) {
return undefined;
}
const envType = toEnvironmentType(env);
return {
id: env.execInfo.run.executable,
Expand Down
124 changes: 73 additions & 51 deletions src/client/envExt/envExtApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import * as path from 'path';
import { Event, EventEmitter, Disposable, Uri } from 'vscode';
import { PythonEnvInfo, PythonEnvKind, PythonEnvType, PythonVersion } from '../pythonEnvironments/base/info';
import { PythonEnvInfo, PythonEnvKind, PythonEnvType } from '../pythonEnvironments/base/info';
import {
GetRefreshEnvironmentsOptions,
IDiscoveryAPI,
Expand All @@ -26,8 +26,8 @@ import {
} from './types';
import { FileChangeType } from '../common/platform/fileSystemWatcher';
import { Architecture, isWindows } from '../common/utils/platform';
import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
import { Interpreters } from '../common/utils/localize';
import { parsePythonEnvironmentVersion } from './utils';

function getKind(pythonEnv: PythonEnvironment): PythonEnvKind {
if (pythonEnv.envId.managerId.toLowerCase().endsWith('system')) {
Expand Down Expand Up @@ -127,39 +127,51 @@ function getEnvType(kind: PythonEnvKind): PythonEnvType | undefined {
}

function toPythonEnvInfo(pythonEnv: PythonEnvironment): PythonEnvInfo | undefined {
const kind = getKind(pythonEnv);
const arch = Architecture.x64;
const version: PythonVersion = parseVersion(pythonEnv.version);
const { name, displayName, sysPrefix } = pythonEnv;
const executable = getExecutable(pythonEnv);
const location = getLocation(pythonEnv);

return {
name,
location,
kind,
id: executable,
executable: {
filename: executable,
sysPrefix,
ctime: -1,
mtime: -1,
},
version: {
sysVersion: pythonEnv.version,
major: version.major,
minor: version.minor,
micro: version.micro,
},
arch,
distro: {
org: '',
},
source: [],
detailedDisplayName: displayName,
display: displayName,
type: getEnvType(kind),
};
const version = parsePythonEnvironmentVersion(pythonEnv);
if (!version) {
return undefined;
}

try {
const kind = getKind(pythonEnv);
const arch = Architecture.x64;
const { name, displayName, sysPrefix } = pythonEnv;
const executable = getExecutable(pythonEnv);
const location = getLocation(pythonEnv);

return {
name,
location,
kind,
id: executable,
executable: {
filename: executable,
sysPrefix,
ctime: -1,
mtime: -1,
},
version: {
sysVersion: pythonEnv.version,
major: version.major,
minor: version.minor,
micro: version.micro,
},
arch,
distro: {
org: '',
},
source: [],
detailedDisplayName: displayName,
display: displayName,
type: getEnvType(kind),
};
} catch (error) {
traceError(
`Failed to convert environment "${pythonEnv.displayName}" from the Python Environments extension`,
error,
);
return undefined;
}
}

function hasChanged(old: PythonEnvInfo, newEnv: PythonEnvInfo): boolean {
Expand Down Expand Up @@ -214,11 +226,16 @@ class EnvExtApis implements IDiscoveryAPI, Disposable {
this._onChanged,
this.envExtApi.onDidChangeEnvironments((e) => this.onDidChangeEnvironments(e)),
this.envExtApi.onDidChangeEnvironment((e) => {
const oldEnv = e.old ? toPythonEnvInfo(e.old) : undefined;
const newEnv = e.new ? toPythonEnvInfo(e.new) : undefined;
if ((e.old && !oldEnv) || (e.new && !newEnv)) {
return;
}
this._onChanged.fire({
type: FileChangeType.Changed,
searchLocation: e.uri,
old: e.old ? toPythonEnvInfo(e.old) : undefined,
new: e.new ? toPythonEnvInfo(e.new) : undefined,
old: oldEnv,
new: newEnv,
});
}),
);
Expand Down Expand Up @@ -293,15 +310,11 @@ class EnvExtApis implements IDiscoveryAPI, Disposable {
return info;
}

private removeEnv(env: PythonEnvInfo | string): void {
if (typeof env === 'string') {
const old = this._envs.find((item) => item.executable.filename === env);
this._envs = this._envs.filter((item) => item.executable.filename !== env);
this._onChanged.fire({ type: FileChangeType.Deleted, old });
return;
}
this._envs = this._envs.filter((item) => item.executable.filename !== env.executable.filename);
this._onChanged.fire({ type: FileChangeType.Deleted, old: env });
private removeEnv(env: PythonEnvironment): void {
const executable = getExecutable(env);
const old = this._envs.find((item) => item.executable.filename === executable);
this._envs = this._envs.filter((item) => item.executable.filename !== executable);
this._onChanged.fire({ type: FileChangeType.Deleted, old });
}

async resolveEnv(envPath?: string): Promise<PythonEnvInfo | undefined> {
Expand All @@ -328,11 +341,20 @@ class EnvExtApis implements IDiscoveryAPI, Disposable {

onDidChangeEnvironments(e: DidChangeEnvironmentsEventArgs): void {
e.forEach((item) => {
if (item.kind === EnvironmentChangeKind.remove) {
this.removeEnv(item.environment.environmentPath.fsPath);
}
if (item.kind === EnvironmentChangeKind.add) {
this.addEnv(item.environment);
try {
if (item.kind === EnvironmentChangeKind.remove) {
this.removeEnv(item.environment);
}
if (item.kind === EnvironmentChangeKind.add) {
this.addEnv(item.environment);
}
} catch (error) {
traceError(
`Failed to process environment change for "${
item?.environment?.displayName ?? 'unknown environment'
}" from the Python Environments extension`,
error,
);
}
});
}
Expand Down
24 changes: 24 additions & 0 deletions src/client/envExt/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { traceError, traceVerbose } from '../logging';
import { PythonVersion } from '../pythonEnvironments/base/info';
import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
import { PythonEnvironment } from './types';

export function parsePythonEnvironmentVersion(pythonEnv: PythonEnvironment): PythonVersion | undefined {
if (pythonEnv.version === 'no-python') {
traceVerbose(`Skipping environment without Python: ${pythonEnv.displayName}`);
return undefined;
}

try {
return parseVersion(pythonEnv.version);
} catch (error) {
traceError(
`Failed to parse version for environment "${pythonEnv.displayName}" from the Python Environments extension`,
error,
);
return undefined;
}
}
16 changes: 16 additions & 0 deletions src/test/envExt/api.legacy.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,20 @@ suite('Env extension legacy API - getActiveInterpreterLegacy', () => {

expect(getEnvironmentStub.callCount).to.equal(2);
});

test('Returns undefined for an environment without Python', async () => {
getEnvironmentStub.resolves(buildEnv('/usr/bin/conda', 'no-python'));

const result = await getActiveInterpreterLegacy(undefined);

expect(result).to.equal(undefined);
});

test('Returns undefined for an environment with an invalid version', async () => {
getEnvironmentStub.resolves(buildEnv('/usr/bin/python', 'not-a-version'));

const result = await getActiveInterpreterLegacy(undefined);

expect(result).to.equal(undefined);
});
});
Loading
Loading