|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { commands, Disposable, l10n, QuickPickItem, Uri, window } from 'vscode'; |
| 5 | +import { PythonEnvironment } from '../../api'; |
| 6 | +import { INLINE_SCRIPT_MANAGER_ID } from '../../common/constants'; |
| 7 | +import { InlineScriptRoutingRegistry } from '../../common/inlineScript/routingRegistry'; |
| 8 | +import { readInlineScriptMetadataFromFile } from '../../common/inlineScript/metadata'; |
| 9 | +import { traceError, traceInfo } from '../../common/logging'; |
| 10 | +import { showErrorMessage, showInformationMessage, showQuickPickWithButtons } from '../../common/window.apis'; |
| 11 | +import { asRelativePath, findFiles } from '../../common/workspace.apis'; |
| 12 | +import { EnvironmentManagers } from '../../internal.api'; |
| 13 | +import { registerInlineScriptCodeLens } from './codeLens'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Hidden command invoked by the inline-script CodeLens to set up the environment for one script. |
| 17 | + * Intentionally not contributed in `package.json` while the feature is behind the internal flag. |
| 18 | + */ |
| 19 | +export const SETUP_INLINE_SCRIPT_ENV_COMMAND = 'python-envs.setupInlineScriptEnv'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Hidden command that scans the workspace and sets up environments for the selected inline-script |
| 23 | + * files. Intentionally not contributed in `package.json` while the feature is behind the internal flag. |
| 24 | + */ |
| 25 | +export const SETUP_INLINE_SCRIPT_ENVS_COMMAND = 'python-envs.setupInlineScriptEnvs'; |
| 26 | + |
| 27 | +/** Upper bound on the number of `.py` files the bulk command scans. */ |
| 28 | +const MAX_INLINE_SCRIPT_FILES = 1000; |
| 29 | + |
| 30 | +/** How many candidate files' PEP 723 headers are read concurrently during the bulk scan. */ |
| 31 | +const METADATA_READ_CONCURRENCY = 20; |
| 32 | + |
| 33 | +/** |
| 34 | + * Create or reuse the inline-script environment for `scriptUri` and make it the file's environment. |
| 35 | + * |
| 36 | + * Reuses the pipeline built by earlier PEP 723 PRs: |
| 37 | + * - `manager.create` builds or reuses the cached environment from the script's PEP 723 metadata |
| 38 | + * (selecting or, with consent, installing a compatible base interpreter); |
| 39 | + * - `setEnvironment` persists the association, registers the exact script project, and publishes the |
| 40 | + * per-file environment change so routing picks up the inline environment. |
| 41 | + * |
| 42 | + * Returns the environment on success, or `undefined` when creation produced none (the manager has |
| 43 | + * already surfaced the reason — a declined install, no compatible Python, or a cancelled/failed |
| 44 | + * build — and emitted telemetry). |
| 45 | + */ |
| 46 | +export async function setUpInlineScriptEnvironment( |
| 47 | + scriptUri: Uri, |
| 48 | + em: EnvironmentManagers, |
| 49 | + routing: InlineScriptRoutingRegistry, |
| 50 | +): Promise<PythonEnvironment | undefined> { |
| 51 | + const manager = em.getEnvironmentManager(INLINE_SCRIPT_MANAGER_ID); |
| 52 | + if (!manager) { |
| 53 | + traceError('Inline-script setup requested but the inline-script environment manager is not registered.'); |
| 54 | + return undefined; |
| 55 | + } |
| 56 | + const metadataIdentityBeforeCreate = routing.getMetadataIdentity(scriptUri); |
| 57 | + const environment = await manager.create(scriptUri, undefined); |
| 58 | + if (!environment) { |
| 59 | + return undefined; |
| 60 | + } |
| 61 | + if (routing.getMetadataIdentity(scriptUri) !== metadataIdentityBeforeCreate) { |
| 62 | + // The script's saved metadata changed while the environment was being built, so this |
| 63 | + // environment was built for stale metadata. Skip associating it rather than overwrite a newer |
| 64 | + // setup; the current metadata's CodeLens stays so the user can run setup again. |
| 65 | + traceInfo(`Inline-script metadata for ${scriptUri.fsPath} changed during setup; skipping association.`); |
| 66 | + return undefined; |
| 67 | + } |
| 68 | + await em.setEnvironment(scriptUri, environment); |
| 69 | + return environment; |
| 70 | +} |
| 71 | + |
| 72 | +function setupInlineScriptEnvironmentHandler( |
| 73 | + em: EnvironmentManagers, |
| 74 | + routing: InlineScriptRoutingRegistry, |
| 75 | +): (scriptUri?: Uri) => Promise<void> { |
| 76 | + return async (scriptUri?: Uri): Promise<void> => { |
| 77 | + const uri = scriptUri ?? window.activeTextEditor?.document.uri; |
| 78 | + if (!uri || uri.scheme !== 'file') { |
| 79 | + return; |
| 80 | + } |
| 81 | + if (!em.getEnvironmentManager(INLINE_SCRIPT_MANAGER_ID)) { |
| 82 | + showErrorMessage(l10n.t('The inline script environment manager is not available yet. Try again shortly.')); |
| 83 | + return; |
| 84 | + } |
| 85 | + try { |
| 86 | + await setUpInlineScriptEnvironment(uri, em, routing); |
| 87 | + } catch (error) { |
| 88 | + traceError(`Failed to set up the inline-script environment for ${uri.fsPath}:`, error); |
| 89 | + showErrorMessage( |
| 90 | + l10n.t( |
| 91 | + 'Failed to set up the environment for this script. See the Python Environments output for details.', |
| 92 | + ), |
| 93 | + ); |
| 94 | + } |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +interface InlineScriptQuickPickItem extends QuickPickItem { |
| 99 | + readonly uri: Uri; |
| 100 | + readonly configured: boolean; |
| 101 | +} |
| 102 | + |
| 103 | +export async function setUpInlineScriptEnvironmentsInWorkspace( |
| 104 | + em: EnvironmentManagers, |
| 105 | + routing: InlineScriptRoutingRegistry, |
| 106 | +): Promise<void> { |
| 107 | + if (!em.getEnvironmentManager(INLINE_SCRIPT_MANAGER_ID)) { |
| 108 | + showErrorMessage(l10n.t('The inline script environment manager is not available yet. Try again shortly.')); |
| 109 | + return; |
| 110 | + } |
| 111 | + const files = await findFiles('**/*.py', '{**/.venv/**,**/node_modules/**}', MAX_INLINE_SCRIPT_FILES); |
| 112 | + if (!files || files.length === 0) { |
| 113 | + showInformationMessage(l10n.t('No Python files were found in the workspace.')); |
| 114 | + return; |
| 115 | + } |
| 116 | + const candidates = await filterInlineScriptFiles(files); |
| 117 | + if (candidates.length === 0) { |
| 118 | + showInformationMessage( |
| 119 | + l10n.t('No Python files with PEP 723 inline script metadata were found in the workspace.'), |
| 120 | + ); |
| 121 | + return; |
| 122 | + } |
| 123 | + const items: InlineScriptQuickPickItem[] = candidates |
| 124 | + .map((uri) => { |
| 125 | + const configured = routing.shouldRoute(uri); |
| 126 | + return { |
| 127 | + label: asRelativePath(uri), |
| 128 | + description: configured ? l10n.t('environment already set up') : undefined, |
| 129 | + uri, |
| 130 | + configured, |
| 131 | + }; |
| 132 | + }) |
| 133 | + .sort((a, b) => a.label.localeCompare(b.label)); |
| 134 | + // Pre-select only scripts that do not already have a validated inline environment, so that |
| 135 | + // accepting the picker never silently rebuilds or replaces an already-configured script's |
| 136 | + // environment (which could otherwise move it onto a newer base interpreter). |
| 137 | + const preselected = items.filter((item) => !item.configured); |
| 138 | + const selection = await showQuickPickWithButtons(items, { |
| 139 | + canPickMany: true, |
| 140 | + ignoreFocusOut: true, |
| 141 | + title: l10n.t('Set Up Environments for Inline Script Files'), |
| 142 | + placeHolder: l10n.t('Select the scripts to set up environments for'), |
| 143 | + selected: preselected, |
| 144 | + }); |
| 145 | + const picks = Array.isArray(selection) ? selection : selection ? [selection] : []; |
| 146 | + if (picks.length === 0) { |
| 147 | + return; |
| 148 | + } |
| 149 | + let succeeded = 0; |
| 150 | + for (const pick of picks) { |
| 151 | + try { |
| 152 | + if (await setUpInlineScriptEnvironment(pick.uri, em, routing)) { |
| 153 | + succeeded += 1; |
| 154 | + } |
| 155 | + } catch (error) { |
| 156 | + traceError(`Failed to set up the inline-script environment for ${pick.uri.fsPath}:`, error); |
| 157 | + } |
| 158 | + } |
| 159 | + traceInfo(`Inline-script bulk setup: created or reused ${succeeded} of ${picks.length} environment(s).`); |
| 160 | + showInformationMessage(l10n.t('Set up {0} of {1} selected inline script environment(s).', succeeded, picks.length)); |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Read the head of each candidate file (bounded concurrency) and keep only those that declare a |
| 165 | + * PEP 723 `# /// script` block. |
| 166 | + */ |
| 167 | +async function filterInlineScriptFiles(files: readonly Uri[]): Promise<Uri[]> { |
| 168 | + const candidates: Uri[] = []; |
| 169 | + for (let index = 0; index < files.length; index += METADATA_READ_CONCURRENCY) { |
| 170 | + const chunk = files.slice(index, index + METADATA_READ_CONCURRENCY); |
| 171 | + const results = await Promise.all( |
| 172 | + chunk.map(async (uri) => ((await readInlineScriptMetadataFromFile(uri)) ? uri : undefined)), |
| 173 | + ); |
| 174 | + for (const uri of results) { |
| 175 | + if (uri) { |
| 176 | + candidates.push(uri); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + return candidates; |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * Register the inline-script user-facing surfaces (the CodeLens and its setup commands). Only called |
| 185 | + * when the PEP 723 inline-script feature flag is enabled; the commands are intentionally hidden from |
| 186 | + * `package.json` for now. |
| 187 | + */ |
| 188 | +export function registerInlineScriptUx(em: EnvironmentManagers, routing: InlineScriptRoutingRegistry): Disposable[] { |
| 189 | + return [ |
| 190 | + registerInlineScriptCodeLens(routing, SETUP_INLINE_SCRIPT_ENV_COMMAND), |
| 191 | + commands.registerCommand(SETUP_INLINE_SCRIPT_ENV_COMMAND, setupInlineScriptEnvironmentHandler(em, routing)), |
| 192 | + commands.registerCommand(SETUP_INLINE_SCRIPT_ENVS_COMMAND, () => |
| 193 | + setUpInlineScriptEnvironmentsInWorkspace(em, routing), |
| 194 | + ), |
| 195 | + ]; |
| 196 | +} |
0 commit comments