Repository navigation
go generate ./... is broken #7976
Description
Activity
- addedtech-debtA chore that addresses technical debtA chore that addresses technical debtcoreThis issue is not accepting PRs from outside contributorsThis issue is not accepting PRs from outside contributors
on Sep 11, 2023 Why is this happening?
Well the problem is to do with dependencies and packages. Let's take a look at the
Configinterface which lives in theconfigpackage:Lines 32 to 50 in 4896546
//go:generate moq -rm -out config_mock.go . Config type Config interface { GetOrDefault(string, string) (string, error) Set(string, string, string) Write() error Migrate(Migration) error CacheDir() string Aliases() *AliasConfig Authentication() *AuthConfig Browser(string) string Editor(string) string GitProtocol(string) string HTTPUnixSocket(string) string Pager(string) string Prompt(string) string Version() string } This
Configinterface is defined in theconfigpackage. Thego generatecomment also says to generate theConfigMockstruct in the same package. Finally, there is a stub config used in tests that also exists in the same package that references the concreteConfigMock.With the
-rmflag provided tomoqviago generate, theconfig_mock.gofile is deleted, then the package is loaded in order to generate the code from the interface. However, becausestub.goreferencesConfigMockand that has been removed, the package is in a broken state.However, that's not all because changing the interface and regenerating exhibits the same problem. For example, changing a return type results in the
ConfigMockand therefore the Stub from no longer satisfying the interface. Thus, loading the package to regenerate the interface also results in a broken state.Typically this isn't a problem because test specific code lives in
_testpackages but for whatever reason the CLI has mostly been built with tests living inside the implementation packages.What do we do about this?
At it's core the issue is that our dependency graph is all messed up. The usual thing to do here is to have the interface in a consumer package, put the mock into its own package
mocksorfoo_testand then to move the stubs as well so that the import graph makes sense. However we can't move the stub easily because it is reaching into the unexportedconfig.cfgstruct.I think the right thing to do is to then to move the interface to a shared location e.g. a
ghpackage, then generate the mock in a nearby package. In this way the stub can stay inconfigand have access to the unexported struct.The other advantage to this is that it now starts to form a what I would call a "domain package", a place where you can look to see the shapes of the puzzle pieces that are required for the
ghapp to work.
Description
When adding
moqgenerate stanzas, I expected to be able to rungo generate ./...to create my new mocks. Unfortunately, this fails (ontrunk):This results in a broken go project.
This isn't explicitly a bug since it doesn't affect the built artifact of the CLI, but it is a really annoying developer experience.