fix: add ?? [] guards on all environment manager collection assignments#1444
Merged
fix: add ?? [] guards on all environment manager collection assignments#1444
?? [] guards on all environment manager collection assignments#1444Conversation
…ection assignments (fixes #1440) Agent-Logs-Url: https://github.com/microsoft/vscode-python-environments/sessions/fb299e37-208e-4102-98c3-260fe06e03a7 Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix TypeError by adding null-coalescing guards
fix: add Apr 8, 2026
?? [] guards on all environment manager collection assignments
edvilme
approved these changes
Apr 8, 2026
karthiknadig
approved these changes
Apr 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If any
refresh*function resolves toundefinedat runtime, the subsequent.map()/.filter()call onthis.collectionthrowsTypeError: Cannot read properties of undefined (reading 'map')— the crash reported in #1440 from the Conda manager.Changes
Added
?? []null-coalescing at everythis.collection = await refresh*()assignment across all six environment managers:condaEnvManager.tsinitialize,refresh,startBackgroundInitpyenvManager.tsinitialize,refresh,startBackgroundInitpoetryManager.tsinitialize,refreshpipenvManager.tsinitialize,refresh,startBackgroundInitsysPythonManager.tsinternalRefreshvenvManager.tsinternalRefreshWhen 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/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 thegetEnvironments('all')→initialize()call chain where.map()is called onthis.collectionafter it's been assigned from an async refresh function.All environment managers follow the same pattern:
this.collection = await refreshXxx(...)followed bythis.collection.map(...). If the refresh function ever resolves toundefined(due to an unexpected runtime condition), the subsequent.map()call crashes.What to fix
Add
?? []null-coalescing guards at everythis.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:
After:
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
refreshCondaEnvsfunction more closely, particularly where the.mapcall could fail onundefined:Assistant: Now let me look at where environments are collected from managers and where the
.mapcall 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:Let me look more closely at the
initialize()method and therefreshCondaEnvspath to find where.mapis called on something that could beundefined.Assistant: Now let me look at the
initialize()method more closely and therefreshCondaEnvsfunction to understand what could returnundefinedthat then has.mapcalled on it. The error from the issue is:Let me check the
NativePythonFinder.refreshreturn type, since that's where the data flows from.Assistant: Now let me look at the
helpers.tsfile 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'sgetEnvironments('all')call, which is invoked bycollectEnvironmentInfo()in the issue reporter.Where it originates: The most likely crash site is in
refreshCondaEnvs()incondaUtils.ts. Here's the chain:CondaEnvManager.getEnvironments('all')(line 111) callsthis.initialize()initialize()(line 84) callsrefreshCondaEnvs()(line 98)refreshCondaEnvs()callsnativeFinder.refresh(false)(line 872 ofcondaUtils.ts)This pull request was created from Copilot chat.