|
| 1 | +import * as path from 'path'; |
| 2 | +import { Disposable, LogOutputChannel, RelativePattern } from 'vscode'; |
| 3 | +import { EnvironmentManager, PackageManager, PythonEnvironment } from '../../api'; |
| 4 | +import { createSimpleDebounce } from '../../common/utils/debounce'; |
| 5 | +import { createFileSystemWatcher, getConfiguration } from '../../common/workspace.apis'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Derives the file system watch targets for a given Python environment. |
| 9 | + * |
| 10 | + * Targets include site-packages `.dist-info/METADATA` files for pip-style installs. |
| 11 | + * |
| 12 | + * @param env - The Python environment to derive watch targets for. |
| 13 | + * @returns An array of RelativePattern objects, one per discoverable package location. |
| 14 | + * Empty if the environment has no `sysPrefix` or discoverable paths. |
| 15 | + */ |
| 16 | +function getDefaultPackageWatchTargets(env: PythonEnvironment): RelativePattern[] { |
| 17 | + if (!env.sysPrefix) { |
| 18 | + return []; |
| 19 | + } |
| 20 | + return process.platform === 'win32' |
| 21 | + ? [new RelativePattern(path.join(env.sysPrefix, 'Lib'), 'site-packages/**/*.dist-info/METADATA')] // Windows |
| 22 | + : [new RelativePattern(path.join(env.sysPrefix, 'lib'), 'python*/site-packages/**/*.dist-info/METADATA')]; // Unix-like |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Creates a file system watcher for package changes in a single environment. |
| 27 | + * |
| 28 | + * Monitors default site-packages locations and any manager-specific extra locations |
| 29 | + * for install/uninstall operations. |
| 30 | + * and triggers a debounced package refresh when changes are detected. |
| 31 | + * |
| 32 | + * @param env - The Python environment to watch. |
| 33 | + * @param packageManager - The package manager to call refresh on when changes occur. |
| 34 | + * @param log - Logger for diagnostic messages. |
| 35 | + * @returns A disposable that removes the watcher when disposed. |
| 36 | + */ |
| 37 | +export function watchPackageChangesForEnvironment( |
| 38 | + env: PythonEnvironment, |
| 39 | + packageManager: PackageManager, |
| 40 | + log: LogOutputChannel, |
| 41 | +): Disposable { |
| 42 | + // Watch targets |
| 43 | + const watchTargets = [ |
| 44 | + ...getDefaultPackageWatchTargets(env), |
| 45 | + ...(packageManager.getPackageWatchTargets?.(env) ?? []), |
| 46 | + ]; |
| 47 | + if (watchTargets.length === 0) { |
| 48 | + log.debug(`No watch targets for environment ${env.envId.id}`); |
| 49 | + return new Disposable(() => undefined); |
| 50 | + } |
| 51 | + // Debounced refresh function |
| 52 | + const debouncedRefresh = createSimpleDebounce(500, async () => { |
| 53 | + log.debug(`Package change detected for environment ${env.envId.id}, refreshing packages.`); |
| 54 | + packageManager.refresh(env).catch((ex) => { |
| 55 | + log.error( |
| 56 | + `Failed to refresh packages for environment ${env.envId.id}: ${ex instanceof Error ? ex.message : String(ex)}`, |
| 57 | + ); |
| 58 | + }); |
| 59 | + }); |
| 60 | + // Create watchers |
| 61 | + const disposables: Disposable[] = [debouncedRefresh]; |
| 62 | + const trigger = debouncedRefresh.trigger.bind(debouncedRefresh); |
| 63 | + for (const target of watchTargets) { |
| 64 | + const watcher = createFileSystemWatcher( |
| 65 | + target, |
| 66 | + false, // create -> install |
| 67 | + true, // change -> ignore |
| 68 | + false, // delete -> uninstall |
| 69 | + ); |
| 70 | + disposables.push( |
| 71 | + watcher, |
| 72 | + watcher.onDidCreate(trigger), |
| 73 | + watcher.onDidDelete(trigger), |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + return new Disposable(() => disposables.forEach((d) => d.dispose())); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Registers automatic file system watchers for the active environment managed by a given manager. |
| 82 | + * |
| 83 | + * Creates per-environment watchers that are attached when the active environment changes |
| 84 | + * and detached when it changes to a different environment. Ensures package changes |
| 85 | + * (installs/uninstalls) in the active environment are detected and trigger a refresh. |
| 86 | + * |
| 87 | + * @param envManager - The environment manager whose active environment should be watched. |
| 88 | + * @param packageManager - The package manager to call refresh on when changes occur. |
| 89 | + * @param log - Logger for diagnostic and error messages. |
| 90 | + * @returns A disposable that removes all watchers and subscriptions when disposed. |
| 91 | + */ |
| 92 | +export function registerPackageWatcherForManager( |
| 93 | + envManager: EnvironmentManager, |
| 94 | + packageManager: PackageManager, |
| 95 | + log: LogOutputChannel, |
| 96 | +): Disposable { |
| 97 | + const packageWatchersEnabled = getConfiguration('python-envs').get<boolean>('packageWatchers', true); |
| 98 | + if (!packageWatchersEnabled) { |
| 99 | + return new Disposable(() => undefined); |
| 100 | + } |
| 101 | + |
| 102 | + // One watcher per environment id. |
| 103 | + const watchers = new Map<string, Disposable>(); |
| 104 | + |
| 105 | + const addWatcher = (env: PythonEnvironment): void => { |
| 106 | + if (!watchers.has(env.envId.id)) { |
| 107 | + watchers.set(env.envId.id, watchPackageChangesForEnvironment(env, packageManager, log)); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + const removeWatcher = (envId: string): void => { |
| 112 | + watchers.get(envId)?.dispose(); |
| 113 | + watchers.delete(envId); |
| 114 | + }; |
| 115 | + |
| 116 | + const envChangeDisposable = envManager.onDidChangeEnvironment?.((changes) => { |
| 117 | + if (changes.new) { |
| 118 | + addWatcher(changes.new); |
| 119 | + } |
| 120 | + if (changes.old && changes.old.envId.id !== changes.new?.envId.id) { |
| 121 | + removeWatcher(changes.old.envId.id); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + return new Disposable(() => { |
| 126 | + envChangeDisposable?.dispose(); |
| 127 | + watchers.forEach((watcher) => watcher.dispose()); |
| 128 | + watchers.clear(); |
| 129 | + }); |
| 130 | +} |
0 commit comments