[PR #511] [MERGED] fix(runtime-config): defer ensureEnvFileExists warn past import cycle #4649

Closed
opened 2026-10-22 04:09:28 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/511
Author: @Romuromylus
Created: 5/7/2026
Status: ✅ Merged
Merged: 5/7/2026
Merged by: @Romuromylus

Base: main ← Head: fix/runtime-config-tdz


📝 Commits (1)

  • 65db33c fix(runtime-config): defer ensureEnvFileExists warn past import cycle

📊 Changes

1 file changed (+13 additions, -4 deletions)

View changed files

📝 packages/server/src/config/runtime-config.ts (+13 -4)

📄 Description

Linked issue

Closes #510

Why this change

PR #507 added ensureEnvFileExists() to auto-create .env at startup. When the write fails (read-only filesystem, or a Docker user that lacks write permission to the monorepo root — exactly what happens to the node user against root-owned /app in the upstream Dockerfile), the catch references sharedLogger, which is in TDZ at that point in startup. Node throws ReferenceError: Cannot access 'sharedLogger' before initialization, which masks the original EACCES and crash-loops the container with exit 1.

runtime-config.ts and lib/logger.ts form an import cycle. 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 mid-evaluation, runtime-config.ts runs to its bottom-of-file loadRuntimeEnv() call. Function-declaration exports (getLogLevel, getNodeEnv) survive the partial-module access because they're hoisted in the linking phase, but the const export binding for logger is still uninitialized — synchronous access from the catch is a ReferenceError.

Reproduces deterministically on EasyPanel, Docker Swarm, and any container/host where the node user lacks write permission to the resolved MONOREPO_ROOT.

What changed

  • Wrap the sharedLogger.warn call in setImmediate(...) inside ensureEnvFileExists's catch, so the access fires one tick later — after both modules in the cycle finish evaluating.

The diagnostic is preserved; startup no longer crashes on read-only or locked-down filesystems, matching the function comment's promise that the write is best-effort.

Considered alternatives:

  • Drop the warn entirely — simplest fix and matches the "silently fall back" comment, but loses a useful operator-facing diagnostic for a class of misconfiguration that's otherwise hard to spot.
  • Break the cycle by inlining process.env.LOG_LEVEL/NODE_ENV reads in logger.ts — eliminates this whole TDZ class but changes semantics: today the cycle's evaluation order means loadRuntimeEnv() populates process.env from .env before pino() is constructed, so LOG_LEVEL set in .env is honored. Inlining drops that.

The deferred-warn fix is the smallest change that closes the regression without surrendering either the diagnostic or the .env-driven LOG_LEVEL behavior.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Verified on a downstream fork deployed via EasyPanel against the same upstream Dockerfile that crash-loops the unpatched server:

  • Before patch: container exits 1 immediately on startup with the TDZ stack trace; Swarm restarts it endlessly (task: non-zero exit (1)).
  • After patch: container reaches Up, server listens on 0.0.0.0:7860, public endpoint returns the configured 401 (basicAuth gate) instead of the upstream "Not Found" / restart-loop. App is reachable and behaves normally.
  • pnpm check passes locally on the patched tree (TypeScript + ESLint + client/server build).

Reviewer should still tick the boxes after running their own check + container smoke per the template instructions.

Docs and release impact

  • No docs changes needed

UI evidence (if applicable)

N/A — server-side bootstrap fix, no UI surface.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a startup crash that occurred when the application's configuration file couldn't be automatically created.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/511 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/7/2026 **Status:** ✅ Merged **Merged:** 5/7/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `main` ← **Head:** `fix/runtime-config-tdz` --- ### 📝 Commits (1) - [`65db33c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/65db33cb962c34d51825ac6a1434d7cb89e0dbc3) fix(runtime-config): defer ensureEnvFileExists warn past import cycle ### 📊 Changes **1 file changed** (+13 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/config/runtime-config.ts` (+13 -4) </details> ### 📄 Description ## Linked issue Closes #510 ## Why this change PR #507 added `ensureEnvFileExists()` to auto-create `.env` at startup. When the write fails (read-only filesystem, or a Docker user that lacks write permission to the monorepo root — exactly what happens to the `node` user against root-owned `/app` in the upstream Dockerfile), the catch references `sharedLogger`, which is in TDZ at that point in startup. Node throws `ReferenceError: Cannot access 'sharedLogger' before initialization`, which masks the original `EACCES` and crash-loops the container with exit 1. `runtime-config.ts` and `lib/logger.ts` form an import cycle. `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 mid-evaluation, `runtime-config.ts` runs to its bottom-of-file `loadRuntimeEnv()` call. Function-declaration exports (`getLogLevel`, `getNodeEnv`) survive the partial-module access because they're hoisted in the linking phase, but the `const` export binding for `logger` is still uninitialized — synchronous access from the catch is a `ReferenceError`. Reproduces deterministically on EasyPanel, Docker Swarm, and any container/host where the node user lacks write permission to the resolved `MONOREPO_ROOT`. ## What changed - Wrap the `sharedLogger.warn` call in `setImmediate(...)` inside `ensureEnvFileExists`'s catch, so the access fires one tick later — after both modules in the cycle finish evaluating. The diagnostic is preserved; startup no longer crashes on read-only or locked-down filesystems, matching the function comment's promise that the write is best-effort. Considered alternatives: - **Drop the warn entirely** — simplest fix and matches the "silently fall back" comment, but loses a useful operator-facing diagnostic for a class of misconfiguration that's otherwise hard to spot. - **Break the cycle** by inlining `process.env.LOG_LEVEL`/`NODE_ENV` reads in `logger.ts` — eliminates this whole TDZ class but changes semantics: today the cycle's evaluation order means `loadRuntimeEnv()` populates `process.env` from `.env` before `pino()` is constructed, so `LOG_LEVEL` set in `.env` is honored. Inlining drops that. The deferred-warn fix is the smallest change that closes the regression without surrendering either the diagnostic or the `.env`-driven `LOG_LEVEL` behavior. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes Verified on a downstream fork deployed via EasyPanel against the same upstream Dockerfile that crash-loops the unpatched server: - Before patch: container exits 1 immediately on startup with the TDZ stack trace; Swarm restarts it endlessly (`task: non-zero exit (1)`). - After patch: container reaches `Up`, server listens on `0.0.0.0:7860`, public endpoint returns the configured `401` (basicAuth gate) instead of the upstream "Not Found" / restart-loop. App is reachable and behaves normally. - `pnpm check` passes locally on the patched tree (TypeScript + ESLint + client/server build). Reviewer should still tick the boxes after running their own check + container smoke per the template instructions. ## Docs and release impact - [x] No docs changes needed ## UI evidence (if applicable) N/A — server-side bootstrap fix, no UI surface. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed a startup crash that occurred when the application's configuration file couldn't be automatically created. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:09:28 +07:00
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#4649
No description provided.