nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Crash after start if bun

Open pomazanbohdan opened this issue 11 months ago • 4 comments

I launch nanocoder, it asks for access to the current directory and immediately crashes into the console.

pomazanbohdan avatar Dec 08 '25 18:12 pomazanbohdan

Hi @pomazanbohdan - thanks for reporting this. Are you able to report which OS and terminal application you're using? I can then get this explored.

Additionally, you're saying you get this if you install via Bun?

Thanks :)

will-lamerton avatar Dec 08 '25 21:12 will-lamerton

Win 11 25, wsl, oracle linux 9, proto install bun, bun i -g *nanocoder

pomazanbohdan avatar Dec 09 '25 06:12 pomazanbohdan

Root Cause Analysis

Summary

The crash is caused by Pino's transport API incompatibility with the Bun runtime. The application calls pino.transport() early in the startup sequence, before any UI renders or error handling can catch the failure.

Crash Flow

1. User runs: bun nanocoder
2. cli.tsx renders <App />
3. App.tsx:43 - Logger created in useMemo()
   └── const logger = useMemo(() => createPinoLogger(), []);
4. pino-logger.ts:82-84 - pino.transport({ target: 'pino/file', ... })
5. ❌ Bun throws: "unable to determine transport target for 'pino/file'"
6. Unhandled exception, app crashes before UI renders

Technical Cause

Pino transports use Node.js worker threads internally. Bun's worker thread implementation differs, causing pino.transport() to fail when dynamically resolving the 'pino/file' target at runtime.

Known Bun/Pino incompatibility issues:

Related Issue: #241

This issue is related to #241 (LoggerProvider async loading bug). The codebase has a LoggerProvider class with a fallback console logger that could prevent this crash, but:

  1. App.tsx bypasses it - calls createPinoLogger() directly with no fallback
  2. LoggerProvider itself is broken - #241 documents a flag logic bug where _dependenciesLoaded is set before async loading begins, causing real dependencies to never load

Proposed Fix (Single PR for both issues)

A single PR could close both #128 and #241 by:

  1. Fix LoggerProvider's flag logic (#241):

    • Use separate flags: _fallbackInitialized and _realDependenciesLoaded
    • Allow async Pino loading to complete properly
  2. Use LoggerProvider in App.tsx (#128):

    • Replace direct createPinoLogger() call with LoggerProvider.getInstance().getLogger()
    • Provides graceful fallback when Pino transport fails
  3. Add Bun runtime detection (optional enhancement):

    const isBun = typeof globalThis.Bun !== 'undefined';
    if (isBun) {
        // Skip pino.transport(), use console fallback
    }
    

Expected Result After Fix

Runtime Current Behavior After Fix
Node.js ✅ Works ✅ Works (Pino file logging)
Bun ❌ Crashes ✅ Works (console fallback)

Workaround (for now)

Install with Node.js instead of Bun:

npm i -g nanocoder

Files Involved

  • source/app/App.tsx:43 - Logger initialization
  • source/utils/logging/pino-logger.ts:74-84 - Transport creation (crash site)
  • source/utils/logging/logger-provider.ts - Fallback mechanism (broken, see #241)

JimStenstrom avatar Dec 29 '25 13:12 JimStenstrom

Confirmation

Tested locally with Bun v1.3.4 on Linux. The root cause is confirmed:

Node.js:

Testing pino.transport() with Bun...
✅ Transport created successfully
✅ Logger works

Bun:

Testing pino.transport() with Bun...
✅ Transport created successfully
✅ Logger works
❌ Cannot find package 'real-require' from '[email protected]/lib/worker.js'
❌ the worker thread exited

Pino's thread-stream dependency uses worker threads and real-require, which Bun doesn't support properly. The transport appears to initialize but crashes when the worker thread tries to start.

This confirms the fix should add a fallback logger path when pino.transport() fails, which ties into #241.

JimStenstrom avatar Dec 29 '25 13:12 JimStenstrom