-
Notifications
You must be signed in to change notification settings - Fork 0
fix: preserve out-of-scope environments during venv scoped refresh #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,20 @@ export function normalizePath(fsPath: string): string { | |
| return path1; | ||
| } | ||
|
|
||
| /** | ||
| * Returns `true` when `candidateFsPath` is the same path as, or nested inside, `scopeFsPath` | ||
| * (inclusive of the scope itself). Both operands are resolved to absolute paths and compared with | ||
| * `path.relative`, so sibling directories sharing a name prefix (e.g. `.../app` vs `.../app-2`) are | ||
| * correctly treated as outside the scope. | ||
| */ | ||
| export function isPathInside(scopeFsPath: string, candidateFsPath: string): boolean { | ||
| const relative = path.relative(path.resolve(scopeFsPath), path.resolve(candidateFsPath)); | ||
| return ( | ||
| relative === '' || | ||
| (relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)) | ||
| ); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This duplicates |
||
| } | ||
|
|
||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This duplicates [verified] |
||
| export function getResourceUri(resourcePath: string, root?: string): Uri | undefined { | ||
| try { | ||
| if (!resourcePath) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ import { PYTHON_EXTENSION_ID } from '../../common/constants'; | |
| import { VenvManagerStrings } from '../../common/localize'; | ||
| import { traceError, traceWarn } from '../../common/logging'; | ||
| import { createDeferred, Deferred } from '../../common/utils/deferred'; | ||
| import { normalizePath } from '../../common/utils/pathUtils'; | ||
| import { normalizePath, isPathInside } from '../../common/utils/pathUtils'; | ||
| import { showErrorMessage, showInformationMessage, withProgress } from '../../common/window.apis'; | ||
| import { findParentIfFile } from '../../features/envCommands'; | ||
| import { getProjectFsPathForScope, tryFastPathGet } from '../common/fastPath'; | ||
|
|
@@ -324,12 +324,7 @@ export class VenvManager implements EnvironmentManager { | |
| title, | ||
| }, | ||
| async () => { | ||
| const discard = this.collection.map((env) => ({ | ||
| kind: EnvironmentChangeKind.remove, | ||
| environment: env, | ||
| })); | ||
|
|
||
| this.collection = | ||
| const discovered = | ||
| (await findVirtualEnvironments( | ||
| hardRefresh, | ||
| this.nativeFinder, | ||
|
|
@@ -338,14 +333,66 @@ export class VenvManager implements EnvironmentManager { | |
| this, | ||
| scope ? [scope] : undefined, | ||
| )) ?? []; | ||
| await this.loadEnvMap(); | ||
|
|
||
| const added = this.collection.map((env) => ({ environment: env, kind: EnvironmentChangeKind.add })); | ||
| this._onDidChangeEnvironments.fire([...discard, ...added]); | ||
| let changes: DidChangeEnvironmentsEventArgs; | ||
| if (scope) { | ||
| changes = await this.mergeScopedEnvironments(scope, discovered); | ||
| } else { | ||
| const discard = this.collection.map((env) => ({ | ||
| kind: EnvironmentChangeKind.remove, | ||
| environment: env, | ||
| })); | ||
| this.collection = discovered; | ||
| await this.loadEnvMap(); | ||
| const added = this.collection.map((env) => ({ | ||
| environment: env, | ||
| kind: EnvironmentChangeKind.add, | ||
| })); | ||
| changes = [...discard, ...added]; | ||
| } | ||
|
|
||
| this._onDidChangeEnvironments.fire(changes); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // A scoped discovery is authoritative only within `scope`: environments in other workspace | ||
| // folders (and globals outside it) are retained untouched, so they neither disappear nor emit | ||
| // events; only in-scope environments are replaced by the freshly discovered ones. | ||
| private async mergeScopedEnvironments( | ||
| scope: Uri, | ||
| discovered: PythonEnvironment[], | ||
| ): Promise<DidChangeEnvironmentsEventArgs> { | ||
| let scopeDir: string; | ||
| try { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[verified] |
||
| scopeDir = await findParentIfFile(scope.fsPath); | ||
| } catch { | ||
| scopeDir = scope.fsPath; | ||
| } | ||
| const inScope = (env: PythonEnvironment): boolean => isPathInside(scopeDir, env.environmentPath.fsPath); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a nested file scope such as |
||
|
|
||
| const retained: PythonEnvironment[] = []; | ||
| const removed: PythonEnvironment[] = []; | ||
| for (const env of this.collection) { | ||
| (inScope(env) ? removed : retained).push(env); | ||
| } | ||
| const retainedIds = new Set(retained.map((env) => env.envId.id)); | ||
| const retainedPaths = new Set(retained.map((env) => normalizePath(env.environmentPath.fsPath))); | ||
|
|
||
| this.collection = [ | ||
| ...retained, | ||
| ...discovered.filter((env) => !retainedPaths.has(normalizePath(env.environmentPath.fsPath))), | ||
| ]; | ||
| await this.loadEnvMap(); | ||
|
|
||
| return [ | ||
| ...removed.map((env) => ({ kind: EnvironmentChangeKind.remove, environment: env })), | ||
| ...this.collection | ||
| .filter((env) => !retainedIds.has(env.envId.id)) | ||
| .map((env) => ({ environment: env, kind: EnvironmentChangeKind.add })), | ||
| ]; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The merge only deduplicates discovered paths against retained environments; it does not exclude newly discovered out-of-scope |
||
| } | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[verified]
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Calling the collection-wide |
||
|
|
||
| async getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]> { | ||
| await this.initialize(); | ||
|
|
||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Path-only partitioning treats configured global environments physically beneath the project scope, including every environment for a filesystem-root workspace, as authoritative in-scope data. Exclude configured global roots from the replaceable partition or classify discovery results by ownership/provenance. [verified] |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.