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
1 change: 1 addition & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path';

export const ENVS_EXTENSION_ID = 'ms-python.vscode-python-envs';
export const PYTHON_EXTENSION_ID = 'ms-python.python';
export const PYLANCE_EXTENSION_ID = 'ms-python.vscode-pylance';
export const CONDA_MANAGER_ID = `${PYTHON_EXTENSION_ID}:conda`;
export const INLINE_SCRIPT_MANAGER_ID = `${PYTHON_EXTENSION_ID}:inline-script`;
export const INLINE_SCRIPT_ENVS_KEY = `${ENVS_EXTENSION_ID}:inline-script:SCRIPT_ENVIRONMENTS`;
Expand Down
31 changes: 29 additions & 2 deletions src/common/extVersion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
import { compare as pep440Compare, valid as pep440Valid } from '@renovatebot/pep440';
import { compare as pep440Compare, explain as pep440Explain, valid as pep440Valid } from '@renovatebot/pep440';
import { PYTHON_EXTENSION_ID } from './constants';
import { getExtension } from './extension.apis';
import { traceError } from './logging';
import { traceError, traceWarn } from './logging';

export type ComparableExtensionVersion =
| { readonly kind: 'version'; readonly version: string }
| { readonly kind: 'not-installed' }
| { readonly kind: 'unknown' };

export function getComparableExtensionVersion(extensionId: string): ComparableExtensionVersion {
const extension = getExtension(extensionId);
if (!extension) {
return { kind: 'not-installed' };
}
const rawVersion = extension.packageJSON?.version;
if (typeof rawVersion !== 'string') {
traceWarn(`Extension ${extensionId} reported no version string; skipping version comparison.`);
return { kind: 'unknown' };
}
const parsed = pep440Explain(rawVersion);
if (!parsed) {
traceWarn(`Extension ${extensionId} version "${rawVersion}" is not PEP 440 parseable; skipping comparison.`);
return { kind: 'unknown' };
}
if (parsed.is_devrelease) {
traceWarn(`Extension ${extensionId} version "${rawVersion}" is a dev build; skipping version comparison.`);
return { kind: 'unknown' };
}
return { kind: 'version', version: rawVersion };
}

export function ensureCorrectVersion() {
const extension = getExtension(PYTHON_EXTENSION_ID);
Expand Down
20 changes: 17 additions & 3 deletions src/common/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ export namespace WorkbenchStrings {
export const installExtension = l10n.t('Install Extension');
}

export namespace InlineScriptStrings {
export const updateExtension = l10n.t('Update Extension');

export const updatePythonExtension = l10n.t(
'The environment for this script was created. Update the Python extension for the full inline script experience.',
);
export const updatePylanceExtension = l10n.t(
'The environment for this script was created. Update Pylance for the full inline script experience.',
);
export const updatePythonAndPylanceExtensions = l10n.t(
'The environment for this script was created. Update the Python and Pylance extensions for the full inline script experience.',
);
}

export namespace Interpreter {
export const statusBarSelect = l10n.t('Select Interpreter');
export const browsePath = l10n.t('Browse...');
Expand Down Expand Up @@ -249,7 +263,7 @@ export namespace UvInstallStrings {
export function inlineScriptInstallPythonPrompt(requiresPython?: string, version?: string): string {
if (requiresPython && version) {
return l10n.t(
'No installed Python satisfies this script\'s requirement ({0}). Would you like to install Python {1} using uv?',
"No installed Python satisfies this script's requirement ({0}). Would you like to install Python {1} using uv?",
requiresPython,
version,
);
Expand All @@ -267,7 +281,7 @@ export namespace UvInstallStrings {
export function inlineScriptInstallPythonAndUvPrompt(requiresPython?: string, version?: string): string {
if (requiresPython && version) {
return l10n.t(
'No installed Python satisfies this script\'s requirement ({0}). Would you like to install uv and use it to install Python {1}? This will download and run an installer from https://astral.sh.',
"No installed Python satisfies this script's requirement ({0}). Would you like to install uv and use it to install Python {1}? This will download and run an installer from https://astral.sh.",
requiresPython,
version,
);
Expand All @@ -284,7 +298,7 @@ export namespace UvInstallStrings {
}
export function inlineScriptInstallUvForVersionLookupPrompt(requiresPython: string): string {
return l10n.t(
'No installed Python satisfies this script\'s requirement ({0}). Install uv to find a compatible Python version? This will download and run an installer from https://astral.sh.',
"No installed Python satisfies this script's requirement ({0}). Install uv to find a compatible Python version? This will download and run an installer from https://astral.sh.",
requiresPython,
);
}
Expand Down
123 changes: 123 additions & 0 deletions src/features/inlineScript/extensionVersionCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { compare as pep440Compare } from '@renovatebot/pep440';
import { PYLANCE_EXTENSION_ID, PYTHON_EXTENSION_ID } from '../../common/constants';
import { getComparableExtensionVersion } from '../../common/extVersion';
import { Common, InlineScriptStrings } from '../../common/localize';
import { traceInfo, traceVerbose } from '../../common/logging';
import { getGlobalPersistentState } from '../../common/persistentState';
import { showWarningMessage } from '../../common/window.apis';
import { openExtension } from '../../common/workbenchCommands';
import { getConfiguration } from '../../common/workspace.apis';

export const INLINE_SCRIPT_UPDATE_EXTENSIONS_DONT_SHOW_KEY = 'python-envs:inline-script:UPDATE_EXTENSIONS_DONT_SHOW';

interface CompanionExtension {
readonly id: string;
/** Newest version on each channel that still LACKS the required change; anything newer is fine. */
readonly lastUnsupportedStable: string;
readonly lastUnsupportedPreRelease: string;
/** Patch component at or above which a version is a pre-release build. */
readonly preReleasePatchFloor: number;
}

/**
* Python needs per-file interpreter resolution (`exactResource`, PR #26129, merged 2026-08-31);
* Pylance needs the `python/didChangeFilePythonPath` notification (PR #9302, merged 2026-09-01).
*/
const PYTHON_COMPANION: CompanionExtension = {
id: PYTHON_EXTENSION_ID,
lastUnsupportedStable: '2026.4.0',
lastUnsupportedPreRelease: '2026.7.2026082601',
preReleasePatchFloor: 1_000_000,
};

const PYLANCE_COMPANION: CompanionExtension = {
id: PYLANCE_EXTENSION_ID,
lastUnsupportedStable: '2026.3.1',
lastUnsupportedPreRelease: '2026.3.101',
preReleasePatchFloor: 100,
};

let promptShownThisSession = false;

export function resetInlineScriptExtensionPromptForTests(): void {
promptShownThisSession = false;
}

function isPylanceInUse(): boolean {
const languageServer = getConfiguration('python').get<string>('languageServer', 'Default');
return languageServer === 'Default' || languageServer === 'Pylance';
}

function isPreReleaseBuild(version: string, preReleasePatchFloor: number): boolean {
const patch = Number((version.split('.')[2] ?? '').replace(/\D.*$/, ''));
return Number.isFinite(patch) && patch >= preReleasePatchFloor;
}

function isOutdated(extension: CompanionExtension): boolean {
const resolved = getComparableExtensionVersion(extension.id);
if (resolved.kind !== 'version') {
traceVerbose(`inline-script companion check: ${extension.id} -> ${resolved.kind}`);
return false;
}
const lastUnsupported = isPreReleaseBuild(resolved.version, extension.preReleasePatchFloor)
? extension.lastUnsupportedPreRelease
: extension.lastUnsupportedStable;
const outdated = pep440Compare(resolved.version, lastUnsupported) <= 0;
if (outdated) {
traceVerbose(`inline-script companion check: ${extension.id} ${resolved.version} <= ${lastUnsupported}`);
}
return outdated;
}

export function getOutdatedInlineScriptExtensions(): CompanionExtension[] {
const candidates = isPylanceInUse() ? [PYTHON_COMPANION, PYLANCE_COMPANION] : [PYTHON_COMPANION];
return candidates.filter(isOutdated);
}

function getOutdatedMessage(outdated: readonly CompanionExtension[]): string {
const hasPython = outdated.some((extension) => extension.id === PYTHON_EXTENSION_ID);
const hasPylance = outdated.some((extension) => extension.id === PYLANCE_EXTENSION_ID);
if (hasPython && hasPylance) {
return InlineScriptStrings.updatePythonAndPylanceExtensions;
}
return hasPython ? InlineScriptStrings.updatePythonExtension : InlineScriptStrings.updatePylanceExtension;
}

export async function promptUpdateExtensionsForInlineScripts(): Promise<void> {
if (promptShownThisSession) {
return;
}

const outdated = getOutdatedInlineScriptExtensions();
if (outdated.length === 0) {
return;
}

// Latched here, not on entry, so an up-to-date run does not consume the session's one prompt.
promptShownThisSession = true;

const state = await getGlobalPersistentState();
if (await state.get<boolean>(INLINE_SCRIPT_UPDATE_EXTENSIONS_DONT_SHOW_KEY)) {
traceInfo('Skipping inline-script companion extension prompt: user selected "Don\'t Show Again".');
return;
}

const names = outdated.map((extension) => extension.id).join(', ');
traceInfo(`Inline-script companion extensions out of date: ${names}`);

const result = await showWarningMessage(
getOutdatedMessage(outdated),
InlineScriptStrings.updateExtension,
Common.dontShowAgain,
);

if (result === InlineScriptStrings.updateExtension) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning · Non-blocking recommendation

When both extensions are outdated, the combined prompt names both but outdated[0] opens only Python, and the session latch prevents another prompt. Provide separate actions or make every named update reachable.

await openExtension(outdated[0].id);
} else if (result === Common.dontShowAgain) {
await state.set(INLINE_SCRIPT_UPDATE_EXTENSIONS_DONT_SHOW_KEY, true);
traceInfo('User selected "Don\'t Show Again" for the inline-script companion extension prompt.');
}
}
9 changes: 9 additions & 0 deletions src/features/inlineScript/setupEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { asRelativePath, findFiles, getOpenTextDocuments } from '../../common/workspace.apis';
import { EnvironmentManagers } from '../../internal.api';
import { registerInlineScriptCodeLens } from './codeLens';
import { promptUpdateExtensionsForInlineScripts } from './extensionVersionCheck';

/**
* Hidden command invoked by the inline-script CodeLens to set up the environment for one script.
Expand Down Expand Up @@ -115,7 +116,9 @@ function setupInlineScriptEnvironmentHandler(
const environment = await setUpInlineScriptEnvironment(uri, em, routing);
if (!environment) {
notifyInlineScriptSetupOutcome(uri, routing);
return;
}
await promptUpdateExtensionsForInlineScripts();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning · Non-blocking recommendation

If the advisory prompt rejects after environment creation, this handler reports setup as failed. Isolate prompt failures from the setup try block, matching the bulk path, and add a rejection test.

} catch (error) {
traceError(`Failed to set up the inline-script environment for ${uri.fsPath}:`, error);
showErrorMessage(
Expand Down Expand Up @@ -259,6 +262,12 @@ export async function setUpInlineScriptEnvironmentsInWorkspace(
`Inline-script bulk setup: created or reused ${succeeded} of ${picks.length} environment(s)` +
`${cancelled ? ' (canceled)' : ''}.`,
);
if (succeeded > 0) {
// Not awaited so the run summary below is not held behind this notification.
void promptUpdateExtensionsForInlineScripts().catch((error) =>
traceError('Failed to check companion extension versions for inline scripts:', error),
);
}
if (cancelled) {
showWarningMessage(
l10n.t(
Expand Down
Loading
Loading