Repository navigation
fix(apidoc): deterministic enum order for fork-extended enums - #414
Conversation
swag collects constants per file in Go map iteration order, so the enum arrays for codersdk.Experiment, codersdk.ChatContextResourceKind and codersdk.ChatWatchEventKind came out in a different order on every make gen run, changing coderd/apidoc/swagger.json, coderd/apidoc/docs.go and the generated markdown. The swaginit wrapper now canonicalizes the generated spec after generation: upstream (non fork_) constants keep the order upstream produces, fork constants are appended at the back in source scan-rank order. swagger.json, docs.go and the markdown are all generated from the canonicalized spec. Closes #410
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2d00194045
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
- Rename canonicalize.go, enumorder.go and enumorder_test.go to fork_enumorder_canonicalize.go, fork_enumorder_scan.go and fork_enumorder_scan_test.go so every new fork file follows the fork_*.go naming rule. - Depend on $(wildcard scripts/apidocgen/swaginit/*.go) instead of the directory so edits to the generator rerun make gen.
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
The testpackage linter only exempts package main test files named *_internal_test.go, so rename both swaginit test files accordingly. Addresses the P1 from the Codex review on 66e2974.
@codex review P1 (white-box test naming): fixed in c16d294, both test files renamed to *_internal_test.go. Tests and golangci-lint re-run clean under the flock. P2 (track swaginit sources instead of the directory): already fixed on this branch, the Makefile depends on $(wildcard scripts/apidocgen/swaginit/*.go) since 8433e27. |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Merged at head Gates on this exact head:
Review notes:
Not exercised: the server at runtime. This is a generation-time change, and enum values are unchanged. Only the order in the docs and the example strings move. |
Closes #410
What and why
make genproduced a differentcoderd/apidoc/swagger.json,coderd/apidoc/docs.goand generated markdown on every run. swag v1.16.6 collects Go constants per file in Go map iteration order (PackagesDefinitions.ParseTypesiteratespkgDefs.files, a map keyed by*ast.File), so any enum whose constants span more than one file came out in a random order. Three fork-extended enums are affected:codersdk.Experiment(deployment.go+fork_experiments.go)codersdk.ChatContextResourceKind(chats.go+fork_agent_definitions.go)codersdk.ChatWatchEventKind(chats.go+fork_chat_watch.go)The swaginit wrapper (our own generator,
scripts/apidocgen/swaginit) now canonicalizes the generated spec right after swag writes it: upstream (nonfork_) constants keep exactly the order swag emitted, fork constants declared infork_*.gofiles are appended at the back in deterministic source scan-rank order. No alphabetical sorting. The three parallel arrays (enum,x-enum-varnames,x-enum-descriptions) are permuted in lockstep. swagger.json is edited in place; docs.go is edited inside its embedded docTemplate JSON by exact offset splicing; the markdown is generated from the canonicalized spec by the existing widdershins pipeline.Scope
scripts/apidocgen/swaginit/fork_enumorder.goandfork_enumorder_scan.go(the wrapper package is ours to edit).scripts/apidocgen/swaginit/main.goaftergen.Build.Makefilechanges one dependency line;scripts/apidocgen/generate.shchanges thego runtarget frommain.goto the package directory (required now that the package has multiple files).coderd/apidoc/swagger.json,coderd/apidoc/docs.go,docs/reference/api/chats.md,docs/reference/api/schemas.md. TheExperimentblock is unchanged versus base because upstream-first plus fork-last is already what base had;ChatContextResourceKindandChatWatchEventKindmove their fork constants to the back, so the docs examplekindvalues follow the new first element (instruction_file,status_change), matching upstream's examples.Determinism evidence
After merging origin/main (fa59c8f), three back-to-back
make genruns underflock ~/.fork-go.lock, sha256 each run, all byte-identical:All 33 generated files hashed identically across runs 1, 2 and 3.
Test that fails without the fix
TestCanonicalizeRestoresRandomizedEnumOrder(fork_enumorder_scan_test.go) mutates the committed swagger.json and docs.go the way swag's random order does, then requirescanonicalizeEnumArraysto restore the committed bytes exactly. WithPlanneutered to return nil, the test fails on both files:With the fix:
go test ./scripts/apidocgen/swaginit/... -count=1passes;golangci-lint run ./scripts/apidocgen/swaginit/...reports 0 issues;go vetclean.check-apidoc.sh
.github/fork/check-apidoc.shjq sort is left in place. It is now redundant for the three fork-extended enums because generation is deterministic, but it also guards forks that compare a committed tree against a fresh run withoutmake genfull context, so it stays as a cheap safety net.Pre-commit hook
This PR follows the pre-commit hook setup from #407. The white-box test files are named *_internal_test.go per the testpackage linter rule. The worktree opt-out (
git config --worktree coder.pre-commit false) was used for the commits on this branch because the fullmake pre-commithook runs outside the flock and includes the known main-branch golangci findings. It applies to this worktree only and was set back after the work. Targeted checks run instead, each underflock ~/.fork-go.lock:go test ./scripts/apidocgen/swaginit/... -count=1: okgolangci-lint run ./scripts/apidocgen/swaginit/...: 0 issuesgofmt -l scripts/apidocgen/swaginit/: cleanmake gendeterminism runs after the origin/main merge: byte-identicalNot exercised
Runtime behavior of the three enums in a running server was not tested; the change is generation-time only and the enum values themselves are untouched, only their order in docs and example strings.