1# drizzle-schema-chunks
2
3Fixture for [#749](https://github.com/alchemy-run/alchemy/issues/749).
4
5Cloudflare Worker `ScriptStartupError` after Alchemy/Rolldown bundling when
6top-level Drizzle schema modules are code-split into a chunk separate from
7`drizzle-orm`. Cross-chunk evaluation in workerd leaves class bindings
8incomplete (`PgSerialBuilder is not a constructor`, or the classic
9`Cannot access '<minified>' before initialization` TDZ).
10
11## Layout
12
13Mirrors the reporter's monorepo shape:
14
15- `schema/*` — db package tables (`pgTable` at module scope)
16- `auth/*` — auth package tables that cross-import the db schema
17- `worker.ts` — Worker entry that imports the full graph
18
19## How the test pins the fix
20
21Small graphs stay single-chunk under Alchemy's default Worker bundler, so
22`DrizzleSchemaChunks.test.ts` deploys a stack whose `Cloudflare.Worker`
23*forces* the issue's chunk layout via `build` options: the schema group
24captures only the schema modules (`includeDependenciesRecursively: false`),
25leaving `drizzle-orm` in the entry chunk. The resulting graph is **cyclic**
26(`worker.js -> auth-*.js -> worker.js`), so ESM evaluation runs the schema
27chunk before drizzle's class bindings initialize — the TDZ. Cloudflare's
28script-startup validation runs on exactly those chunks at upload, so the
29deploy itself is the regression assertion; the test then verifies the
30cyclic layout on disk and fetches the worker.
31
32The cycle is essential: an *acyclic* split (e.g. drizzle in its own chunk
33imported by the schema chunk) evaluates correctly under plain import order
34and never triggers the bug, with or without `strictExecutionOrder`. Setting
35`strictExecutionOrder: false` on the stack's `build.output` restores the
36exact issue error:
37
38```
39ScriptStartupError: Uncaught ReferenceError: Cannot access 'a' before initialization
40 at auth-BFaahPAe.js:1:110
41```
42
43## The fix
44
45`WorkerBundle` sets `strictExecutionOrder: true` on its rolldown output
46options, which wraps cross-chunk modules so evaluation follows ESM semantics
47regardless of how the graph was chunked. Before that default, this exact
48split failed Cloudflare startup validation. The user-side `advancedChunks`
49grouping workaround from the issue is no longer necessary.