uber-go / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
1.81k stars 103 forks source link

Import shadowing breaks generated code #170

Open blampe opened 3 months ago

blampe commented 3 months ago

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

Triage Notes for the Maintainers

peats-bond commented 3 months ago

same @blampe