fix: ensure clientDir exists before writing _headers file#292
fix: ensure clientDir exists before writing _headers file#292james-elicx merged 1 commit intocloudflare:mainfrom
Conversation
The `closeBundle` hook writes `_headers` to `clientDir` (`dist/client/`)
without creating the directory first. Only `distDir` (`dist/`) is checked
earlier. When build cache is invalidated, `dist/client/` may not exist yet,
causing an ENOENT error on `writeFileSync`.
Add `fs.mkdirSync(clientDir, { recursive: true })` before the write.
`recursive: true` is a no-op if the directory already exists, so this is
safe on warm builds.
There was a problem hiding this comment.
Pull request overview
Fixes a build-time failure in the vinext:cloudflare-build closeBundle hook by ensuring the client output directory exists before writing the Cloudflare _headers file (used for immutable caching of content-hashed assets).
Changes:
- Create
dist/client/(clientDir) recursively before writingdist/client/_headersto preventENOENTon cold builds / cache invalidation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
/bigbonk review |
commit: |
There was a problem hiding this comment.
Clean, correct fix. The closeBundle hook guards distDir existence at line 3457 but never ensures clientDir (dist/client/) exists before writing _headers. All reads from clientDir earlier in the hook are individually guarded by existsSync, so they degrade gracefully — but the writeFileSync at line 3574 is the one unguarded write and will throw ENOENT when the directory is missing.
mkdirSync with recursive: true is the right call — idempotent on warm builds, creates the directory on cold builds. No concerns.
|
Approved. The fix is correct and minimal:
|
Summary
closeBundlehook writes_headerstoclientDir(dist/client/) without creating the directory first. OnlydistDir(dist/) is checked earlier in the hook.dist/client/may not exist yet, causing an ENOENT error onwriteFileSync.fs.mkdirSync(clientDir, { recursive: true })before the write.recursive: trueis a no-op if the directory already exists, so this is safe on warm builds.Reproduction
dist/entirely (simulating cache invalidation)bun run buildENOENT: no such file or directory, open 'dist/client/_headers'Fix
Single line addition at
packages/vinext/src/index.ts— ensures the directory exists before writing.Test plan
rm -rf dist && bun run build) succeeds without ENOENTbun run buildwith existingdist/) still succeeds (no-opmkdirSync)dist/client/_headersfile is generated with correct cache headers