|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { compare as pep440Compare } from '@renovatebot/pep440'; |
| 5 | +import { PYLANCE_EXTENSION_ID, PYTHON_EXTENSION_ID } from '../../common/constants'; |
| 6 | +import { getComparableExtensionVersion } from '../../common/extVersion'; |
| 7 | +import { Common, InlineScriptStrings } from '../../common/localize'; |
| 8 | +import { traceInfo, traceVerbose } from '../../common/logging'; |
| 9 | +import { getGlobalPersistentState } from '../../common/persistentState'; |
| 10 | +import { showWarningMessage } from '../../common/window.apis'; |
| 11 | +import { openExtension } from '../../common/workbenchCommands'; |
| 12 | +import { getConfiguration } from '../../common/workspace.apis'; |
| 13 | + |
| 14 | +export const INLINE_SCRIPT_UPDATE_EXTENSIONS_DONT_SHOW_KEY = 'python-envs:inline-script:UPDATE_EXTENSIONS_DONT_SHOW'; |
| 15 | + |
| 16 | +interface CompanionExtension { |
| 17 | + readonly id: string; |
| 18 | + /** Newest version on each channel that still LACKS the required change; anything newer is fine. */ |
| 19 | + readonly lastUnsupportedStable: string; |
| 20 | + readonly lastUnsupportedPreRelease: string; |
| 21 | + /** Patch component at or above which a version is a pre-release build. */ |
| 22 | + readonly preReleasePatchFloor: number; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Python needs per-file interpreter resolution (`exactResource`, PR #26129, merged 2026-08-31); |
| 27 | + * Pylance needs the `python/didChangeFilePythonPath` notification (PR #9302, merged 2026-09-01). |
| 28 | + */ |
| 29 | +const PYTHON_COMPANION: CompanionExtension = { |
| 30 | + id: PYTHON_EXTENSION_ID, |
| 31 | + lastUnsupportedStable: '2026.4.0', |
| 32 | + lastUnsupportedPreRelease: '2026.7.2026082601', |
| 33 | + preReleasePatchFloor: 1_000_000, |
| 34 | +}; |
| 35 | + |
| 36 | +const PYLANCE_COMPANION: CompanionExtension = { |
| 37 | + id: PYLANCE_EXTENSION_ID, |
| 38 | + lastUnsupportedStable: '2026.3.1', |
| 39 | + lastUnsupportedPreRelease: '2026.3.101', |
| 40 | + preReleasePatchFloor: 100, |
| 41 | +}; |
| 42 | + |
| 43 | +let promptShownThisSession = false; |
| 44 | + |
| 45 | +export function resetInlineScriptExtensionPromptForTests(): void { |
| 46 | + promptShownThisSession = false; |
| 47 | +} |
| 48 | + |
| 49 | +function isPylanceInUse(): boolean { |
| 50 | + const languageServer = getConfiguration('python').get<string>('languageServer', 'Default'); |
| 51 | + return languageServer === 'Default' || languageServer === 'Pylance'; |
| 52 | +} |
| 53 | + |
| 54 | +function isPreReleaseBuild(version: string, preReleasePatchFloor: number): boolean { |
| 55 | + const patch = Number((version.split('.')[2] ?? '').replace(/\D.*$/, '')); |
| 56 | + return Number.isFinite(patch) && patch >= preReleasePatchFloor; |
| 57 | +} |
| 58 | + |
| 59 | +function isOutdated(extension: CompanionExtension): boolean { |
| 60 | + const resolved = getComparableExtensionVersion(extension.id); |
| 61 | + if (resolved.kind !== 'version') { |
| 62 | + traceVerbose(`inline-script companion check: ${extension.id} -> ${resolved.kind}`); |
| 63 | + return false; |
| 64 | + } |
| 65 | + const lastUnsupported = isPreReleaseBuild(resolved.version, extension.preReleasePatchFloor) |
| 66 | + ? extension.lastUnsupportedPreRelease |
| 67 | + : extension.lastUnsupportedStable; |
| 68 | + const outdated = pep440Compare(resolved.version, lastUnsupported) <= 0; |
| 69 | + if (outdated) { |
| 70 | + traceVerbose(`inline-script companion check: ${extension.id} ${resolved.version} <= ${lastUnsupported}`); |
| 71 | + } |
| 72 | + return outdated; |
| 73 | +} |
| 74 | + |
| 75 | +export function getOutdatedInlineScriptExtensions(): CompanionExtension[] { |
| 76 | + const candidates = isPylanceInUse() ? [PYTHON_COMPANION, PYLANCE_COMPANION] : [PYTHON_COMPANION]; |
| 77 | + return candidates.filter(isOutdated); |
| 78 | +} |
| 79 | + |
| 80 | +function getOutdatedMessage(outdated: readonly CompanionExtension[]): string { |
| 81 | + const hasPython = outdated.some((extension) => extension.id === PYTHON_EXTENSION_ID); |
| 82 | + const hasPylance = outdated.some((extension) => extension.id === PYLANCE_EXTENSION_ID); |
| 83 | + if (hasPython && hasPylance) { |
| 84 | + return InlineScriptStrings.updatePythonAndPylanceExtensions; |
| 85 | + } |
| 86 | + return hasPython ? InlineScriptStrings.updatePythonExtension : InlineScriptStrings.updatePylanceExtension; |
| 87 | +} |
| 88 | + |
| 89 | +export async function promptUpdateExtensionsForInlineScripts(): Promise<void> { |
| 90 | + if (promptShownThisSession) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const outdated = getOutdatedInlineScriptExtensions(); |
| 95 | + if (outdated.length === 0) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + // Latched here, not on entry, so an up-to-date run does not consume the session's one prompt. |
| 100 | + promptShownThisSession = true; |
| 101 | + |
| 102 | + const state = await getGlobalPersistentState(); |
| 103 | + if (await state.get<boolean>(INLINE_SCRIPT_UPDATE_EXTENSIONS_DONT_SHOW_KEY)) { |
| 104 | + traceInfo('Skipping inline-script companion extension prompt: user selected "Don\'t Show Again".'); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + const names = outdated.map((extension) => extension.id).join(', '); |
| 109 | + traceInfo(`Inline-script companion extensions out of date: ${names}`); |
| 110 | + |
| 111 | + const result = await showWarningMessage( |
| 112 | + getOutdatedMessage(outdated), |
| 113 | + InlineScriptStrings.updateExtension, |
| 114 | + Common.dontShowAgain, |
| 115 | + ); |
| 116 | + |
| 117 | + if (result === InlineScriptStrings.updateExtension) { |
| 118 | + await openExtension(outdated[0].id); |
| 119 | + } else if (result === Common.dontShowAgain) { |
| 120 | + await state.set(INLINE_SCRIPT_UPDATE_EXTENSIONS_DONT_SHOW_KEY, true); |
| 121 | + traceInfo('User selected "Don\'t Show Again" for the inline-script companion extension prompt.'); |
| 122 | + } |
| 123 | +} |
0 commit comments