feat(mockgen): add project generate mode#306
Open
YuheiTakagawa wants to merge 5 commits into
Open
Conversation
Author
|
Please check this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds a project-level generate mode to
mockgen:The new mode discovers existing
//go:generatedirectives that invokemockgenacross the requested package patterns and runs the matchingmockgeninvocations in-process. This keeps the existinggo:generatedirectives as the source of truth while avoiding the cost of spawning onemockgenprocess per directive.The implementation currently supports:
mockgen ...directivesgo run ... mockgen ...directives, including commongo runflags before the mockgen package//go:generate -command ...aliases that resolve to mockgengeneratebuild-tag files viago list -tags=generateGOPACKAGEexpansion forpackage foo_testIt also shares package name resolution across all discovered targets and uses
go/formatplus deterministic import grouping to avoid per-targetgoimportsenvironment setup while preserving existing generated output.Motivation
Large repositories can have hundreds or thousands of
//go:generate mockgen ...directives. Running them through:spawns one
mockgenprocess per directive. Each process repeats package/import resolution and relatedgocommand work.This mode provides a faster path for repositories that already maintain
mockgendirectives in source files, without introducing a separate config file or requiring users to manually maintain lists of source files.Performance data
On a large repository with 1133 existing source-mode
mockgendirectives, I measured the following:Existing workflow
mockgenprocesses: 1133gocommands from a previous instrumented run:go listgo envNew workflow
mockgenprocesses: 1gocommands:go listgo envThis is approximately:
Compatibility
I also verified the existing
go generate --run=mockgen ./...workflow on the same large repository after these changes:Implementation notes
-generateis a new top-level mode and is mutually exclusive with the existing single-target modes.go list -e -tags=generate -json=Dir,Name,GoFiles,TestGoFiles,XTestGoFiles.go generatevariables used by this mode:GOFILE,GOLINE,GOPACKAGE, andDOLLAR. Other variables are resolved from the process environment.mockgendirectives are converted into the same option paths used by the existing CLI.go listcalls and avoid command-line length issues.Test plan
go test ./mockgen ./mockgen/modelgo test ./...Additional manual validation:
mockgen -generate ./...on a large repository with 1133 existingmockgendirectives.gocommand count was reduced to 55 total / 55go list/ 0go env.