mock icon indicating copy to clipboard operation
mock copied to clipboard

Unable to generate mock for package using alternate major version and generic type with reflect mode

Open bcho opened this issue 2 years ago • 2 comments

Actual behavior

mockgen generates a uncompilable mock file for interface using alternate major version package and generic types.

Here is a reproduce:

package mockgenrepro

import (
	"context"
	"fmt"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	armcontainerservice "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4" // a package using alternate major version
)

//go:generate mockgen -destination mock_main/foo.go -package mock_main . Interface

type Interface interface {
	BeginAbortLatestOperation(ctx context.Context, resourceGroupName string, resourceName string, agentPoolName string, options *armcontainerservice.AgentPoolsClientBeginAbortLatestOperationOptions) (*runtime.Poller[armcontainerservice.AgentPoolsClientAbortLatestOperationResponse], error)
}

Above program fails the mockgen with error like this (full output):

2023/09/26 16:19:41 Failed to format generated source code: mock_main/foo.go:41:201: missing ',' in type argument list (and 1 more errors)

You can find the full repro here: https://github.com/bahe-msft/mockgen-repro

Expected behavior

Generated mock file should be a valid go source. Expected output: https://github.com/bahe-msft/mockgen-repro/blob/main/mock_main/foo.go

To Reproduce Steps to reproduce the behavior

  1. clone https://github.com/bahe-msft/mockgen-repro
  2. run go generate -v ...

Additional Information

  • gomock mode (reflect or source): reflect
  • gomock version or git ref: v0.3.0
  • golang version: go1.21.0

The same mock can be generated using source mode. But our project is using reflect mode heavily, so we want to fix in reflect mode as well.

Triage Notes for the Maintainers

bcho avatar Sep 26 '23 08:09 bcho

I have a potential fix here: https://github.com/uber-go/mock/pull/98, but needs more time to investigate and understand the root cause.

bcho avatar Sep 26 '23 08:09 bcho

I think it's kind of related to this: https://github.com/golang/go/issues/54393

bcho avatar Sep 26 '23 08:09 bcho

@bcho Your mockgen is not the latest version. Please run go install go.uber.org/mock/mockgen@main before executing go generate.

tra4less avatar Oct 12 '24 03:10 tra4less

@bcho Your mockgen is not the latest version. Please run go install go.uber.org/mock/mockgen@main before executing go generate.

Hi, I think the problem is reflect mode does not support generating generic type. The issue is reporting the reflect mode usage. I understand the latest main branch has deprecated the reflect mode. Your suggestion is for switching to a different mode, which will require some additional update on the command due to legacy usages.

Will close this issue since there is no way to implement this feature under reflect mode.

bcho avatar Oct 12 '24 03:10 bcho