uber-go/mock

Import shadowing breaks generated code

blampe opened this issue · 1 comments

Actual behavior

Generated code doesn't take into account whether a function parameter might shadow an imported package name.

Expected behavior

The generated code should use imported package names or aliases which don't conflict with parameters. Parameter names should ideally remain the same since they are user-facing.

To Reproduce

// apiclient.go
package foo

import "github.com/docker/docker/client"

type APIClient interface {
	client.APIClient
}
go run go.uber.org/mock/mockgen -typed -package foo -source apiclient.go -destination mockapiclient.go --self_package <...>/foo

The generated code produces functions like this (note the container string argument):

// ContainerDiff mocks base method.
func (m *MockAPIClient) ContainerDiff(ctx context.Context, container string) ([]container.FilesystemChange, error) {
    m.ctrl.T.Helper()
    ret := m.ctrl.Call(m, "ContainerDiff", ctx, container)
    ret0, _ := ret[0].([]container.FilesystemChange)         // <----- error
    ret1, _ := ret[1].(error)
    return ret0, ret1
}

[compiler NotAType] [E] container.FilesystemChange is not a type

Additional Information

  • gomock mode (reflect or source): source
  • gomock version or git ref: 0.4.0
  • golang version: go1.22.1 darwin/arm64

Triage Notes for the Maintainers

same @blampe