Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 61 additions & 36 deletions src/managers/conda/condaEnvManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
private collection: PythonEnvironment[] = [];
private fsPathToEnv: Map<string, PythonEnvironment> = new Map();
private globalEnv: PythonEnvironment | undefined;
private inventoryOperations: Promise<void> = Promise.resolve();

private readonly _onDidChangeEnvironment = new EventEmitter<DidChangeEnvironmentEventArgs>();
public readonly onDidChangeEnvironment = this._onDidChangeEnvironment.event;
Expand Down Expand Up @@ -283,16 +284,18 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
}
const discovery = (this.discovery = createDeferred());
try {
await withProgress(
{ location: ProgressLocation.Window, title: CondaStrings.condaDiscovering },
async () => {
this.collection = await this.discoverEnvironments(false);
await this.loadEnvMap();
this._onDidChangeEnvironments.fire(
this.collection.map((environment) => ({ environment, kind: EnvironmentChangeKind.add })),
);
},
);
await this.enqueueInventoryOperation(async () => {
await withProgress(
{ location: ProgressLocation.Window, title: CondaStrings.condaDiscovering },
async () => {
this.collection = await this.discoverEnvironments(false);
await this.loadEnvMap();
this._onDidChangeEnvironments.fire(
this.collection.map((environment) => ({ environment, kind: EnvironmentChangeKind.add })),
);
},
);
});
} catch (error) {
this.initializationError = error;
this.discovery = undefined;
Expand All @@ -302,6 +305,15 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
}
}

private enqueueInventoryOperation<T>(operation: () => Promise<T>): Promise<T> {
const next = this.inventoryOperations.then(operation);
this.inventoryOperations = next.then(
() => undefined,
() => undefined,
);
return next;
}

private async discoverEnvironments(hardRefresh: boolean): Promise<PythonEnvironment[]> {
try {
let discoveryError: unknown;
Expand Down Expand Up @@ -454,26 +466,28 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
}
async refresh(context: RefreshEnvironmentsScope): Promise<void> {
if (context === undefined) {
await withProgress(
{
location: ProgressLocation.Window,
title: CondaStrings.condaRefreshingEnvs,
},
async () => {
this.log.info('Refreshing Conda Environments');
const discard = this.collection.map((c) => c);
this.collection = await this.discoverEnvironments(true);
await this.enqueueInventoryOperation(async () => {
await withProgress(
{
location: ProgressLocation.Window,
title: CondaStrings.condaRefreshingEnvs,
},
async () => {
this.log.info('Refreshing Conda Environments');
const discard = this.collection.map((c) => c);
this.collection = await this.discoverEnvironments(true);

await this.loadEnvMap();
await this.loadEnvMap();

const args = [
...discard.map((env) => ({ kind: EnvironmentChangeKind.remove, environment: env })),
...this.collection.map((env) => ({ kind: EnvironmentChangeKind.add, environment: env })),
];
const args = [
...discard.map((env) => ({ kind: EnvironmentChangeKind.remove, environment: env })),
...this.collection.map((env) => ({ kind: EnvironmentChangeKind.add, environment: env })),
];

this._onDidChangeEnvironments.fire(args);
},
);
this._onDidChangeEnvironments.fire(args);
},
);
});
}
}
async get(scope: GetEnvironmentScope, toolExecution = false): Promise<PythonEnvironment | undefined> {
Expand Down Expand Up @@ -649,12 +663,8 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {

// If the environment is not found, resolve the fsPath. Could be portable conda.
if (!this.globalEnv) {
this.globalEnv = await resolveCondaPath(fsPath, this.nativeFinder, this.api, this.log, this);

// If the environment is resolved, add it to the collection
if (this.globalEnv) {
this.collection.push(this.globalEnv);
}
const resolved = await resolveCondaPath(fsPath, this.nativeFinder, this.api, this.log, this);
this.globalEnv = resolved ? this.trackResolvedEnvironment(resolved) : undefined;
}
}

Expand Down Expand Up @@ -694,9 +704,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
const resolved = await resolveCondaPath(env, this.nativeFinder, this.api, this.log, this);

if (resolved) {
// If resolved add it to the collection
this.fsPathToEnv.set(normalizedPath, resolved);
this.collection.push(resolved);
this.fsPathToEnv.set(normalizedPath, this.trackResolvedEnvironment(resolved));
} else {
this.log.error(`Failed to resolve conda environment: ${env}`);
}
Expand Down Expand Up @@ -756,6 +764,23 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
});
}

/**
* Returns the canonical collection entry for a resolved Conda prefix, adding it only when the
* normalized environment path is not already present. Persisted paths can be resolved concurrently,
* so callers must perform this check after resolution rather than relying on an earlier lookup.
*/
private trackResolvedEnvironment(resolved: PythonEnvironment): PythonEnvironment {
const key = normalizePath(resolved.environmentPath.fsPath);
const existing = this.collection.find((environment) =>
normalizePath(environment.environmentPath.fsPath) === key,
);
if (existing) {
return existing;
}
this.collection.push(resolved);
return resolved;
}

private findEnvironmentByName(name: string): PythonEnvironment | undefined {
return this.collection.find((e) => {
return e.name === name;
Expand Down
Loading
Loading