Skip to content

mockgen: format generated code without invoking go - #310

Open
podtserkovskiy wants to merge 1 commit into
uber-go:mainfrom
podtserkovskiy:mockgen-format-without-go
Open

mockgen: format generated code without invoking go#310
podtserkovskiy wants to merge 1 commit into
uber-go:mainfrom
podtserkovskiy:mockgen-format-without-go

Conversation

@podtserkovskiy

Copy link
Copy Markdown
Contributor

mockgen formats its output with goimports (x/tools, imports.Process). With the default options that pass also inserts and deletes imports, and since x/tools v0.27.0 it shells out to go unconditionally — even when there is nothing to fix. So mockgen needs the go toolchain in $PATH purely to format its output, which breaks hermetic builds (Buck, Bazel):

Failed to format generated source code: exec: "go": executable file not found in $PATH

mockgen already computes a complete, self-consistent import set (it even excludes the self/output package), so goimports has nothing to add or remove. Passing FormatOnly: true keeps the gofmt formatting and stdlib/third-party import grouping while skipping the go invocation.

Verification

  • go generate ./... produces byte-for-byte identical output across the whole mock corpus (the insert/delete pass was a no-op).
  • go build ./... and go test ./... pass.
  • Running archive mode with go removed from $PATH now generates and formats a mock successfully (previously failed with the error above).

Bisected the regression to x/tools v0.27.0 (v0.26.0 formats hermetically; v0.27.0+ requires go).

mockgen formats its output with goimports, whose default import insert/delete
pass shells out to `go` (unconditionally since x/tools v0.27.0). That means the
`go` toolchain must be in $PATH just to format, breaking hermetic builds
(Buck, Bazel):

    Failed to format generated source code: exec: "go": executable file not found in $PATH

mockgen already emits a complete import set, so set FormatOnly to skip that pass.
Output is byte-for-byte unchanged.
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.

1 participant