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

Fix behavior when missing Pip #6020

Merged
merged 9 commits into from
Jan 22, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,10 @@ export class DataScienceInstaller extends BaseInstaller {
if (pipInstaller) {
traceInfo(`Installing pip as its not available to install ${moduleName}.`);
await pipInstaller
.installModule(Product.pip, interpreter, cancel)
// --- Start Positron ---
// installAsProcess is required to respect the "Install in Terminal" setting?
.installModule(Product.pip, interpreter, cancel, undefined, { installAsProcess: true })
// --- End Positron ---
.catch((ex) =>
traceError(
`Error in installing the module '${moduleName} as Pip could not be installed', ${ex}`,
Expand Down Expand Up @@ -418,17 +421,37 @@ export class DataScienceInstaller extends BaseInstaller {
): Promise<InstallerResponse> {
// --- Start Positron ---
const productName = ProductNames.get(product)!;
const install = await positron.window.showSimpleModalDialogPrompt(
l10n.t('Install Python package "{0}"?', productName),
message ??
l10n.t(
'To enable Python support, Positron needs to install the package "{0}" for the active interpreter.',
productName,
),
l10n.t('Install'),
);

let hasPip = true;
if (_flags && _flags & ModuleInstallFlags.installPipIfRequired) {
const installer = this.serviceContainer.get<IInstaller>(IInstaller);
hasPip = await installer.isInstalled(Product.pip, resource);
}

let install;
if (hasPip) {
install = await positron.window.showSimpleModalDialogPrompt(
l10n.t('Install Python package "{0}"?', productName),
message ??
l10n.t(
'To enable Python support, Positron needs to install the package "{0}" for the active interpreter.',
productName,
),
l10n.t('Install'),
);
} else {
install = await positron.window.showSimpleModalDialogPrompt(
l10n.t('Install Python packages "{0}" and "{1}"?', ProductNames.get(Product.pip)!, productName),
message ??
l10n.t(
'To enable Python support, Positron needs to install the package "{0}" for the active interpreter.',
productName,
),
l10n.t('Install'),
);
}
if (install) {
return this.install(product, resource, cancel, undefined, options);
return this.install(product, resource, cancel, _flags, options);
}
// --- End Positron ---
return InstallerResponse.Ignore;
Expand Down
35 changes: 26 additions & 9 deletions extensions/positron-python/src/client/positron/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import * as fs from 'fs';
import * as vscode from 'vscode';
import PQueue from 'p-queue';
import { ProductNames } from '../common/installer/productNames';
import { InstallOptions } from '../common/installer/types';
import { InstallOptions, ModuleInstallFlags } from '../common/installer/types';

import {
IConfigurationService,
IInstaller,
Expand Down Expand Up @@ -254,6 +255,9 @@ export class PythonRuntimeSession implements positron.LanguageRuntimeSession, vs
);
}

// Check if we have Pip installed, already
const hasPip = await installer.isInstalled(Product.pip, interpreter);

// Pass a cancellation token to enable VSCode's progress indicator and let the user
// cancel the install.
const tokenSource = new vscode.CancellationTokenSource();
Expand All @@ -265,19 +269,32 @@ export class PythonRuntimeSession implements positron.LanguageRuntimeSession, vs
const installOrUpgrade = hasCompatibleKernel === ProductInstallStatus.NeedsUpgrade ? 'upgrade' : 'install';

const product = Product.ipykernel;
const message = vscode.l10n.t(
'To enable Python support, Positron needs to {0} the package <code>{1}</code> for the active interpreter {2} at: <code>{3}</code>.',
installOrUpgrade,
ProductNames.get(product)!,
`Python ${this.runtimeMetadata.languageVersion}`,
this.runtimeMetadata.runtimePath,
);

let message;
if (!hasPip) {
message = vscode.l10n.t(
'To enable Python support, Positron needs to {0} the packages <code>{1}</code> and <code>{2}</code> for the active interpreter {3} at: <code>{4}</code>.',
samclark2015 marked this conversation as resolved.
Show resolved Hide resolved
installOrUpgrade,
ProductNames.get(Product.pip)!,
ProductNames.get(product)!,
`Python ${this.runtimeMetadata.languageVersion}`,
this.runtimeMetadata.runtimePath,
);
} else {
message = vscode.l10n.t(
'To enable Python support, Positron needs to {0} the package <code>{1}</code> for the active interpreter {2} at: <code>{3}</code>.',
installOrUpgrade,
ProductNames.get(product)!,
`Python ${this.runtimeMetadata.languageVersion}`,
this.runtimeMetadata.runtimePath,
);
}

const response = await installer.promptToInstall(
product,
interpreter,
installerToken,
undefined,
ModuleInstallFlags.installPipIfRequired,
installOptions,
message,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { expect } from 'chai';
import * as TypeMoq from 'typemoq';
import { IApplicationShell } from '../../../client/common/application/types';
import { DataScienceInstaller } from '../../../client/common/installer/productInstaller';
import { IInstallationChannelManager, IModuleInstaller, InterpreterUri } from '../../../client/common/installer/types';
import {
IInstallationChannelManager,
IModuleInstaller,
InterpreterUri,
ModuleInstallFlags,
} from '../../../client/common/installer/types';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: can you pull these out into their own line? with // --- Start/End Positron ---

See: https://connect.posit.it/positron-wiki/overlay-strategy.html#a-note-on-imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. this? Will the lint check yell about importing the package multiple times?

Screenshot 2025-01-21 at 12 14 59 PM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to // eslint-disable-next-line no-duplicate-imports right above that line to ignore the yelling 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - should have looked at the link more closely. 😅 Two notes:

  • Looks like the annotation is now import/no-duplicates... the no-duplicate-imports has no impact so it seems and errors with Definition for rule ... was not found.
  • With either annotation, the original import is still flagged. The first one at least satisfies ESLint for the added import.

Thoughts? Should the whole import block in the referenced commit just be wrapped with Positron markers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh that is new! I don't have strong opinions here, but others might. (@seeM maybe?)

Copy link
Contributor

@juliasilge juliasilge Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if our advice is now outdated because of the change in imports in the recent upstream merge.

No wait actually, did it ever work in the Python extension?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { InstallerResponse, Product } from '../../../client/common/types';
import { Architecture } from '../../../client/common/utils/platform';
import { IServiceContainer } from '../../../client/ioc/types';
Expand Down Expand Up @@ -220,4 +225,72 @@ suite('DataScienceInstaller install', async () => {
const result = await dataScienceInstaller.install(Product.ipykernel, testEnvironment);
expect(result).to.equal(InstallerResponse.Installed, 'Should be Installed');
});

// --- Start Positron ---
test('Will install pip if necessary', async () => {
const testEnvironment: PythonEnvironment = {
envType: EnvironmentType.VirtualEnv,
envName: 'test',
envPath: interpreterPath,
path: interpreterPath,
architecture: Architecture.x64,
sysPrefix: '',
};
const testInstaller = TypeMoq.Mock.ofType<IModuleInstaller>();

testInstaller.setup((c) => c.type).returns(() => ModuleInstallerType.Pip);

// Mock a function to install Product.pip
testInstaller
.setup((c) =>
c.installModule(
TypeMoq.It.isValue(Product.pip),
TypeMoq.It.isValue(testEnvironment),
TypeMoq.It.isAny(),
TypeMoq.It.isAny(),
// We added the `options` param in https://github.com/posit-dev/positron-python/pull/66.
TypeMoq.It.isAny(),
),
)
.callback(() => {
// Add the testInstaller to the available channels once installModule is called
// with Product.pip
installationChannelManager
.setup((c) => c.getInstallationChannels(TypeMoq.It.isAny()))
.returns(() => Promise.resolve([testInstaller.object]));
})
.returns(() => Promise.resolve());

testInstaller
.setup((c) =>
c.installModule(
TypeMoq.It.isValue(Product.ipykernel),
TypeMoq.It.isValue(testEnvironment),
TypeMoq.It.isAny(),
TypeMoq.It.isAny(),
// We added the `options` param in https://github.com/posit-dev/positron-python/pull/66.
TypeMoq.It.isAny(),
),
)
.returns(() => Promise.resolve());

serviceContainer
.setup((c) => c.getAll(TypeMoq.It.isValue(IModuleInstaller)))
.returns(() => [testInstaller.object]);

installationChannelManager
.setup((c) => c.getInstallationChannels(TypeMoq.It.isAny()))
// Specify no installation channels from the get-go
.returns(() => Promise.resolve([]));

const result = await dataScienceInstaller.install(
Product.ipykernel,
testEnvironment,
undefined,
// Pass in the flag to install Pip if it's not available yet
ModuleInstallFlags.installPipIfRequired,
);
expect(result).to.equal(InstallerResponse.Installed, 'Should be Installed');
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test!

// --- End Positron ---
});
Loading