sessions: guard folders[0] access with optional chaining#323145
sessions: guard folders[0] access with optional chaining#323145ricardoofnl wants to merge 2 commits into
Conversation
A session workspace's folders array can be empty, so accessing folders[0].gitRepository without optional chaining throws a TypeError. Use folders[0]?.gitRepository to match the null-safe convention used elsewhere in src/vs/sessions/.
There was a problem hiding this comment.
Pull request overview
Guards access to workspace.folders[0] in the Sessions (Agents window) codepath to prevent TypeError crashes when a session workspace exists but contains zero folders (matching the existing null-safe convention in src/vs/sessions/).
Changes:
- Adds optional chaining to
folders[0]?.gitRepositoryin the Changes view’s “has git repository” observable. - Adds optional chaining to
folders[0]?.gitRepositoryin Copilot Chat sessions changeset resolvers/changesets to avoid crashes for empty-folder workspaces.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/vs/sessions/contrib/providers/copilotChatSessions/browser/copilotChatSessionsChangesets.ts | Makes git repository lookups null-safe when the workspace has an empty folders array. |
| src/vs/sessions/contrib/changes/browser/changesViewService.ts | Prevents the Changes view from throwing when checking for a git repo with an empty folders array. |
@microsoft-github-policy-service agree |
Fixes #323144
Summary
A session workspace's
foldersarray can be empty (the type isreadonly folders: ISessionFolder[], with no non-empty guarantee), so accessingfolders[0].gitRepositorywithout optional chaining throws aTypeErrorwhen a session has a workspace but no folders. This can crash the Changes view, the session header, and the changeset resolvers.This change adds optional chaining to the 5 spots that diverged from the null-safe convention used everywhere else in
src/vs/sessions/(which even guards explicitly withworkspace.folders.length > 0insessionsActions.ts).Changes
changesViewService.ts:86—folders[0].gitRepository→folders[0]?.gitRepositorycopilotChatSessionsChangesets.ts(lines 30, 60, 190, 242) — same null-safe accessVerification
folders[0]accesses remain insrc/vs/sessions/.tsc -p srcproduces no errors for the changed files.