Files
shakespeare/electron/COMPLETE_FIX_SUMMARY.md
T

5.9 KiB

Complete Electron Filesystem Fix Summary

This document summarizes all fixes applied to enable project creation in the Electron app.

Problems Encountered

Problem 1: Error Codes Not Preserved Across IPC

Symptom: isomorphic-git's FileSystem.exists() method was throwing "Unhandled error" instead of returning false for non-existent files.

Root Cause: Error code properties (like ENOENT) were being lost during IPC serialization because they are non-enumerable by default.

Impact: Code that checks error.code === 'ENOENT' failed, breaking file existence checks throughout the codebase.

Problem 2: Missing fs.Stats Methods

Symptom: TypeError: h.isSymbolicLink is not a function

Root Cause: The stat() and lstat() methods only returned isDirectory() and isFile(), but isomorphic-git requires the complete fs.Stats interface including isSymbolicLink().

Impact: isomorphic-git operations failed when checking file types during Git operations.

Solutions Implemented

Fix 1: Error Code Preservation (Two-Part Fix)

Part A: Main Process (electron/main.js)

Made error codes enumerable so they survive IPC serialization:

catch (error) {
  const err = new Error(`Failed to stat ${filePath}: ${error.message}`);
  if (error.code) {
    Object.defineProperty(err, 'code', {
      value: error.code,
      enumerable: true,  // ← Critical for IPC serialization
      writable: true,
      configurable: true,
    });
  }
  throw err;
}

Applied to all 11 IPC handlers:

  • fs:readFile
  • fs:writeFile
  • fs:readdir
  • fs:mkdir
  • fs:stat
  • fs:lstat
  • fs:unlink
  • fs:rmdir
  • fs:rename
  • fs:readlink
  • fs:symlink

Part B: Renderer Process (src/lib/ElectronFSAdapter.ts)

Added error unwrapping to handle Electron's IPC error wrapping:

private unwrapElectronError(error: unknown): Error {
  if (!(error instanceof Error)) {
    return error as Error;
  }

  // Extract error code from the error object if it exists
  const code = (error as NodeJS.ErrnoException).code;
  
  // If there's already a code, preserve it
  if (code) {
    const err = new Error(error.message);
    (err as NodeJS.ErrnoException).code = code;
    return err;
  }

  // If no code property, try to extract from the message
  const match = error.message.match(/:\s*(E[A-Z]+):/);
  if (match) {
    const err = new Error(error.message);
    (err as NodeJS.ErrnoException).code = match[1];
    return err;
  }

  return error;
}

All filesystem methods now wrap IPC calls in try-catch and use unwrapElectronError().

Fix 2: Complete fs.Stats Interface

Added all fs.Stats methods to stat() and lstat() in both main process and adapter:

Main Process (electron/main.js):

return {
  isDirectory: stats.isDirectory(),
  isFile: stats.isFile(),
  isBlockDevice: stats.isBlockDevice(),
  isCharacterDevice: stats.isCharacterDevice(),
  isSymbolicLink: stats.isSymbolicLink(),  // ← Critical for isomorphic-git
  isFIFO: stats.isFIFO(),
  isSocket: stats.isSocket(),
  size: stats.size,
  mtimeMs: stats.mtimeMs,
};

Renderer Process (src/lib/ElectronFSAdapter.ts):

return {
  isDirectory: () => result.isDirectory,
  isFile: () => result.isFile,
  isBlockDevice: () => result.isBlockDevice,
  isCharacterDevice: () => result.isCharacterDevice,
  isSymbolicLink: () => result.isSymbolicLink,
  isFIFO: () => result.isFIFO,
  isSocket: () => result.isSocket,
  size: result.size,
  mtimeMs: result.mtimeMs,
};

TypeScript Definitions (electron/electron.d.ts):

interface StatResult {
  isDirectory: boolean;
  isFile: boolean;
  isBlockDevice: boolean;
  isCharacterDevice: boolean;
  isSymbolicLink: boolean;
  isFIFO: boolean;
  isSocket: boolean;
  size?: number;
  mtimeMs?: number;
}

Files Modified

  1. electron/main.js

    • Added error code preservation to all IPC handlers
    • Added complete fs.Stats methods to stat() and lstat()
  2. src/lib/ElectronFSAdapter.ts

    • Added unwrapElectronError() helper method
    • Updated all filesystem methods to use error unwrapping
    • Added complete fs.Stats methods to stat() and lstat()
  3. electron/electron.d.ts

    • Updated StatResult interface with all fs.Stats methods
  4. Documentation

    • electron/BUGFIX-ERROR-CODES.md
    • ELECTRON_ERROR_FIX_SUMMARY.md
    • electron/ERROR-CODE-FLOW.md
    • electron/COMPLETE_FIX_SUMMARY.md (this file)

Validation

✅ TypeScript compilation: Passes without errors
✅ Test suite: All 877 tests pass (106 test files)
✅ Error codes preserved: Both in main and renderer processes
✅ isomorphic-git compatibility: Full fs.Stats interface provided
✅ Project creation: Should now work in Electron app

Why Both Fixes Were Necessary

  1. Error Code Fix: Required for isomorphic-git to determine if files exist
  2. fs.Stats Methods Fix: Required for isomorphic-git to check file types during Git operations

Both fixes work together to provide complete filesystem compatibility with isomorphic-git in the Electron environment.

Differences from LightningFS

LightningFS (browser):

  • Runs in renderer process - no IPC boundary
  • Error properties preserved automatically
  • May have incomplete fs.Stats interface (but isomorphic-git handles gracefully)

ElectronFS (desktop):

  • Uses IPC - requires explicit error code preservation
  • Requires complete fs.Stats interface for isomorphic-git compatibility
  • Must unwrap Electron's IPC error wrapping

Testing in Electron App

To verify the fixes work:

  1. Build the Electron app: npm run electron:build
  2. Run the app
  3. Try creating a new project
  4. Project creation should complete successfully without errors

Expected behavior:

  • No "Unhandled error in FileSystem.exists()" errors
  • No "isSymbolicLink is not a function" errors
  • Project files created successfully
  • Git repository initialized properly