refactor: faster go codegen - #780
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: ef09c17 The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Pull request overview
This PR speeds up Go code generation in the monorepo by (1) consolidating many per-interface mockgen directives into a single package-level invocation for internal/{service,domain,crypto}, and (2) running go generate across directive-holding directories concurrently (while keeping api generation serialized first to avoid source-read/write races). It also updates the server Moon tasks and repository guidance to use the new generator.
Changes:
- Add
scripts/go-generate.mjsto discover//go:generatedirectives and run package generation in parallel with anapi-first phase boundary. - Consolidate mock generation in
internal/{service,domain,crypto}into singlegenerate.godirectives, collapsing multiple generated mock files into one per package. - Update Moon tasks +
AGENTS.mdguidance, and add an empty changeset describing the non-shipping performance improvement.
Reviewed changes
Copilot reviewed 36 out of 37 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| scripts/go-generate.mjs | New Node script to discover //go:generate directives and run generation concurrently with an api-first phase. |
| scripts/check-go-generate.mjs | Switches “check generate drift” to call the new generator runner and handle errors without stack noise. |
| internal/service/token.go | Removes per-interface mockgen directive (now consolidated). |
| internal/service/statement.go | Removes large per-file mockgen directive (now consolidated). |
| internal/service/session.go | Removes per-interface mockgen directive (now consolidated). |
| internal/service/mocks/token.mock.go | Deletes standalone generated mock file (consolidated into service.mock.go). |
| internal/service/mocks/session.mock.go | Deletes standalone generated mock file (consolidated into service.mock.go). |
| internal/service/mocks/service.mock.go | Regenerated: now contains combined mocks for service/database/auth/session/token/key-related interfaces. |
| internal/service/mocks/keys.mock.go | Deletes standalone generated mock file (consolidated into service.mock.go). |
| internal/service/mocks/database.mock.go | Deletes standalone generated mock file (consolidated into service.mock.go). |
| internal/service/mocks/auth_attempt.mock.go | Deletes standalone generated mock file (consolidated into service.mock.go). |
| internal/service/keys.go | Removes per-interface mockgen directive (now consolidated). |
| internal/service/generate.go | New package-level mockgen directive for the entire internal/service package. |
| internal/service/database.go | Removes per-interface mockgen directive (now consolidated). |
| internal/service/auth_attempt.go | Removes per-interface mockgen directive for secondary ports (now consolidated). |
| internal/domain/mock/json_schema.mock.go | Deletes standalone generated mock file (consolidated into domain.mock.go). |
| internal/domain/mock/flow_passkey_registration.mock.go | Deletes standalone generated mock file (consolidated into domain.mock.go). |
| internal/domain/mock/flow_on_success.mock.go | Deletes standalone generated mock file (consolidated into domain.mock.go). |
| internal/domain/mock/flow_field_resolver.schema.mock.go | Deletes standalone generated mock file (consolidated into domain.mock.go). |
| internal/domain/mock/flow_field_resolver.mock.go | Deletes standalone generated mock file (consolidated into domain.mock.go). |
| internal/domain/mock/flow_auth_attempt.mock.go | Deletes standalone generated mock file (consolidated into domain.mock.go). |
| internal/domain/mock/domain.mock.go | New combined generated mocks for multiple domain interfaces. |
| internal/domain/json_schema.go | Removes per-interface mockgen directive (now consolidated). |
| internal/domain/generate.go | New package-level mockgen directive for the entire internal/domain package (keeps enumer directives near types). |
| internal/domain/flow_passkey_registration.go | Removes per-interface mockgen directive (now consolidated). |
| internal/domain/flow_on_success.go | Removes per-interface mockgen directive (now consolidated). |
| internal/domain/flow_field_resolver.go | Removes per-interface mockgen directive (now consolidated). |
| internal/domain/flow_field_resolver_schema.go | Removes per-interface mockgen directive (now consolidated). |
| internal/domain/flow_auth_attempt.go | Removes per-interface mockgen directive (now consolidated). |
| internal/crypto/mock/hash.mock.go | Deletes standalone generated mock file (consolidated into crypto.mock.go). |
| internal/crypto/mock/crypto.mock.go | Regenerated: now also includes hash-related mocks in addition to encryption-related mocks. |
| internal/crypto/hash.go | Removes per-interface mockgen directive (now consolidated). |
| internal/crypto/generate.go | New package-level mockgen directive for the entire internal/crypto package. |
| internal/crypto/encryption.go | Removes per-interface mockgen directive (now consolidated). |
| apps/server/moon.yml | Updates server:generate to run the new Node generator script; extends inputs accordingly. |
| AGENTS.md | Updates contributor guidance to prefer moon run server:generate (with note that bare go generate ./... remains valid but slower). |
| .changeset/parallel-go-codegen.md | Adds an empty changeset documenting the performance-focused refactor. |
Files not reviewed (1)
- internal/domain/mock/domain.mock.go: Generated file
Summary
I noticed that the
go generate ./...command was getting slow on my computer so I started researching what was up:Impact
go generate ./...moon run server:generate/server:check-generateWhere the time went
internal/domaininternal/serviceinternal/cryptoapiThe two numbers differ because mockgen's cost is per invocation, not per interface:
it type-loads the package before mocking anything, so a directive covering one
interface costs the same ~800ms as one covering twenty.
internal/servicehad sixsuch directives and paid the load six times.
After the change the run is no longer a sum — it's a critical path of
api (469ms) + internal/domain (2134ms), with every other package finishingunderneath it.
Validation
go generate ./...Release notes / changeset
Notes