Repository navigation
Commit ab3c776
fix(cli): two builds of an unchanged app ship one legacy runtime module, so its hashed name and runtimeModule ref stop changing (#26547)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"}
-->
_Requested by **Leehom** · [project
thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1X7csHsNjL2aQgnCaNu1p3hu)_
Fixes #26478
Clause-②: no
Metadata-protocol: none
## 维护者速读
- **改了什么:** `os build` 打包旧版运行时模块时,临时入口不再以「进程号+时间戳」命名写到配置旁边,而是以固定名经
esbuild stdin 传入;模块里的路径一律相对配置所在目录。
- **为什么改:** 同一个应用每次重建都会产出新的 `objectstack-runtime.HASH.mjs` 文件名和新的
`runtimeModule` 引用,缓存与部署 diff 永远不收敛(#7651 当年缺的复现就是这个)。
- **风险与回滚:** 风险低,只改一个函数的 esbuild 入参,模块导出与行为不变;回滚即 revert 本 PR。
- **先看:** `packages/cli/src/utils/build-runtime.ts`(`buildRuntimeBundle`
里 `esbuild.build` 的 `stdin` 与 `absWorkingDir`
两处);`packages/cli/test/build-runtime-reproducible.test.ts`。
- **两分钟验证:** `pnpm --filter @objectstack/cli exec vitest run --project
unit test/build-runtime-reproducible.test.ts`,预期 3 个用例全绿;在 main 上同一命令红 2
个(文件名不同、sourcemap 带临时目录与进程号)。
- **需要你做的:** 无,席位落地。
## What changes
`buildRuntimeBundle` (`packages/cli/src/utils/build-runtime.ts`) used to
write its generated esbuild entry beside the config as
`.objectstack-runtime.entry.PID.TIMESTAMP.mjs`. esbuild prints every
input's path into the output, once as a path comment above that input's
code and once in the inline sourcemap's `sources`, and the module's file
name is `sha256` over that output. So `process.pid` and `Date.now()`
reached the shipped bytes and the name, and no two builds could share a
name.
Now:
- The entry is handed to esbuild as `stdin` (`contents` = the same
generated source, `resolveDir` = the config's directory, `sourcefile` =
the fixed `.objectstack-runtime.entry.mjs`). Nothing is written next to
the config, so concurrent builds share no file, and the `unlinkSync`
cleanup paths are gone.
- `absWorkingDir` is the config's directory. The audit the order asked
for (any other per-run value reaching the bundle) found one more of the
same mechanism: every path esbuild prints was relative to the process
cwd, so the same app built from another directory (`os build
path/to/objectstack.config.ts`) or another checkout location shipped
different bytes and a different name. Paths are now relative to the
config, so they read `objectstack.config.ts`, `slug.ts`,
`.objectstack-runtime.entry.mjs`.
Nothing else per-run reaches the module. Read on a real bundle: the
banner is a constant; `meta.builtAt` is emitted as the source text `new
Date().toISOString()`, evaluated when the module is imported, not a
build-time value; `LOCATORS` and `refCount` derive from the config;
`sourcesContent` carries file contents only; packages are external, so
no `node_modules` path is printed.
The suggested route's example (a fixed name inside a per-build temp
directory) was not taken: a `mkdtemp` directory name is itself random
and esbuild would print it into the same comment and sourcemap, so the
bytes would still differ. `stdin` gives a stable name with no directory
at all.
## Why
Two builds of an unchanged app must give a byte-identical bundle, the
same file name and the same `runtimeModule` ref in
`dist/objectstack.json` (triage ruling on #26478). Otherwise any cache
or CDN keyed on the name is busted every build and every deploy diff
shows the artifact as changed.
## Tests
New `packages/cli/test/build-runtime-reproducible.test.ts`, `unit` tier:
it calls `buildRuntimeBundle` in-process (no CLI spawn) on a fixture
whose hook calls a helper imported from another module, so the handler
is bundled.
1. Two builds into one `dist/` give the same `outputFileName`, the same
`hash`, byte-identical files, and only one file in `dist/`.
2. The inline sourcemap's `sources` are exactly the three
config-relative names, and the module text does not contain the
fixture's `mkdtemp` directory name.
3. Nothing is left beside the config, and the module imported back
serves the hook and runs it (`' Hello World '` becomes `'hello world'`),
which shows the `stdin` entry still resolves the config and its
transitive import.
Red first on the base (`c7d6ca8e`, test committed alone as `c1c07c89`):
cases 1 and 2 fail, case 3 passes.
<details><summary>Red on the base, green on the fix, and the
ablation</summary>
Base, `pnpm --filter @objectstack/cli exec vitest run --project unit
--maxWorkers=2 test/build-runtime-reproducible.test.ts` under the verify
lock:
```text
× two builds of an unchanged app give byte-identical modules under one name
AssertionError: expected 'objectstack-runtime.0d8a62ba362af40a.…' to be 'objectstack-runtime.0509e52d02fdce77.…'
× the module names its inputs relative to the config, never a per-run or machine-local path
+ "../../../../../tmp/os-build-runtime-reproducible-fADw6M/app/.objectstack-runtime.entry.1505.1793084124288.mjs",
+ "../../../../../tmp/os-build-runtime-reproducible-fADw6M/app/objectstack.config.ts",
+ "../../../../../tmp/os-build-runtime-reproducible-fADw6M/app/slug.ts",
Tests 2 failed | 1 passed (3)
os-verify-lock: VERDICT command-exit 1
```
Fix (`37a263c1`): `Tests 3 passed (3)`, `VERDICT command-exit 0`.
Ablation of the `absWorkingDir` line through
`scripts/ablation-replace.mjs` (anchor 1 → 0, blob `36fb893d` →
`d6c7a478`): case 2 red with the cwd-relative `sources` above (now with
the fixed entry name), cases 1 and 3 green; restored to blob `36fb893d`
= HEAD, `git diff HEAD` empty. The test imports `../src/`, so no `dist/`
rebuild sits between the edit and the run.
Outside the test, the same app built from two different cwds through
`tsx` produced `objectstack-runtime.29956dd5d3b128f6.mjs` both times.
</details>
## Acceptance notes
- The test is in-process, so it does not spawn `os build` at all;
`compile.ts` hands `outputFileName` to `runtimeModule` unchanged
(`finalBundle.runtimeModule = ./FILE`), so the artifact ref follows the
name.
- Open PRs on this file: a search of open PRs for `build-runtime`
returned 13 hits, none of which is a CLI build change.
## Verification
All local readings at head `c914e2e6`.
- `pnpm --filter @objectstack/cli exec vitest run --project unit
--maxWorkers=2` (whole `unit` tier): 336 files passed, 2 failed only
because `packages/cli` itself was not yet built (`packages/cli is not
built (./dist/index.js is absent)`). After `pnpm --filter
@objectstack/cli build`, those two (`published-subpath-console.pin`,
`published-subpath-hook-body.pin`) pass: 29/29.
- `pnpm --filter @objectstack/cli typecheck` (`tsc --noEmit && pnpm
check:test-typecheck`): exit 0.
- `eslint --no-inline-config` on the two touched source files: exit 0.
- The `integration` tier is declared to CI: the diff touches no spawn
entry and no integration-tier file.
- Gates: `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` derived 61 families; 60 ran green, and
`--ran` reconciles `61 derived, 60 run, 1 NOT-MEASURED, 0 UNRUN`.
<details><summary>Gate readings</summary>
- `pnpm check:dual-build-cjs-loads`: NOT MEASURED, exit 3 `PREREQUISITE
NOT MET` (it reads every package's `dist/`, and only the CLI's closure
was built). CI's Lint and Repo Gates runs it.
- Four gates first exited 3 because they ran before the CLI build or on
a shallow clone, then went green on a re-run after `pnpm --filter
@objectstack/cli build` and `git fetch --unshallow`:
`check-plugin-teardown-shape.mjs --self-test`, `check:i18n`,
`check:i18n-coverage`, `check:i18n-walk-parity`.
- Every other derived family: exit 0.
</details>
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(No `packages/spec` change.)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes (eslint on the touched
files).
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset
(`@objectstack/cli` patch).
- [x] 6. `packages/spec` touched: n/a.
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01FEkQ5ozQ2e4NBxHuYv9Wph
---
_Generated by [Claude
Code](https://claude.ai/code/session_01FEkQ5ozQ2e4NBxHuYv9Wph)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 2f21b48 commit ab3c776
3 files changed
Lines changed: 159 additions & 26 deletions
File tree
- .changeset
- packages/cli
- src/utils
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | 201 | | |
207 | 202 | | |
208 | 203 | | |
209 | 204 | | |
210 | 205 | | |
211 | | - | |
212 | 206 | | |
213 | 207 | | |
214 | 208 | | |
215 | 209 | | |
216 | 210 | | |
217 | 211 | | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
238 | 240 | | |
239 | 241 | | |
240 | 242 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
0 commit comments