Skip to content

fix(ui): defer fs.constants eval to prevent browser bundle crash - #48930

Merged
steipete merged 2 commits into
openclaw:mainfrom
Valentinws:fix/browser-bundle-fs-constants-crash
Apr 25, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
Valentinws:fix/browser-bundle-fs-constants-crash

Conversation

@Valentinws

Copy link
Copy Markdown
Contributor

Summary

The control UI shows a black screen and throws in the browser console:

TypeError: Cannot read properties of undefined (reading 'W_OK')
    at index-BURQWco-.js:1198

Root cause: src/infra/tmp-openclaw-dir.ts evaluates fs.constants.W_OK at module load time via a top-level const. When Vite bundles this for the browser, node:fs resolves to a stub where fs.constants is undefined, so the initializer throws on import before any code runs.

The import chain introduced in a33caab pulls this into the browser bundle:

thinking.ts → provider-runtime.ts → providers.ts
  → subsystem.ts → logger.ts → tmp-openclaw-dir.ts

Note: logger.ts already has a canUseNodeFs() guard that prevents calling resolvePreferredOpenClawTmpDir() in the browser — but that guard is never reached because the crash happens earlier when tmp-openclaw-dir.ts is first imported.

Fix: move TMP_DIR_ACCESS_MODE inside resolvePreferredOpenClawTmpDir() so fs.constants is only accessed when the function is called (Node.js runtime), not when the module is imported (browser bundle).

Test plan

  • src/infra/tmp-openclaw-dir.test.ts passes (all 20 tests)
  • Control UI loads without the W_OK TypeError
  • Gateway dashboard connects and renders correctly

🤖 Generated with Claude Code

@greptile-apps

greptile-apps Bot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a browser bundle crash caused by fs.constants.W_OK being evaluated at module load time. The single-line change moves TMP_DIR_ACCESS_MODE from a module-level const into the body of resolvePreferredOpenClawTmpDir(), so fs.constants is only accessed during a Node.js call rather than at import time (when Vite's browser stub leaves fs.constants as undefined).

  • The fix is minimal and correctly targets the root cause: deferred evaluation ensures the module is safe to import in browser bundles without altering Node.js runtime behavior.
  • fs.constants.W_OK | fs.constants.X_OK are static integer values, so re-computing them on each call has no meaningful performance impact.
  • The existing canUseNodeFs() guard in logger.ts already prevents resolvePreferredOpenClawTmpDir() from being called in the browser, so the fix restores the correct layered defence.
  • The added comment clearly explains the rationale for the placement change.

Confidence Score: 5/5

  • This PR is safe to merge — it is a minimal, correctly-targeted one-line fix with no behavioural change on Node.js and a clear improvement for browser bundles.
  • The change is a straightforward deferral of a constant initializer. The constant's value is identical on every call (static bit-flags), correctness is unchanged, all 20 existing tests pass, and the PR description thoroughly explains both the root cause and the fix.
  • No files require special attention.

Last reviewed commit: af3ef34

@steipete

Copy link
Copy Markdown
Contributor

Codex deep review: this still looks like a valid, narrow fix.

What I verified on current main:

  • src/infra/tmp-openclaw-dir.ts still evaluates fs.constants.W_OK | fs.constants.X_OK at module load time.
  • Browser-safe logger imports are already guarded so resolvePreferredOpenClawTmpDir() should not run in browser contexts, but the top-level initializer still defeats that layering when a browser bundler stubs node:fs with incomplete constants.
  • Moving the mode calculation inside resolvePreferredOpenClawTmpDir() keeps Node behavior identical and only defers the Node-only access until the guarded code path is actually called.

I would still want this refreshed before landing because the PR is old and CI currently reports UNSTABLE/red on some lanes, but the code change itself is the right shape. A focused test would be ideal if we have a cheap way to import this module with an fs stub missing constants; otherwise the existing browser-safe logger import coverage plus the small diff is acceptable.

vaba and others added 2 commits April 25, 2026 07:00
The control UI crashes on load with:

  TypeError: Cannot read properties of undefined (reading 'W_OK')

Root cause: `tmp-openclaw-dir.ts` evaluates `fs.constants.W_OK` at
module load time via a top-level const. When Vite bundles this file
for the browser, `node:fs` resolves to a stub where `fs.constants`
is undefined, so the const initializer throws immediately on import.

The import chain that pulls this into the browser bundle:

  thinking.ts → provider-runtime.ts → providers.ts
    → subsystem.ts → logger.ts → tmp-openclaw-dir.ts

Fix: move TMP_DIR_ACCESS_MODE inside resolvePreferredOpenClawTmpDir()
so fs.constants is only accessed when the function is called in a
Node.js context, not when the module is imported.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@steipete
steipete force-pushed the fix/browser-bundle-fs-constants-crash branch from af3ef34 to 2510bec Compare April 25, 2026 06:01
@steipete
steipete merged commit 4a68fa3 into openclaw:main Apr 25, 2026
9 checks passed
@steipete

steipete commented Apr 25, 2026 •

Copy link
Copy Markdown
Contributor

Landed. Thanks @Valentinws.

Codex follow-up added a regression test that bundles src/infra/tmp-openclaw-dir.ts with browser-style node:fs/node:os/node:path shims and imports the result, so the original fs.constants import-time crash is covered.

Local verification before merge:

  • pnpm test src/infra/tmp-openclaw-dir.browser-import.test.ts src/infra/tmp-openclaw-dir.test.ts src/logging/logger.browser-import.test.ts
  • pnpm test src/infra/run-node.test.ts -t "serializes runtime postbuild restaging|forwards wrapper SIGTERM"
  • pnpm build

Note: full pnpm check:changed passed typecheck/lint/import-cycle/guards but the broad infra changed-test shard hit two unrelated run-node.test.ts 120s timeouts under shard pressure; both timed-out tests passed immediately in the focused rerun above.

Merge commit: 4a68fa3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants