Skip to content

Potential undefined access in findDefaultModel (missing optional chaining) #309202

@Akash504-ai

Description

@Akash504-ai

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.115.0
  • OS Version: Windows 11

Steps to Reproduce:

  1. Open chatModelSelectionLogic.ts

  2. Locate the findDefaultModel function

  3. Observe the following line:

    return models.find(m => m.metadata.isDefaultForLocation[location]) || models[0];
  4. Notice that isDefaultForLocation is accessed without null/undefined checks


Expected Behavior:

Access to nested properties like isDefaultForLocation should be safe (e.g., using optional chaining) to prevent potential runtime errors when metadata is incomplete or comes from external sources.


Actual Behavior:

The code assumes m.metadata.isDefaultForLocation is always defined.
If it is undefined for any model, this may throw a runtime error:

Cannot read properties of undefined

Additional Information:

Since models can be contributed by external providers, metadata fields may not always be guaranteed. Other parts of the codebase use optional chaining (?.) for similar cases.

Suggested fix:

m.metadata.isDefaultForLocation?.[location]

Additionally, models[0] is used as a fallback without checking if the array is empty, which may lead to unintended undefined returns.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions