Skip to content

cannot generate mockfile for generic type #271

Open

Description

@zhangjianxing

Actual behavior A clear and concise description of what the bug is.

# gogen.go
package src

//go:generate mockgen -source=gogen.go -destination=gogen_mock.go -package=src_test

type Base[M any] interface {
	Get() M
}

type MyInterface interface {
	Base[int]
}

type IntInterface = Base[int]

type MyInterface2 interface {
	IntInterface
}

Mockgen is able to generate MyInterface successfully, but not able to generate MyInterface2, and returns following bug

2025/07/28 10:05:40 Loading input failed: gogen.go:18:2: unknown embedded interface interview/src.IntInterface
gogen.go:3: running "mockgen": exit status 1

Expected behavior A clear and concise description of what you expected to
happen.

Mockgen should be able to generate MyInterface2 since the interface should be exactally same as MyInterface

To Reproduce Steps to reproduce the behavior

  1. run go generage ./...

Additional Information

  • gomock mode (reflect or source): source
  • gomock version or git ref: v0.5.2
  • golang version: 1.24.4

Triage Notes for the Maintainers

Activity

  1. JuanBC9 commented on Oct 24, 2025

    @JuanBC9

    Hi, adding (what I think is) a related finding to this issue.

    I've found that when using direct embedding of an instantiated generic (e.g., Base[MyStruct]). Mockgen generates syntactically incorrect code by using the fully qualified package path for the type argument, instead of the local type.

    Example:

    package mypackage
    
    // A generic function type
    type Callback[T any] func(item T)
    
    // A generic base interface
    type Base[T any] interface {
        OnEvent(callback Callback[T])
    }
    
    // A concrete struct
    type MyStruct struct {
    }
    
    // The interface to be mocked
    type MyInterface interface {
        Base[MyStruct]
    }

    mockgen "-destination" "mypackage/my_interface.go" "-package" "mypackage" -- "github.com/org/repo/mypackage" "MyInterface"

    Error: Failed to format generated source code: mypackage/my_interface.go missing ',' in type argument list

    Generated Code:

    // Uses the fully qualified path, which is a syntax error
    func (m *MockMyInterface) OnEvent(arg0 Callback[github.com/org/repo/mypackage.MyStruct]) {
    	m.ctrl.T.Helper()
    	m.ctrl.Call(m, "OnEvent", arg0)
    }

    Expected::

    // Should use the local type name
    func (m *MockMyInterface) OnEvent(arg0 Callback[MyStruct]) {
    	m.ctrl.T.Helper()
    	m.ctrl.Call(m, "OnEvent", arg0)
    }
    • golang version: 1.25.2
    • gomock version: v0.6.0
  2. Bobrovskii commented on Oct 14, 2026

    @Bobrovskii

    I've opened #326 to fix the original source-mode issue. The PR resolves embedded interface aliases and adds regression tests for the parser and generated mocks.
    I also verified the package-mode example from #271 (comment). Mock generation and compilation already succeed for that example on current main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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