Skip to content

Commit 76c2430

Browse files
Copilotedvilme
andauthored
fix: add ?? [] guards on all environment manager collection assignments (microsoft#1444)
If any `refresh*` function resolves to `undefined` at runtime, the subsequent `.map()` / `.filter()` call on `this.collection` throws `TypeError: Cannot read properties of undefined (reading 'map')` — the crash reported in microsoft#1440 from the Conda manager. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
1 parent 25f8721 commit 76c2430

File tree

6 files changed

+14
-14
lines changed

6 files changed

+14
-14
lines changed

src/managers/builtin/sysPythonManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class SysPythonManager implements EnvironmentManager {
115115
const discard = this.collection.map((c) => c);
116116

117117
// hit here is fine...
118-
this.collection = await refreshPythons(hardRefresh, this.nativeFinder, this.api, this.log, this);
118+
this.collection = (await refreshPythons(hardRefresh, this.nativeFinder, this.api, this.log, this)) ?? [];
119119
await this.loadEnvMap();
120120

121121
const args = [

src/managers/builtin/venvManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,14 @@ export class VenvManager implements EnvironmentManager {
336336
environment: env,
337337
}));
338338

339-
this.collection = await findVirtualEnvironments(
339+
this.collection = (await findVirtualEnvironments(
340340
hardRefresh,
341341
this.nativeFinder,
342342
this.api,
343343
this.log,
344344
this,
345345
scope ? [scope] : undefined,
346-
);
346+
)) ?? [];
347347
await this.loadEnvMap();
348348

349349
const added = this.collection.map((env) => ({ environment: env, kind: EnvironmentChangeKind.add }));

src/managers/conda/condaEnvManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
9595
title: CondaStrings.condaDiscovering,
9696
},
9797
async () => {
98-
this.collection = await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this);
98+
this.collection = (await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this)) ?? [];
9999
await this.loadEnvMap();
100100

101101
this._onDidChangeEnvironments.fire(
@@ -246,7 +246,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
246246
async () => {
247247
this.log.info('Refreshing Conda Environments');
248248
const discard = this.collection.map((c) => c);
249-
this.collection = await refreshCondaEnvs(true, this.nativeFinder, this.api, this.log, this);
249+
this.collection = (await refreshCondaEnvs(true, this.nativeFinder, this.api, this.log, this)) ?? [];
250250

251251
await this.loadEnvMap();
252252

@@ -273,7 +273,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
273273
resolve: (p) => resolveCondaPath(p, this.nativeFinder, this.api, this.log, this),
274274
startBackgroundInit: () =>
275275
withProgress({ location: ProgressLocation.Window, title: CondaStrings.condaDiscovering }, async () => {
276-
this.collection = await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this);
276+
this.collection = (await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this)) ?? [];
277277
await this.loadEnvMap();
278278
this._onDidChangeEnvironments.fire(
279279
this.collection.map((e) => ({

src/managers/pipenv/pipenvManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class PipenvManager implements EnvironmentManager, Disposable {
102102
title: PipenvStrings.pipenvDiscovering,
103103
},
104104
async () => {
105-
this.collection = await refreshPipenv(false, this.nativeFinder, this.api, this);
105+
this.collection = (await refreshPipenv(false, this.nativeFinder, this.api, this)) ?? [];
106106
await this.loadEnvMap();
107107

108108
this._onDidChangeEnvironments.fire(
@@ -181,7 +181,7 @@ export class PipenvManager implements EnvironmentManager, Disposable {
181181
async () => {
182182
traceInfo('Refreshing Pipenv Environments');
183183
const oldCollection = [...this.collection];
184-
this.collection = await refreshPipenv(hardRefresh, this.nativeFinder, this.api, this);
184+
this.collection = (await refreshPipenv(hardRefresh, this.nativeFinder, this.api, this)) ?? [];
185185
await this.loadEnvMap();
186186

187187
// Fire change events for environments that were added or removed
@@ -317,7 +317,7 @@ export class PipenvManager implements EnvironmentManager, Disposable {
317317
withProgress(
318318
{ location: ProgressLocation.Window, title: PipenvStrings.pipenvDiscovering },
319319
async () => {
320-
this.collection = await refreshPipenv(false, this.nativeFinder, this.api, this);
320+
this.collection = (await refreshPipenv(false, this.nativeFinder, this.api, this)) ?? [];
321321
await this.loadEnvMap();
322322
this._onDidChangeEnvironments.fire(
323323
this.collection.map((e) => ({

src/managers/poetry/poetryManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class PoetryManager implements EnvironmentManager, Disposable {
100100
title: PoetryStrings.poetryDiscovering,
101101
},
102102
async () => {
103-
this.collection = await refreshPoetry(false, this.nativeFinder, this.api, this);
103+
this.collection = (await refreshPoetry(false, this.nativeFinder, this.api, this)) ?? [];
104104
await this.loadEnvMap();
105105

106106
this._onDidChangeEnvironments.fire(
@@ -172,7 +172,7 @@ export class PoetryManager implements EnvironmentManager, Disposable {
172172
async () => {
173173
traceInfo('Refreshing Poetry Environments');
174174
const discard = this.collection.map((c) => c);
175-
this.collection = await refreshPoetry(true, this.nativeFinder, this.api, this);
175+
this.collection = (await refreshPoetry(true, this.nativeFinder, this.api, this)) ?? [];
176176

177177
await this.loadEnvMap();
178178

src/managers/pyenv/pyenvManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class PyEnvManager implements EnvironmentManager, Disposable {
100100
title: PyenvStrings.pyenvDiscovering,
101101
},
102102
async () => {
103-
this.collection = await refreshPyenv(false, this.nativeFinder, this.api, this);
103+
this.collection = (await refreshPyenv(false, this.nativeFinder, this.api, this)) ?? [];
104104
await this.loadEnvMap();
105105

106106
this._onDidChangeEnvironments.fire(
@@ -170,7 +170,7 @@ export class PyEnvManager implements EnvironmentManager, Disposable {
170170
async () => {
171171
traceInfo('Refreshing Pyenv Environments');
172172
const discard = this.collection.map((c) => c);
173-
this.collection = await refreshPyenv(true, this.nativeFinder, this.api, this);
173+
this.collection = (await refreshPyenv(true, this.nativeFinder, this.api, this)) ?? [];
174174

175175
await this.loadEnvMap();
176176

@@ -198,7 +198,7 @@ export class PyEnvManager implements EnvironmentManager, Disposable {
198198
resolve: (p) => resolvePyenvPath(p, this.nativeFinder, this.api, this),
199199
startBackgroundInit: () =>
200200
withProgress({ location: ProgressLocation.Window, title: PyenvStrings.pyenvDiscovering }, async () => {
201-
this.collection = await refreshPyenv(false, this.nativeFinder, this.api, this);
201+
this.collection = (await refreshPyenv(false, this.nativeFinder, this.api, this)) ?? [];
202202
await this.loadEnvMap();
203203
this._onDidChangeEnvironments.fire(
204204
this.collection.map((e) => ({

0 commit comments

Comments
 (0)