|
| 1 | +# Automatic Package List Refresh |
| 2 | + |
| 3 | +This feature automatically refreshes the package list when packages are installed or uninstalled in Python environments. It works by monitoring the `site-packages` directory for changes and triggering the package manager's refresh functionality when changes are detected. |
| 4 | + |
| 5 | +## How It Works |
| 6 | + |
| 7 | +1. **Environment Monitoring**: The `SitePackagesWatcherService` listens for environment changes (add/remove) |
| 8 | +2. **Site-packages Resolution**: For each environment, the service resolves the site-packages path using the environment's `sysPrefix` |
| 9 | +3. **File System Watching**: Creates VS Code file system watchers to monitor the site-packages directories |
| 10 | +4. **Automatic Refresh**: When changes are detected, triggers the appropriate package manager's `refresh()` method |
| 11 | + |
| 12 | +## Supported Environment Types |
| 13 | + |
| 14 | +The feature works with all environment types that provide a valid `sysPrefix`: |
| 15 | + |
| 16 | +- **venv** environments |
| 17 | +- **conda** environments |
| 18 | +- **system** Python installations |
| 19 | +- **poetry** environments |
| 20 | +- **pyenv** environments |
| 21 | + |
| 22 | +## Site-packages Path Resolution |
| 23 | + |
| 24 | +The service automatically detects site-packages directories on different platforms: |
| 25 | + |
| 26 | +### Windows |
| 27 | +- `{sysPrefix}/Lib/site-packages` |
| 28 | + |
| 29 | +### Unix/Linux/macOS |
| 30 | +- `{sysPrefix}/lib/python3.*/site-packages` |
| 31 | +- `{sysPrefix}/lib/python3/site-packages` (fallback) |
| 32 | + |
| 33 | +### Conda Environments |
| 34 | +- `{sysPrefix}/site-packages` (for minimal environments) |
| 35 | + |
| 36 | +## Implementation Details |
| 37 | + |
| 38 | +### Key Components |
| 39 | + |
| 40 | +1. **`SitePackagesWatcherService`**: Main service that manages file system watchers |
| 41 | +2. **`sitePackagesUtils.ts`**: Utility functions for resolving site-packages paths |
| 42 | +3. **Integration**: Automatically initialized in `extension.ts` when the extension activates |
| 43 | + |
| 44 | +### Lifecycle Management |
| 45 | + |
| 46 | +- **Initialization**: Watchers are created for existing environments when the service starts |
| 47 | +- **Environment Changes**: New watchers are added when environments are created, removed when environments are deleted |
| 48 | +- **Cleanup**: All watchers are properly disposed when the extension deactivates |
| 49 | + |
| 50 | +### Error Handling |
| 51 | + |
| 52 | +- Graceful handling of environments without valid `sysPrefix` |
| 53 | +- Robust error handling for file system operations |
| 54 | +- Fallback behavior when site-packages directories cannot be found |
| 55 | + |
| 56 | +## Benefits |
| 57 | + |
| 58 | +1. **Real-time Updates**: Package lists are automatically updated when packages change |
| 59 | +2. **Cross-platform Support**: Works on Windows, macOS, and Linux |
| 60 | +3. **Environment Agnostic**: Supports all Python environment types |
| 61 | +4. **Performance**: Uses VS Code's efficient file system watchers |
| 62 | +5. **User Experience**: No manual refresh needed after installing/uninstalling packages |
| 63 | + |
| 64 | +## Technical Notes |
| 65 | + |
| 66 | +- File system events are debounced to avoid excessive refresh calls |
| 67 | +- Package refreshes happen asynchronously to avoid blocking the UI |
| 68 | +- The service integrates seamlessly with existing package manager architecture |
| 69 | +- Comprehensive test coverage ensures reliability across different scenarios |
0 commit comments