Skip to content

fix: add ?? [] guards on all environment manager collection assignments#1444

Merged
edvilme merged 2 commits intomainfrom
copilot/fix-null-coalescing-guards
Apr 8, 2026
Merged

fix: add ?? [] guards on all environment manager collection assignments#1444
edvilme merged 2 commits intomainfrom
copilot/fix-null-coalescing-guards

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

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 #1440 from the Conda manager.

Changes

Added ?? [] null-coalescing at every this.collection = await refresh*() assignment across all six environment managers:

Manager File Sites patched
Conda condaEnvManager.ts initialize, refresh, startBackgroundInit
Pyenv pyenvManager.ts initialize, refresh, startBackgroundInit
Poetry poetryManager.ts initialize, refresh
Pipenv pipenvManager.ts initialize, refresh, startBackgroundInit
System Python sysPythonManager.ts internalRefresh
Venv venvManager.ts internalRefresh
// Before
this.collection = await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this);

// After
this.collection = (await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this)) ?? [];

When the refresh function returns a valid array (the normal path), ?? [] is a no-op. This is a purely defensive change with no behavioural impact under normal conditions.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/graphql
    • Triggering command: /usr/bin/gh gh issue list --state open --limit 5 --json number,title,labels (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Problem

Issue #1440 reports a TypeError: Cannot read properties of undefined (reading 'map') originating from the Conda environment manager. While we can't pinpoint the exact crash line without a full stack trace, the error occurs somewhere in the getEnvironments('all')initialize() call chain where .map() is called on this.collection after it's been assigned from an async refresh function.

All environment managers follow the same pattern: this.collection = await refreshXxx(...) followed by this.collection.map(...). If the refresh function ever resolves to undefined (due to an unexpected runtime condition), the subsequent .map() call crashes.

What to fix

Add ?? [] null-coalescing guards at every this.collection = await refresh...() assignment site across all environment managers. This is a zero-risk defensive change — when the refresh function returns a valid array (the normal case), the ?? [] is a no-op.

Files and locations to update:

1. src/managers/conda/condaEnvManager.ts — 3 sites:

  • initialize(): this.collection = await refreshCondaEnvs(false, ...)
  • refresh(): this.collection = await refreshCondaEnvs(true, ...)
  • get()startBackgroundInit: this.collection = await refreshCondaEnvs(false, ...)

2. src/managers/pyenv/pyenvManager.ts — 3 sites:

  • initialize(): this.collection = await refreshPyenv(false, ...)
  • refresh(): this.collection = await refreshPyenv(true, ...)
  • get()startBackgroundInit: this.collection = await refreshPyenv(false, ...)

3. src/managers/poetry/poetryManager.ts — 3 sites:

  • initialize(): this.collection = await refreshPoetry(false, ...)
  • refresh(): this.collection = await refreshPoetry(true, ...)
  • get()startBackgroundInit: this.collection = await refreshPoetry(false, ...)

4. src/managers/pipenv/pipenvManager.ts — 2 sites:

  • initialize(): this.collection = await refreshPipenv(false, ...)
  • refresh(): this.collection = await refreshPipenv(hardRefresh, ...)

5. src/managers/builtin/sysPythonManager.ts — 1 site:

  • internalRefresh(): this.collection = await refreshPythons(hardRefresh, ...)

6. src/managers/builtin/venvManager.ts — 1 site:

  • internalRefresh(): this.collection = await findVirtualEnvironments(hardRefresh, ...)

Example change pattern

Before:

this.collection = await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this);

After:

this.collection = (await refreshCondaEnvs(false, this.nativeFinder, this.api, this.log, this)) ?? [];

Apply this same pattern to every assignment listed above. Do not change anything else — no getter/setter refactoring, no other logic changes. Just add ?? [] to the assignment.

Resolves #1440

The following is the prior conversation context from the user's chat exploration (may be truncated):

Assistant: Now let me look at the refreshCondaEnvs function more closely, particularly where the .map call could fail on undefined:

Assistant: Now let me look at where environments are collected from managers and where the .map call causing the error likely originates. The error message is: Error getting environments from ms-python.python:conda: TypeError: Cannot read properties of undefined (reading 'map'). Let me find where environments are gathered from managers.

Assistant: Now I can see the error context. The issue reports this error from src/helpers.ts:

Error getting environments from ms-python.python:conda: TypeError: Cannot read properties of undefined (reading 'map')

Let me look more closely at the initialize() method and the refreshCondaEnvs path to find where .map is called on something that could be undefined.

Assistant: Now let me look at the initialize() method more closely and the refreshCondaEnvs function to understand what could return undefined that then has .map called on it. The error from the issue is:

TypeError: Cannot read properties of undefined (reading 'map')

Let me check the NativePythonFinder.refresh return type, since that's where the data flows from.

Assistant: Now let me look at the helpers.ts file where the error message is generated for the issue report:

Assistant: Now I have a clear picture. Let me trace the exact error path.

Root Cause Analysis

What failed: The error TypeError: Cannot read properties of undefined (reading 'map') occurs during the Conda environment manager's getEnvironments('all') call, which is invoked by collectEnvironmentInfo() in the issue reporter.

Where it originates: The most likely crash site is in refreshCondaEnvs() in condaUtils.ts. Here's the chain:

  1. CondaEnvManager.getEnvironments('all') (line 111) calls this.initialize()
  2. initialize() (line 84) calls refreshCondaEnvs() (line 98)
  3. refreshCondaEnvs() calls nativeFinder.refresh(false) (line 872 of condaUtils.ts)
  4. The native finder returns data, ...

This pull request was created from Copilot chat.

Copilot AI changed the title [WIP] Fix TypeError by adding null-coalescing guards fix: add ?? [] guards on all environment manager collection assignments Apr 8, 2026
Copilot AI requested a review from edvilme April 8, 2026 01:01
@edvilme edvilme added the bug Issue identified by VS Code Team member as probable bug label Apr 8, 2026
@edvilme edvilme marked this pull request as ready for review April 8, 2026 17:09
@edvilme edvilme merged commit 76c2430 into main Apr 8, 2026
122 of 124 checks passed
@edvilme edvilme deleted the copilot/fix-null-coalescing-guards branch April 8, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue identified by VS Code Team member as probable bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python Environments]

3 participants