Skip to content

Fix bugs found during TPI #65

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

Merged
merged 5 commits into from
Dec 5, 2024
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
14 changes: 8 additions & 6 deletions src/common/pickers/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,21 @@ async function getCommonPackagesToInstall(
export async function getPackagesToInstallFromPackageManager(
packageManager: InternalPackageManager,
environment: PythonEnvironment,
cachedInstallables?: Installable[],
): Promise<string[] | undefined> {
const packageType = packageManager.supportsGetInstallable
? await getPackageType()
: PackageManagement.commonPackages;
let installable: Installable[] = cachedInstallables ?? [];
if (installable.length === 0 && packageManager.supportsGetInstallable) {
installable = await getInstallables(packageManager, environment);
}
const packageType = installable.length > 0 ? await getPackageType() : PackageManagement.commonPackages;

if (packageType === PackageManagement.workspaceDependencies) {
try {
const installable = await getInstallables(packageManager, environment);
const result = await getWorkspacePackages(installable);
return result;
} catch (ex) {
if (packageManager.supportsGetInstallable && ex === QuickInputButtons.Back) {
return getPackagesToInstallFromPackageManager(packageManager, environment);
return getPackagesToInstallFromPackageManager(packageManager, environment, installable);
}
if (ex === QuickInputButtons.Back) {
throw ex;
Expand All @@ -352,7 +354,7 @@ export async function getPackagesToInstallFromPackageManager(
return result;
} catch (ex) {
if (packageManager.supportsGetInstallable && ex === QuickInputButtons.Back) {
return getPackagesToInstallFromPackageManager(packageManager, environment);
return getPackagesToInstallFromPackageManager(packageManager, environment, installable);
}
if (ex === QuickInputButtons.Back) {
throw ex;
Expand Down
29 changes: 24 additions & 5 deletions src/managers/conda/condaPackageManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Disposable, Event, EventEmitter, LogOutputChannel, MarkdownString, ProgressLocation, window } from 'vscode';
import {
CancellationError,
Disposable,
Event,
EventEmitter,
LogOutputChannel,
MarkdownString,
ProgressLocation,
window,
} from 'vscode';
import {
DidChangePackagesEventArgs,
IconPath,
Expand Down Expand Up @@ -45,15 +54,20 @@ export class CondaPackageManager implements PackageManager, Disposable {
{
location: ProgressLocation.Notification,
title: 'Installing packages',
cancellable: true,
},
async () => {
async (_progress, token) => {
try {
const before = this.packages.get(environment.envId.id) ?? [];
const after = await installPackages(environment, packages, options, this.api, this);
const after = await installPackages(environment, packages, options, this.api, this, token);
const changes = getChanges(before, after);
this.packages.set(environment.envId.id, after);
this._onDidChangePackages.fire({ environment: environment, manager: this, changes });
} catch (e) {
if (e instanceof CancellationError) {
return;
}

this.log.error('Error installing packages', e);
setImmediate(async () => {
const result = await window.showErrorMessage('Error installing packages', 'View Output');
Expand All @@ -71,15 +85,20 @@ export class CondaPackageManager implements PackageManager, Disposable {
{
location: ProgressLocation.Notification,
title: 'Uninstalling packages',
cancellable: true,
},
async () => {
async (_progress, token) => {
try {
const before = this.packages.get(environment.envId.id) ?? [];
const after = await uninstallPackages(environment, packages, this.api, this);
const after = await uninstallPackages(environment, packages, this.api, this, token);
const changes = getChanges(before, after);
this.packages.set(environment.envId.id, after);
this._onDidChangePackages.fire({ environment: environment, manager: this, changes });
} catch (e) {
if (e instanceof CancellationError) {
return;
}

this.log.error('Error uninstalling packages', e);
setImmediate(async () => {
const result = await window.showErrorMessage('Error installing packages', 'View Output');
Expand Down
17 changes: 12 additions & 5 deletions src/managers/conda/condaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import * as path from 'path';
import * as os from 'os';
import * as fsapi from 'fs-extra';
import { LogOutputChannel, ProgressLocation, Uri, window } from 'vscode';
import { CancellationError, CancellationToken, LogOutputChannel, ProgressLocation, Uri, window } from 'vscode';
import { ENVS_EXTENSION_ID } from '../../common/constants';
import { createDeferred } from '../../common/utils/deferred';
import {
Expand Down Expand Up @@ -121,12 +121,17 @@ export async function getConda(): Promise<string> {
throw new Error('Conda not found');
}

async function runConda(args: string[]): Promise<string> {
async function runConda(args: string[], token?: CancellationToken): Promise<string> {
const conda = await getConda();

const deferred = createDeferred<string>();
const proc = ch.spawn(conda, args, { shell: true });

token?.onCancellationRequested(() => {
proc.kill();
deferred.reject(new CancellationError());
});

let stdout = '';
let stderr = '';
proc.stdout?.on('data', (data) => {
Expand Down Expand Up @@ -591,7 +596,7 @@ export async function refreshPackages(
name: parts[0],
displayName: parts[0],
version: parts[1],
description: parts[2],
description: parts[1],
},
environment,
manager,
Expand All @@ -608,6 +613,7 @@ export async function installPackages(
options: PackageInstallOptions,
api: PythonEnvironmentApi,
manager: PackageManager,
token: CancellationToken,
): Promise<Package[]> {
if (!packages || packages.length === 0) {
// TODO: Ask user to pick packages
Expand All @@ -620,7 +626,7 @@ export async function installPackages(
}
args.push(...packages);

await runConda(args);
await runConda(args, token);
return refreshPackages(environment, api, manager);
}

Expand All @@ -629,6 +635,7 @@ export async function uninstallPackages(
packages: PackageInfo[] | string[],
api: PythonEnvironmentApi,
manager: PackageManager,
token: CancellationToken,
): Promise<Package[]> {
const remove = [];
for (let pkg of packages) {
Expand All @@ -642,7 +649,7 @@ export async function uninstallPackages(
throw new Error('No packages to remove');
}

await runConda(['remove', '--prefix', environment.environmentPath.fsPath, '--yes', ...remove]);
await runConda(['remove', '--prefix', environment.environmentPath.fsPath, '--yes', ...remove], token);

return refreshPackages(environment, api, manager);
}
10 changes: 6 additions & 4 deletions src/managers/sysPython/pipManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ export class PipPackageManager implements PackageManager, Disposable {
{
location: ProgressLocation.Notification,
title: 'Installing packages',
cancellable: true,
},
async () => {
async (_progress, token) => {
try {
const before = this.packages.get(environment.envId.id) ?? [];
const after = await installPackages(environment, packages, options, this.api, this);
const after = await installPackages(environment, packages, options, this.api, this, token);
const changes = getChanges(before, after);
this.packages.set(environment.envId.id, after);
this._onDidChangePackages.fire({ environment, manager: this, changes });
Expand All @@ -85,11 +86,12 @@ export class PipPackageManager implements PackageManager, Disposable {
{
location: ProgressLocation.Notification,
title: 'Uninstalling packages',
cancellable: true,
},
async () => {
async (_progress, token) => {
try {
const before = this.packages.get(environment.envId.id) ?? [];
const after = await uninstallPackages(environment, this.api, this, packages);
const after = await uninstallPackages(environment, this.api, this, packages, token);
const changes = getChanges(before, after);
this.packages.set(environment.envId.id, after);
this._onDidChangePackages.fire({ environment: environment, manager: this, changes });
Expand Down
32 changes: 29 additions & 3 deletions src/managers/sysPython/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';
import { LogOutputChannel, QuickPickItem, Uri, window } from 'vscode';
import { CancellationError, CancellationToken, LogOutputChannel, QuickPickItem, Uri, window } from 'vscode';
import {
EnvironmentManager,
Package,
Expand Down Expand Up @@ -110,10 +110,20 @@ export async function isUvInstalled(log?: LogOutputChannel): Promise<boolean> {
return available.promise;
}

export async function runUV(args: string[], cwd?: string, log?: LogOutputChannel): Promise<string> {
export async function runUV(
args: string[],
cwd?: string,
log?: LogOutputChannel,
token?: CancellationToken,
): Promise<string> {
log?.info(`Running: uv ${args.join(' ')}`);
return new Promise<string>((resolve, reject) => {
const proc = ch.spawn('uv', args, { cwd: cwd });
token?.onCancellationRequested(() => {
proc.kill();
reject(new CancellationError());
});

let builder = '';
proc.stdout?.on('data', (data) => {
const s = data.toString('utf-8');
Expand All @@ -134,10 +144,20 @@ export async function runUV(args: string[], cwd?: string, log?: LogOutputChannel
});
}

export async function runPython(python: string, args: string[], cwd?: string, log?: LogOutputChannel): Promise<string> {
export async function runPython(
python: string,
args: string[],
cwd?: string,
log?: LogOutputChannel,
token?: CancellationToken,
): Promise<string> {
log?.info(`Running: ${python} ${args.join(' ')}`);
return new Promise<string>((resolve, reject) => {
const proc = ch.spawn(python, args, { cwd: cwd });
token?.onCancellationRequested(() => {
proc.kill();
reject(new CancellationError());
});
let builder = '';
proc.stdout?.on('data', (data) => {
const s = data.toString('utf-8');
Expand Down Expand Up @@ -312,6 +332,7 @@ export async function installPackages(
options: PackageInstallOptions,
api: PythonEnvironmentApi,
manager: PackageManager,
token?: CancellationToken,
): Promise<Package[]> {
if (environment.execInfo) {
if (packages.length === 0) {
Expand All @@ -329,13 +350,15 @@ export async function installPackages(
[...installArgs, '--python', environment.execInfo.run.executable, ...packages],
undefined,
manager.log,
token,
);
} else {
await runPython(
environment.execInfo.run.executable,
['-m', ...installArgs, ...packages],
undefined,
manager.log,
token,
);
}

Expand All @@ -349,6 +372,7 @@ export async function uninstallPackages(
api: PythonEnvironmentApi,
manager: PackageManager,
packages: string[] | Package[],
token?: CancellationToken,
): Promise<Package[]> {
if (environment.execInfo) {
const remove = [];
Expand All @@ -375,13 +399,15 @@ export async function uninstallPackages(
['pip', 'uninstall', '--python', environment.execInfo.run.executable, ...remove],
undefined,
manager.log,
token,
);
} else {
await runPython(
environment.execInfo.run.executable,
['-m', 'pip', 'uninstall', '-y', ...remove],
undefined,
manager.log,
token,
);
}
return refreshPackages(environment, api, manager);
Expand Down
2 changes: 0 additions & 2 deletions src/managers/sysPython/venvUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ export async function createPythonVenv(
const basePython = await pickEnvironmentFrom(sortEnvironments(filtered));
if (!basePython || !basePython.execInfo) {
log.error('No base python selected, cannot create virtual environment.');
showErrorMessage('No base python selected, cannot create virtual environment.');
return;
}

Expand All @@ -283,7 +282,6 @@ export async function createPythonVenv(
});
if (!name) {
log.error('No name entered, cannot create virtual environment.');
showErrorMessage('No name entered, cannot create virtual environment.');
return;
}

Expand Down
Loading