|
| 1 | +import * as path from 'path'; |
| 2 | +import { Disposable, LogOutputChannel, RelativePattern, Uri } from 'vscode'; |
| 3 | +import { EnvironmentChangeKind, EnvironmentManager, PackageManager, PythonEnvironment } from '../../api'; |
| 4 | +import { traceVerbose } from '../../common/logging'; |
| 5 | +import { createSimpleDebounce } from '../../common/utils/debounce'; |
| 6 | +import { createFileSystemWatcher } from '../../common/workspace.apis'; |
| 7 | + |
| 8 | +function getWatchTargets(env: PythonEnvironment): RelativePattern[] { |
| 9 | + if (!env.sysPrefix) return []; |
| 10 | + const targets: RelativePattern[] = []; |
| 11 | + |
| 12 | + if (process.platform === 'win32') { |
| 13 | + targets.push( |
| 14 | + new RelativePattern(Uri.file(path.join(env.sysPrefix, 'Lib')), 'site-packages/**/*.dist-info/METADATA'), |
| 15 | + ); |
| 16 | + } else { |
| 17 | + targets.push( |
| 18 | + new RelativePattern( |
| 19 | + Uri.file(path.join(env.sysPrefix, 'lib')), |
| 20 | + 'python*/site-packages/**/*.dist-info/METADATA', |
| 21 | + ), |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + // Conda |
| 26 | + targets.push(new RelativePattern(Uri.file(path.join(env.sysPrefix, 'conda-meta')), '**/*.json')); |
| 27 | + |
| 28 | + return targets; |
| 29 | +} |
| 30 | + |
| 31 | +export function watchPackageChangesForEnvironment( |
| 32 | + env: PythonEnvironment, |
| 33 | + packageManager: PackageManager, |
| 34 | + log: LogOutputChannel, |
| 35 | +): Disposable { |
| 36 | + // Watch targets |
| 37 | + const watchTargets = getWatchTargets(env); |
| 38 | + if (watchTargets.length === 0) { |
| 39 | + traceVerbose(log, `No watch targets for environment ${env.envId}`); |
| 40 | + return new Disposable(() => undefined); |
| 41 | + } |
| 42 | + // Debounced refresh function |
| 43 | + const debouncedRefresh = createSimpleDebounce(500, async () => { |
| 44 | + packageManager.refresh(env).catch((ex) => { |
| 45 | + log.error( |
| 46 | + `Failed to refresh packages for environment ${env.envId}: ${ex instanceof Error ? ex.message : String(ex)}`, |
| 47 | + ); |
| 48 | + }); |
| 49 | + }); |
| 50 | + // Create watchers |
| 51 | + const disposables: Disposable[] = []; |
| 52 | + for (const target of watchTargets) { |
| 53 | + const watcher = createFileSystemWatcher( |
| 54 | + target, |
| 55 | + true, // create -> install |
| 56 | + false, // change -> ignore |
| 57 | + true, // delete -> uninstall |
| 58 | + ); |
| 59 | + disposables.push( |
| 60 | + watcher, |
| 61 | + watcher.onDidCreate(debouncedRefresh.trigger), |
| 62 | + watcher.onDidDelete(debouncedRefresh.trigger), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + return new Disposable(() => disposables.forEach((d) => d.dispose())); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Registers package file watchers for all environments managed by the given manager. |
| 71 | + * |
| 72 | + * This is project-agnostic: if a manager discovers an environment, we watch it. |
| 73 | + */ |
| 74 | +export async function registerPackageWatcherForManager( |
| 75 | + envManager: EnvironmentManager, |
| 76 | + packageManager: PackageManager, |
| 77 | + log: LogOutputChannel, |
| 78 | +): Promise<Disposable> { |
| 79 | + // One watcher per environment id. |
| 80 | + const watchers = new Map<string, Disposable>(); |
| 81 | + |
| 82 | + const addWatcher = (env: PythonEnvironment): void => { |
| 83 | + if (!watchers.has(env.envId.id)) { |
| 84 | + watchers.set(env.envId.id, watchPackageChangesForEnvironment(env, packageManager, log)); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + const removeWatcher = (envId: string): void => { |
| 89 | + watchers.get(envId)?.dispose(); |
| 90 | + watchers.delete(envId); |
| 91 | + }; |
| 92 | + |
| 93 | + // Keep watchers in sync with environment discovery/removal events. |
| 94 | + const envChangeDisposable = envManager.onDidChangeEnvironments?.((changes) => { |
| 95 | + changes.forEach((change) => { |
| 96 | + if (change.kind === EnvironmentChangeKind.add) { |
| 97 | + addWatcher(change.environment); |
| 98 | + } else { |
| 99 | + removeWatcher(change.environment.envId.id); |
| 100 | + } |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + // Seed with environments that already exist before this subscription. |
| 105 | + const environments = await envManager.getEnvironments('all'); |
| 106 | + environments.forEach(addWatcher); |
| 107 | + |
| 108 | + return new Disposable(() => { |
| 109 | + envChangeDisposable?.dispose(); |
| 110 | + watchers.forEach((watcher) => watcher.dispose()); |
| 111 | + watchers.clear(); |
| 112 | + }); |
| 113 | +} |
0 commit comments