Skip to content

fix(main): embed DOCKER_AGENT_VERSION as build-time constant via tsup define#199

Merged
derekmisler merged 1 commit into
docker:mainfrom
docker-agent:fix/docker-agent-version-build-time-constant
May 8, 2026
Merged

fix(main): embed DOCKER_AGENT_VERSION as build-time constant via tsup define#199
derekmisler merged 1 commit into
docker:mainfrom
docker-agent:fix/docker-agent-version-build-time-constant

Conversation

@docker-agent
Copy link
Copy Markdown
Contributor

Problem

In the 3-pass release chain, GITHUB_ACTION_PATH is set to the composite sub-action directory (e.g. review-pr/) rather than the action root. The previous runtime code in src/main/index.ts called resolveActionRoot() which returned GITHUB_ACTION_PATH directly, then tried to read DOCKER_AGENT_VERSION from that path:

ENOENT: no such file or directory, open '.../95867eb/review-pr/DOCKER_AGENT_VERSION'

Fix

DOCKER_AGENT_VERSION is a build-time concern, not a runtime file concern. The file is read once during pnpm build and embedded as a compile-time constant (__DOCKER_AGENT_VERSION__) via tsup's define option — exactly the same mechanism as process.env.NODE_ENV replacement in bundlers.

Changes

tsup.config.ts

  • Import readFileSync and read DOCKER_AGENT_VERSION at build time
  • Add define: { __DOCKER_AGENT_VERSION__: JSON.stringify(version) } so the literal string is inlined into every bundle

vitest.config.ts

  • Mirror the same define entry so tests running against TypeScript source (not the bundle) resolve the constant correctly

src/main/index.ts

  • Remove resolveActionRoot(), the ACTION_PATH constant, and readDockerAgentVersion()
  • Add declare const __DOCKER_AGENT_VERSION__: string (TypeScript ambient declaration for the injected global)
  • Replace cagentVersion = readDockerAgentVersion() with cagentVersion = __DOCKER_AGENT_VERSION__

What stays the same

DOCKER_AGENT_VERSION remains the single source of truth: the file is still updated by the update-docker-agent-version.yml workflow 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 ✅ — version v1.57.0 is embedded as a literal on line 100565 of dist/main.js; DOCKER_AGENT_VERSION does not appear anywhere in the bundle
  • pnpm test ✅ — 306 unit tests pass; integration tests pass including the check that cagent-version output matches the version file
  • pnpm lint ✅ — Biome, tsc --noEmit, and actionlint all clean

… 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 docker-agent requested a review from derekmisler May 8, 2026 15:32
Copy link
Copy Markdown
Contributor Author

@docker-agent docker-agent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

  1. tsup.config.ts and vitest.config.ts — no error handling on readFileSync (low): If DOCKER_AGENT_VERSION is absent (e.g. after git clean -fdx without regenerating), both pnpm build and pnpm test fail 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.

  2. src/main/index.ts — no runtime guard for __DOCKER_AGENT_VERSION__ (low): The declare const tells TypeScript the identifier exists, but if the bundle is ever loaded without tsup's substitution applied (e.g. a direct ts-node invocation), a ReferenceError would be thrown. The vitest.config.ts define mirror closes this gap for the official test runner, making this a non-issue for the supported workflows.

@derekmisler derekmisler enabled auto-merge (squash) May 8, 2026 17:15
@derekmisler derekmisler merged commit 99727b9 into docker:main May 8, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants