Skip to content

Conversation

@ammar-agent
Copy link
Collaborator

Problem

When switching between branches with different config formats, the app crashes with:

Cannot read properties of undefined (reading 'split')
at AppInner (App.tsx:684:100)

This occurs because newer branches may add fields to the workspace config that older code doesn't expect (e.g., namedWorkspacePath, id, name).

Solution

Add optional chaining (?.) to all workspacePath.split() calls and provide sensible fallbacks:

// Before
selectedWorkspace.workspacePath.split("/").pop()

// After  
selectedWorkspace.workspacePath?.split("/").pop() ?? selectedWorkspace.workspaceId

Changes

  • App.tsx: Made workspacePath access defensive in ErrorBoundary and AIView
  • utils/commands/sources.ts: Made workspacePath access defensive in all command sources

Testing

  • make typecheck passes ✅
  • Allows switching between branches without config-related crashes
  • Handles both old format (only path) and new format (id, name, path)

Impact

This is a defensive improvement that makes the app more robust. No behavior changes for normal operation - only prevents crashes when config format mismatches occur.

Make workspace path access defensive with optional chaining to prevent
crashes when switching between branches with different config formats.

Changes:
- Add optional chaining (?.) to all workspacePath.split() calls
- Provide fallbacks using workspaceId when path is undefined
- Affects App.tsx and utils/commands/sources.ts

This allows the app to work with both:
- Old config format (only 'path' field)
- New config format (with 'id', 'name', and 'path' fields)

Prevents 'Cannot read properties of undefined' errors when config
has fields that the current code version doesn't expect.
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR.

Don't render AIView if workspacePath is undefined to prevent crashes
in features that depend on a valid path (like terminal operations).

This ensures we show the welcome screen instead of attempting to render
a workspace view with invalid/missing data.
@ammar-agent ammar-agent added this pull request to the merge queue Oct 16, 2025
Merged via the queue into main with commit 93d2670 Oct 16, 2025
8 checks passed
@ammar-agent ammar-agent deleted the config-robustness branch October 16, 2025 00:57
ammario pushed a commit that referenced this pull request Oct 16, 2025
## Problem

After PR #273 merged, additional compatibility issues were discovered:

```
Cannot read properties of undefined (reading 'slice')
at App.tsx:374:29
```

This occurs when `projectConfig.workspaces` is undefined due to config
format differences between branches.

## Root Cause

The config loader uses type assertions without validation:
```typescript
const projectsMap = new Map<string, ProjectConfig>(
  parsed.projects as Array<[string, ProjectConfig]>
);
```

If loaded config has `ProjectConfig` with missing `workspaces` field,
all array operations fail.

## Solution

Add `?? []` fallback to **all** array operations on
`projectConfig.workspaces`:

### Files Fixed

**App.tsx:**
- Line 242: `.find()` → `?? []).find()`
- Line 374: `.slice()` → `?? []).slice()`

**ProjectSidebar.tsx:**
- Line 823: `.map()` → `?? []).map()`

**ipcMain.ts:**
- Line 225: `.push()` → Initialize if undefined first
- Line 300: `.findIndex()` → `?? []).findIndex()`
- Line 367: Array assignment → Guard with existence check
- Line 808-810: `.length` and `.filter()` → `?? []).length` and `??
[]).filter()`
- Line 925-927: `.length` in error message → `?? []).length`

**config.ts:**
- Line 185: `for...of` loop → `?? [])`

## Testing

- ✅ `make typecheck` passes
- ✅ `make static-check` passes
- ✅ All array operations handle undefined gracefully
- ✅ Works with both old (no workspaces) and new (with workspaces)
configs

## Impact

Prevents all `.slice()`, `.map()`, `.filter()`, `.find()`, `.length`
crashes when switching between branches with different config formats.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants