[GH-ISSUE #510] [Issue]: Server crash-loops with TDZ ReferenceError when .env auto-create fails (regression from #507) #231

Closed
opened 2026-10-20 17:26:34 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Romuromylus on GitHub (May 7, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/510

Summary

After PR #507, the server crash-loops at startup with ReferenceError: Cannot access 'sharedLogger' before initialization whenever ensureEnvFileExists() fails to write the auto-created .env file (e.g. read-only filesystem, or a Docker user that lacks write permission to the monorepo root).

Expected behavior

Per the comment in ensureEnvFileExists(): "The write is best-effort: read-only filesystems (some Docker images, locked-down installs) silently fall back to 'no .env' mode, which dotenv handles the same as today." Startup should continue without .env, optionally with a warning.

Actual behavior

writeFileSync throws (e.g. EACCES), the catch tries sharedLogger.warn(...), and sharedLogger is in TDZ. The original EACCES is masked by a ReferenceError and Node exits 1 in a tight loop (Docker/Swarm restarts forever).

file:///app/packages/server/dist/config/runtime-config.js:48
        sharedLogger.warn({ err, envPath }, "[runtime-config] Could not auto-create .env file; continuing without it");
        ^

ReferenceError: Cannot access 'sharedLogger' before initialization
    at ensureEnvFileExists (file:///app/packages/server/dist/config/runtime-config.js:48:9)
    at loadRuntimeEnv (file:///app/packages/server/dist/config/runtime-config.js:55:5)
    at file:///app/packages/server/dist/config/runtime-config.js:67:1

Why TDZ

runtime-config.ts and lib/logger.ts form an import cycle:

  • runtime-config.ts imports sharedLogger from lib/logger.js
  • lib/logger.ts imports getLogLevel/getNodeEnv from config/runtime-config.js

packages/server/src/index.ts imports lib/logger.js first (line 7), then config/runtime-config.js (line 8), so the cycle enters through logger.ts. While logger.ts is still mid-evaluation, runtime-config.ts runs to its bottom and hits the top-level loadRuntimeEnv() call. If ensureEnvFileExists's writeFileSync throws, the catch references sharedLogger — but logger.ts hasn't yet executed export const logger = pino(...), so the import binding is uninitialized.

The function declarations on runtime-config.ts work across the cycle (hoisted in linking phase), which is why getLogLevel/getNodeEnv survive — but const/let exports like logger do not.

Steps to reproduce

  1. Build the Docker image (or run as any non-root user without write access to the monorepo root).
  2. Start the container. The node user cannot write /app/.env, so writeFileSync throws EACCES.
  3. Container exits 1 immediately and crash-loops.

Reproduces deterministically on EasyPanel / Docker Swarm with the upstream Dockerfile.

Environment

  • Marinara Engine version: main at 3ed2709 (or any commit including #507's 96ca8d7)
  • Install type: docker (EasyPanel)
  • OS + version: Linux (container)
  • Browser or app shell: N/A — server never reaches a listening state

Logs, screenshots, or video

See stack trace above. docker service ps sillytavern_marinara shows endless Failed → Ready → Failed cycles with task: non-zero exit (1).

Additional context

Suggested fix: defer the sharedLogger.warn call past the import cycle (e.g. wrap it in setImmediate), so the binding is initialized by the time the warn runs. The diagnostic is preserved without crashing startup. PR to follow.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (May 7, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/510 ## Summary After PR #507, the server crash-loops at startup with `ReferenceError: Cannot access 'sharedLogger' before initialization` whenever `ensureEnvFileExists()` fails to write the auto-created `.env` file (e.g. read-only filesystem, or a Docker user that lacks write permission to the monorepo root). ## Expected behavior Per the comment in `ensureEnvFileExists()`: *"The write is best-effort: read-only filesystems (some Docker images, locked-down installs) silently fall back to 'no .env' mode, which dotenv handles the same as today."* Startup should continue without `.env`, optionally with a warning. ## Actual behavior `writeFileSync` throws (e.g. `EACCES`), the catch tries `sharedLogger.warn(...)`, and `sharedLogger` is in TDZ. The original `EACCES` is masked by a `ReferenceError` and Node exits 1 in a tight loop (Docker/Swarm restarts forever). ``` file:///app/packages/server/dist/config/runtime-config.js:48 sharedLogger.warn({ err, envPath }, "[runtime-config] Could not auto-create .env file; continuing without it"); ^ ReferenceError: Cannot access 'sharedLogger' before initialization at ensureEnvFileExists (file:///app/packages/server/dist/config/runtime-config.js:48:9) at loadRuntimeEnv (file:///app/packages/server/dist/config/runtime-config.js:55:5) at file:///app/packages/server/dist/config/runtime-config.js:67:1 ``` ### Why TDZ `runtime-config.ts` and `lib/logger.ts` form an import cycle: - `runtime-config.ts` imports `sharedLogger` from `lib/logger.js` - `lib/logger.ts` imports `getLogLevel`/`getNodeEnv` from `config/runtime-config.js` `packages/server/src/index.ts` imports `lib/logger.js` first (line 7), then `config/runtime-config.js` (line 8), so the cycle enters through `logger.ts`. While `logger.ts` is still mid-evaluation, `runtime-config.ts` runs to its bottom and hits the top-level `loadRuntimeEnv()` call. If `ensureEnvFileExists`'s `writeFileSync` throws, the catch references `sharedLogger` — but `logger.ts` hasn't yet executed `export const logger = pino(...)`, so the import binding is uninitialized. The function declarations on `runtime-config.ts` work across the cycle (hoisted in linking phase), which is why `getLogLevel`/`getNodeEnv` survive — but `const`/`let` exports like `logger` do not. ## Steps to reproduce 1. Build the Docker image (or run as any non-root user without write access to the monorepo root). 2. Start the container. The `node` user cannot write `/app/.env`, so `writeFileSync` throws `EACCES`. 3. Container exits 1 immediately and crash-loops. Reproduces deterministically on EasyPanel / Docker Swarm with the upstream Dockerfile. ## Environment - Marinara Engine version: `main` at `3ed2709` (or any commit including #507's `96ca8d7`) - Install type: docker (EasyPanel) - OS + version: Linux (container) - Browser or app shell: N/A — server never reaches a listening state ## Logs, screenshots, or video See stack trace above. `docker service ps sillytavern_marinara` shows endless `Failed → Ready → Failed` cycles with `task: non-zero exit (1)`. ## Additional context Suggested fix: defer the `sharedLogger.warn` call past the import cycle (e.g. wrap it in `setImmediate`), so the binding is initialized by the time the warn runs. The diagnostic is preserved without crashing startup. PR to follow. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:26:34 +07:00
  • closed this issue
  • added the
    bug
    label
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#231
No description provided.