Skip to content

mockgen with archive mode generates invalid mocks when interface contains arguments with type parameters #292

@hartblanc

Description

@hartblanc

Actual behavior

  1. GIVEN an archive for a go package which contains an interface with an argument which contains a type parameter.
  2. WHEN running mockgen in archive mode to generate the mocks for the archive
  3. THEN the generated file is an invalid go file which does not compile due to type parameters being missing.

e.g.

...
func (m *MockIFace) Method(param example.Param) {
    m.ctrl.T.Helper()
    m.ctrl.Call(m, "Method", param)
}
...

Expected behavior

The file is generated with the type parameter initialised

...
func (m *MockIFace) Method(param example.Param[int]) {
    m.ctrl.T.Helper()
    m.ctrl.Call(m, "Method", param)
}
...

To Reproduce Steps to reproduce the behavior

  1. Create a new example module
$ mkdir example
$ cd example
$ go mod init example.com/m
  1. Create new go package within the module which contains an interface with a method with a type parameter
$ echo "package example\n\ntype Param[T any] struct {\n       Field T\n}\n\ntype IFace interface {\n  Method(param Param[int])\n}\n" > example.go
  1. build the archive for the example module
$ go build -buildmode=archive -o example.a
  1. Generate the mocks for the archive
$ mkdir mocks
$ mockgen -archive example.a example IFace > mocks/mocks.go
  1. Attempt to build the mocks
$ go get go.uber.org/mock/gomock
$ go build ./mocks
# example.com/m/mocks
mocks/mocks.go:44:34: cannot use generic type example.Param[T any] without instantiation

Additional Information

  • gomock mode (reflect or source): archive
  • gomock version or git ref: v0.6.0
  • golang version: go version go1.25.3 linux/amd64

Triage Notes for the Maintainers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions