mock icon indicating copy to clipboard operation
mock copied to clipboard

Import shadowing breaks generated code

Open blampe opened this issue 1 year ago • 4 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

blampe avatar Mar 27 '24 17:03 blampe

same @blampe

peats-bond avatar Mar 27 '24 23:03 peats-bond

The issue still exists and it seems like there are no workarounds. Could we please get #195 merged?

gajanan-hegde avatar Jan 17 '25 16:01 gajanan-hegde

The issue still exists and it seems like there are no workarounds. Could we please get #195 merged?

I'm in the same boat. Any updates from the Uber team?

jack-w-shaw avatar Feb 07 '25 13:02 jack-w-shaw