Skip to content

Commit 3829315

Browse files
authored
Add watcher for pip installs (#1420)
Fixes #548 This pull request enhances the Python environment management by improving how changes to virtual environments and installed packages are detected and handled. The main focus is on ensuring that updates to the environment and package lists are automatically and efficiently refreshed when relevant files are created or deleted. **Improvements to environment and package monitoring:** * Renamed the `watcher` for `activate` files to `activationWatcher` for clarity and consistency. * Added a new `packageWatcher` that monitors `METADATA` files within `site-packages/*.dist-info` directories to detect package installations and uninstallations, triggering a debounced refresh of the package list for all Python projects. * Introduced `packageDebouncedRefresh`, which efficiently refreshes package lists across all environments when relevant changes are detected, and logs errors if package refresh fails.
1 parent 58868b7 commit 3829315

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/managers/builtin/main.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,52 @@ export async function registerSystemPythonFeatures(
2727
const venvDebouncedRefresh = createSimpleDebounce(500, () => {
2828
venvManager.watcherRefresh();
2929
});
30-
const watcher = createFileSystemWatcher('{**/activate}', false, true, false);
30+
const activationWatcher = createFileSystemWatcher('{**/activate}', false, true, false);
3131
disposables.push(
32-
watcher,
33-
watcher.onDidCreate(() => {
32+
activationWatcher,
33+
activationWatcher.onDidCreate(() => {
3434
venvDebouncedRefresh.trigger();
3535
}),
36-
watcher.onDidDelete(() => {
36+
activationWatcher.onDidDelete(() => {
3737
venvDebouncedRefresh.trigger();
3838
}),
3939
onDidDeleteFiles(() => {
4040
venvDebouncedRefresh.trigger();
4141
}),
4242
);
43+
44+
const packageDebouncedRefresh = createSimpleDebounce(500, async () => {
45+
const projects = await api.getPythonProjects();
46+
await Promise.all(
47+
projects.map(async (project) => {
48+
const env = await api.getEnvironment(project.uri);
49+
if (!env) {
50+
return;
51+
}
52+
try {
53+
await api.refreshPackages(env);
54+
} catch (ex) {
55+
log.error(
56+
`Failed to refresh packages for environment ${env.envId}: ${ex instanceof Error ? ex.message : String(ex)}`,
57+
);
58+
}
59+
}),
60+
);
61+
});
62+
const packageWatcher = createFileSystemWatcher(
63+
'**/site-packages/*.dist-info/METADATA',
64+
false, // don't ignore create events (pip install)
65+
true, // ignore change events (content changes in METADATA don't affect package list)
66+
false // don't ignore delete events (pip uninstall)
67+
);
68+
disposables.push(
69+
packageDebouncedRefresh,
70+
packageWatcher,
71+
packageWatcher.onDidCreate(() => {
72+
packageDebouncedRefresh.trigger();
73+
}),
74+
packageWatcher.onDidDelete(() => {
75+
packageDebouncedRefresh.trigger();
76+
}),
77+
);
4378
}

0 commit comments

Comments
 (0)