INFINITE LOOP FIX PROJECT SWITCHER

Comprehensive documentation system guides

๐Ÿ”ฅ Infinite Loop Fix - ProjectSwitcherComponent

Date: October 28, 2025
Issue: Infinite loop with null pointer exception
Status: โœ… FIXED


๐Ÿ› Problem

Error Message

ERROR TypeError: Cannot read properties of null (reading 'split')
    at ProjectSwitcherComponent2.formatDisplayNameFallback (project-switcher.component.ts:226:8)
    at ProjectSwitcherComponent2.formatDisplayName (project-switcher.component.ts:204:17)
    at Object.computation (project-switcher.component.ts:123:17)

Root Cause

When no project was selected (on initial load or after navigation to /select-project), the currentProject computed property returned null. The currentProjectDisplay computed then tried to format this null value, calling .split('-') on null, causing the error and infinite loop.

Call Stack

currentProjectDisplay computed
  โ†’ currentProject() returns null
    โ†’ formatDisplayName(null)
      โ†’ formatDisplayNameFallback(null)
        โ†’ null.split('-') โŒ CRASH
          โ†’ Angular change detection retries
            โ†’ Infinite loop ๐Ÿ”„

โœ… Solution

Changes Made

File: apps/docs-angular/src/app/shared/project-switcher/project-switcher.component.ts

1. Early Return in currentProjectDisplay Computed

readonly currentProjectDisplay = computed(() => {
  const currentProject = this.currentProject();
  
  // ๐ŸŽฏ NULL SAFETY: Handle no project selected
  if (!currentProject) {
    return 'Select Project';
  }

  // ... rest of logic
});

2. Null-Safe Type Signatures

// Updated method signatures to accept null
private formatDisplayName(projectName: string | null): string {
  return this.formatDisplayNameFallback(projectName);
}

private formatDisplayNameFallback(projectName: string | null): string {
  // ๐ŸŽฏ NULL SAFETY: Handle null/undefined project names
  if (!projectName) {
    return 'Select Project';
  }
  
  // ... rest of logic with safe .split()
}

๐ŸŽฏ Defense in Depth

The fix implements 2 layers of null safety:

Layer 1: Computed Property (Primary Defense)

Layer 2: Format Methods (Secondary Defense)


๐Ÿงช Testing

Before Fix

After Fix


๐Ÿ“Š Impact

Component Before After
Stability โŒ Crash on null โœ… Graceful handling
User Experience โŒ Blank screen โœ… "Select Project" shown
Console โŒ Error flood โœ… Clean
Performance โŒ Infinite loop โœ… Normal

๐Ÿ” Related Issues

This fix is part of the larger Project-Scoped Architecture implementation where:


๐Ÿ“ Lessons Learned

  1. Always handle null in computed properties - Especially when reading from state that may not be initialized
  2. Defense in depth - Multiple layers of null checks prevent cascading failures
  3. Friendly fallbacks - Show meaningful text instead of crashing
  4. Type safety - Use string | null to make nullability explicit

โœ… Verification

# Build succeeds
npx nx build docs-angular --configuration=development
โœ… SUCCESS

# No TypeScript errors
# No runtime errors on load
# "Select Project" displays correctly when no project selected

Status: โœ… FIXED
Build: โœ… SUCCESS
Ready for: ๐Ÿงช TESTING