Skip to content

Commit b3fc81d

Browse files
authored
Generate Zod schemas from codersdk via guts
Hand-maintained Zod schemas drifted from the Go SDK and caused CODAGT-507. This replaces them with codegen. scripts/typegen/ parses codersdk with guts, walks the intermediate AST, and emits Zod v4 schemas to src/codersdk.gen.ts. A daily CI cron checks for drift against coder/coder tip. ChatMessagePart is mapped to z.unknown() because guts flattens the discriminated union into a single object. CreateChatMessageResponse is hand-written since the action only reads the queued field.
1 parent 3bb46f0 commit b3fc81d

15 files changed

Lines changed: 1715 additions & 272 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist/index.js linguist-generated -diff -whitespace
2+
src/codersdk.gen.ts linguist-generated
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Typegen Drift Check
2+
3+
on:
4+
schedule:
5+
# Run daily at 06:00 UTC.
6+
- cron: "0 6 * * *"
7+
workflow_dispatch:
8+
9+
jobs:
10+
typegen-drift:
11+
name: Check for codersdk schema drift
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 10
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v6
18+
19+
- name: Setup Go
20+
uses: actions/setup-go@v5
21+
with:
22+
go-version-file: scripts/typegen/go.mod
23+
24+
- name: Setup Bun
25+
uses: oven-sh/setup-bun@v2
26+
with:
27+
bun-version: 1.3.14
28+
29+
- name: Install dependencies
30+
run: bun install
31+
32+
- name: Update codersdk to latest
33+
working-directory: scripts/typegen
34+
run: go get github.com/coder/coder/v2@main && go mod tidy
35+
36+
- name: Regenerate schemas
37+
run: make gen
38+
39+
- name: Check for drift
40+
run: |
41+
if ! git diff --quiet -- src/codersdk.gen.ts; then
42+
echo "::error::codersdk schema drift detected. The generated schemas are out of date with the latest coder/coder."
43+
echo ""
44+
echo "Diff:"
45+
git diff -- src/codersdk.gen.ts
46+
echo ""
47+
echo "To fix: run 'cd scripts/typegen && go get github.com/coder/coder/v2@main && go mod tidy && cd ../.. && make gen' and commit the result."
48+
exit 1
49+
fi
50+
echo "No drift detected."

Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
.PHONY: fmt fmt-check lint test clean deps all
1+
.PHONY: fmt fmt-check lint test clean deps all gen
22

3-
TS_FILES := $(shell find src -name "*.ts" -type f ! -name "*.test.ts")
3+
TS_FILES := $(shell find src -name "*.ts" -type f ! -name "*.test.ts" ! -name "*.gen.ts")
44

5-
all: build fmt lint test
5+
all: gen build fmt lint test
6+
7+
gen: src/codersdk.gen.ts
8+
9+
src/codersdk.gen.ts: scripts/typegen/main.go scripts/typegen/zod.go scripts/typegen/go.mod
10+
@cd scripts/typegen && go run . > ../../src/codersdk.gen.ts.tmp
11+
@mv src/codersdk.gen.ts.tmp src/codersdk.gen.ts
12+
@bun run format -- src/codersdk.gen.ts || true
613

714
fmt:
815
bun run format
@@ -14,7 +21,7 @@ lint:
1421
bun run lint
1522
bun run typecheck
1623

17-
dist/index.js: $(TS_FILES) package.json bun.lock node_modules
24+
dist/index.js: $(TS_FILES) src/codersdk.gen.ts package.json bun.lock node_modules
1825
bun run build
1926

2027
build: dist/index.js

0 commit comments

Comments
 (0)