Skip to content

Commit 0321c3d

Browse files
authored
fix: environment initialization to ensure proper resolution (#1209)
Fix the environment initialization process in multiple managers to guarantee that the initialization resolves after the refresh operation completes, improving reliability.
1 parent 261fe77 commit 0321c3d

File tree

5 files changed

+89
-66
lines changed

5 files changed

+89
-66
lines changed

src/managers/builtin/sysPythonManager.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ export class SysPythonManager implements EnvironmentManager {
6868

6969
this._initialized = createDeferred();
7070

71-
await this.internalRefresh(false, SysManagerStrings.sysManagerDiscovering);
72-
73-
this._initialized.resolve();
71+
try {
72+
await this.internalRefresh(false, SysManagerStrings.sysManagerDiscovering);
73+
} finally {
74+
this._initialized.resolve();
75+
}
7476
}
7577

7678
refresh(_scope: RefreshEnvironmentsScope): Promise<void> {

src/managers/conda/condaEnvManager.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,24 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
8686

8787
this._initialized = createDeferred();
8888

89-
await withProgress(
90-
{
91-
location: ProgressLocation.Window,
92-
title: CondaStrings.condaDiscovering,
93-
},
94-
async () => {
95-
this.collection = await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this);
96-
await this.loadEnvMap();
97-
98-
this._onDidChangeEnvironments.fire(
99-
this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
100-
);
101-
},
102-
);
103-
this._initialized.resolve();
89+
try {
90+
await withProgress(
91+
{
92+
location: ProgressLocation.Window,
93+
title: CondaStrings.condaDiscovering,
94+
},
95+
async () => {
96+
this.collection = await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this);
97+
await this.loadEnvMap();
98+
99+
this._onDidChangeEnvironments.fire(
100+
this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
101+
);
102+
},
103+
);
104+
} finally {
105+
this._initialized.resolve();
106+
}
104107
}
105108

106109
async getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]> {

src/managers/pipenv/pipenvManager.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ export class PipenvManager implements EnvironmentManager {
4949

5050
private _initialized: Deferred<void> | undefined;
5151

52-
constructor(public readonly nativeFinder: NativePythonFinder, public readonly api: PythonEnvironmentApi) {
52+
constructor(
53+
public readonly nativeFinder: NativePythonFinder,
54+
public readonly api: PythonEnvironmentApi,
55+
) {
5356
this.name = 'pipenv';
5457
this.displayName = 'Pipenv';
5558
this.preferredPackageManagerId = 'ms-python.python:pip';
@@ -70,21 +73,24 @@ export class PipenvManager implements EnvironmentManager {
7073

7174
this._initialized = createDeferred();
7275

73-
await withProgress(
74-
{
75-
location: ProgressLocation.Window,
76-
title: PipenvStrings.pipenvDiscovering,
77-
},
78-
async () => {
79-
this.collection = await refreshPipenv(false, this.nativeFinder, this.api, this);
80-
await this.loadEnvMap();
81-
82-
this._onDidChangeEnvironments.fire(
83-
this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
84-
);
85-
},
86-
);
87-
this._initialized.resolve();
76+
try {
77+
await withProgress(
78+
{
79+
location: ProgressLocation.Window,
80+
title: PipenvStrings.pipenvDiscovering,
81+
},
82+
async () => {
83+
this.collection = await refreshPipenv(false, this.nativeFinder, this.api, this);
84+
await this.loadEnvMap();
85+
86+
this._onDidChangeEnvironments.fire(
87+
this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
88+
);
89+
},
90+
);
91+
} finally {
92+
this._initialized.resolve();
93+
}
8894
}
8995

9096
private async loadEnvMap() {

src/managers/poetry/poetryManager.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export class PoetryManager implements EnvironmentManager, Disposable {
4444
private readonly _onDidChangeEnvironments = new EventEmitter<DidChangeEnvironmentsEventArgs>();
4545
public readonly onDidChangeEnvironments = this._onDidChangeEnvironments.event;
4646

47-
constructor(private readonly nativeFinder: NativePythonFinder, private readonly api: PythonEnvironmentApi) {
47+
constructor(
48+
private readonly nativeFinder: NativePythonFinder,
49+
private readonly api: PythonEnvironmentApi,
50+
) {
4851
this.name = 'poetry';
4952
this.displayName = 'Poetry';
5053
this.preferredPackageManagerId = 'ms-python.python:poetry';
@@ -71,21 +74,24 @@ export class PoetryManager implements EnvironmentManager, Disposable {
7174

7275
this._initialized = createDeferred();
7376

74-
await withProgress(
75-
{
76-
location: ProgressLocation.Window,
77-
title: PoetryStrings.poetryDiscovering,
78-
},
79-
async () => {
80-
this.collection = await refreshPoetry(false, this.nativeFinder, this.api, this);
81-
await this.loadEnvMap();
82-
83-
this._onDidChangeEnvironments.fire(
84-
this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
85-
);
86-
},
87-
);
88-
this._initialized.resolve();
77+
try {
78+
await withProgress(
79+
{
80+
location: ProgressLocation.Window,
81+
title: PoetryStrings.poetryDiscovering,
82+
},
83+
async () => {
84+
this.collection = await refreshPoetry(false, this.nativeFinder, this.api, this);
85+
await this.loadEnvMap();
86+
87+
this._onDidChangeEnvironments.fire(
88+
this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
89+
);
90+
},
91+
);
92+
} finally {
93+
this._initialized.resolve();
94+
}
8995
}
9096

9197
async getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]> {

src/managers/pyenv/pyenvManager.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export class PyEnvManager implements EnvironmentManager, Disposable {
4444
private readonly _onDidChangeEnvironments = new EventEmitter<DidChangeEnvironmentsEventArgs>();
4545
public readonly onDidChangeEnvironments = this._onDidChangeEnvironments.event;
4646

47-
constructor(private readonly nativeFinder: NativePythonFinder, private readonly api: PythonEnvironmentApi) {
47+
constructor(
48+
private readonly nativeFinder: NativePythonFinder,
49+
private readonly api: PythonEnvironmentApi,
50+
) {
4851
this.name = 'pyenv';
4952
this.displayName = 'PyEnv';
5053
this.preferredPackageManagerId = 'ms-python.python:pip';
@@ -71,21 +74,24 @@ export class PyEnvManager implements EnvironmentManager, Disposable {
7174

7275
this._initialized = createDeferred();
7376

74-
await withProgress(
75-
{
76-
location: ProgressLocation.Window,
77-
title: PyenvStrings.pyenvDiscovering,
78-
},
79-
async () => {
80-
this.collection = await refreshPyenv(false, this.nativeFinder, this.api, this);
81-
await this.loadEnvMap();
82-
83-
this._onDidChangeEnvironments.fire(
84-
this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
85-
);
86-
},
87-
);
88-
this._initialized.resolve();
77+
try {
78+
await withProgress(
79+
{
80+
location: ProgressLocation.Window,
81+
title: PyenvStrings.pyenvDiscovering,
82+
},
83+
async () => {
84+
this.collection = await refreshPyenv(false, this.nativeFinder, this.api, this);
85+
await this.loadEnvMap();
86+
87+
this._onDidChangeEnvironments.fire(
88+
this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
89+
);
90+
},
91+
);
92+
} finally {
93+
this._initialized.resolve();
94+
}
8995
}
9096

9197
async getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]> {

0 commit comments

Comments
 (0)