Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating mocks (reflect mode) fails if interface contains methods with generic types #175

Closed
yz89122 opened this issue Apr 23, 2024 · 3 comments · Fixed by #207
Closed
Labels
bug Something isn't working

Comments

@yz89122
Copy link

yz89122 commented Apr 23, 2024

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

Same issue from github.com/golang/mock golang/mock#677.

Interface:

package mypkg

type S struct{}

type GS[T any] struct {
	T T
}

type I interface {
	M() GS[*S]
}

Mock package:

package mypkgmock

//go:generate go run go.uber.org/mock/mockgen -destination=mock.go -package=$GOPACKAGE github.com/yz89122/gomock-test/mypkg I

Command:

go generate ./...
2024/04/24 06:33:24 Failed to format generated source code: mock.go:42:39: missing ',' in type argument list (and 1 more errors)
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/yz89122/gomock-test/mypkg (interfaces: I)
//
// Generated by this command:
//
//      mockgen -destination=mock.go -package=mypkgmock github.com/yz89122/gomock-test/mypkg I
//

// Package mypkgmock is a generated GoMock package.
package mypkgmock

import (
        mypkg "github.com/yz89122/gomock-test/mypkg"
        gomock "go.uber.org/mock/gomock"
        reflect "reflect"
)

// MockI is a mock of I interface.
type MockI struct {
        ctrl     *gomock.Controller
        recorder *MockIMockRecorder
}

// MockIMockRecorder is the mock recorder for MockI.
type MockIMockRecorder struct {
        mock *MockI
}

// NewMockI creates a new mock instance.
func NewMockI(ctrl *gomock.Controller) *MockI {
        mock := &MockI{ctrl: ctrl}
        mock.recorder = &MockIMockRecorder{mock}
        return mock
}

// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockI) EXPECT() *MockIMockRecorder {
        return m.recorder
}

// M mocks base method.
func (m *MockI) M() mypkg.GS[*github.com/yz89122/gomock-test/mypkg.S] {
        m.ctrl.T.Helper()
        ret := m.ctrl.Call(m, "M")
        ret0, _ := ret[0].(mypkg.GS[*github.com/yz89122/gomock-test/mypkg.S])
        return ret0
}

// M indicates an expected call of M.
func (mr *MockIMockRecorder) M() *gomock.Call {
        mr.mock.ctrl.T.Helper()
        return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "M", reflect.TypeOf((*MockI)(nil).M))
}
exit status 1

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

No error.

To Reproduce Steps to reproduce the behavior

  1. Run go generate ./... for above code.

Additional Information

  • gomock mode (reflect or source): reflect
  • gomock version or git ref: v0.4.0
  • golang version: 1.22.2

Triage Notes for the Maintainers

@JacobOaks JacobOaks added the bug Something isn't working label May 7, 2024
@thegodenage
Copy link

Okay, I find it annoying and it blocks me.
Can I take care of this one? 😄

@tchung1118
Copy link
Contributor

@cebilon123 You can submit a PR if you have a fix. We welcome any contribution.

@arxeiss
Copy link

arxeiss commented Aug 6, 2024

Any progress? If I understand correctly, there is some issue with reflect package itself.
But there should be new API in Go 1.22 based on this comment #197 (comment)

Can that help fix this issue too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
5 participants