fix(main): embed DOCKER_AGENT_VERSION as build-time constant via tsup define#199
Conversation
… define Previously, src/main/index.ts read the DOCKER_AGENT_VERSION file at runtime using ACTION_PATH (resolved from GITHUB_ACTION_PATH or by walking up from import.meta.dirname). In the 3-pass release chain, GITHUB_ACTION_PATH is set to the composite sub-action directory (e.g. review-pr/), so the runtime read fails with: ENOENT: no such file or directory, open '.../95867eb/review-pr/DOCKER_AGENT_VERSION' Fix: read DOCKER_AGENT_VERSION once at build time in tsup.config.ts and inject the version string as a global constant (__DOCKER_AGENT_VERSION__) via tsup's `define` option. The bundle now contains a plain string literal; no file lookup occurs at runtime. Changes: - tsup.config.ts: read DOCKER_AGENT_VERSION at build time; add `define` entry that injects __DOCKER_AGENT_VERSION__ as a JSON string literal in every bundle - vitest.config.ts: mirror the same `define` so Vitest (running TypeScript source directly, not the bundle) resolves the constant during test runs - src/main/index.ts: replace resolveActionRoot(), ACTION_PATH, and readDockerAgentVersion() with a `declare const __DOCKER_AGENT_VERSION__` and direct use of the injected constant in the run() function DOCKER_AGENT_VERSION remains the single source of truth: the file is still updated by the auto-update workflow and still consumed by bash steps in the composite sub-actions (action.yml, review-pr/action.yml). Only the JS runtime read is eliminated.
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
The PR correctly solves the ENOENT runtime failure by moving the DOCKER_AGENT_VERSION read from runtime filesystem access to a tsup build-time define constant. The approach is idiomatic (same mechanism as process.env.NODE_ENV in bundlers), the TypeScript ambient declaration is properly scoped, and the vitest define mirror ensures test parity with the bundle. No high or medium severity issues found.
Minor observations (no action required):
-
tsup.config.tsandvitest.config.ts— no error handling onreadFileSync(low): IfDOCKER_AGENT_VERSIONis absent (e.g. aftergit clean -fdxwithout regenerating), bothpnpm buildandpnpm testfail with a raw Node.js stack trace rather than a clear diagnostic message. Given this file is committed to the repo and always present, the risk is negligible in practice. -
src/main/index.ts— no runtime guard for__DOCKER_AGENT_VERSION__(low): Thedeclare consttells TypeScript the identifier exists, but if the bundle is ever loaded without tsup's substitution applied (e.g. a directts-nodeinvocation), aReferenceErrorwould be thrown. Thevitest.config.tsdefinemirror closes this gap for the official test runner, making this a non-issue for the supported workflows.
Problem
In the 3-pass release chain,
GITHUB_ACTION_PATHis set to the composite sub-action directory (e.g.review-pr/) rather than the action root. The previous runtime code insrc/main/index.tscalledresolveActionRoot()which returnedGITHUB_ACTION_PATHdirectly, then tried to readDOCKER_AGENT_VERSIONfrom that path:Fix
DOCKER_AGENT_VERSIONis a build-time concern, not a runtime file concern. The file is read once duringpnpm buildand embedded as a compile-time constant (__DOCKER_AGENT_VERSION__) via tsup'sdefineoption — exactly the same mechanism asprocess.env.NODE_ENVreplacement in bundlers.Changes
tsup.config.tsreadFileSyncand readDOCKER_AGENT_VERSIONat build timedefine: { __DOCKER_AGENT_VERSION__: JSON.stringify(version) }so the literal string is inlined into every bundlevitest.config.tsdefineentry so tests running against TypeScript source (not the bundle) resolve the constant correctlysrc/main/index.tsresolveActionRoot(), theACTION_PATHconstant, andreadDockerAgentVersion()declare const __DOCKER_AGENT_VERSION__: string(TypeScript ambient declaration for the injected global)cagentVersion = readDockerAgentVersion()withcagentVersion = __DOCKER_AGENT_VERSION__What stays the same
DOCKER_AGENT_VERSIONremains the single source of truth: the file is still updated by theupdate-docker-agent-version.ymlworkflow and still consumed by bash steps in the composite sub-actions (action.yml,review-pr/action.yml). Only the JS runtime filesystem read is eliminated.Validation
pnpm build✅ — versionv1.57.0is embedded as a literal on line 100565 ofdist/main.js;DOCKER_AGENT_VERSIONdoes not appear anywhere in the bundlepnpm test✅ — 306 unit tests pass; integration tests pass including the check thatcagent-versionoutput matches the version filepnpm lint✅ — Biome, tsc --noEmit, and actionlint all clean