uber-go / mock

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

Aliased type is replaced with its destination type #186

Open atombender opened 2 weeks ago

atombender commented 2 weeks ago

This appears to only affect reflect mode. Source mode works correctly. It may be that this bug exists because type aliases are not available through reflection. If that is the case, feel free to close.

Actual behavior

Consider these files:

// In foo/foo.go
package foo

import "github.com/atombender/gomock-issue/foo/internal/things"

type Thing = things.Thing
// In foo/internal/things/things.go
package things

type Thing struct{}
// In bar/bar.go
package bar

import "github.com/atombender/gomock-issue/foo"

type Starter interface {
    Start(thing *foo.Thing)
}

The generated mock will look like this:

import (
    reflect "reflect"

    things "github.com/atombender/gomock-issue/foo/internal/things"
    gomock "go.uber.org/mock/gomock"
)
...

func (m *MockStarter) Start(arg0 *things.Thing) {
...

This is a real problem since the things package is internal, e.g. internal/things.go, relative to foo, which is in an adjacent package. This means the compilation fails:

package github.com/atombender/gomock-issue/bar/mocks
    bar/mocks/mocks.go:15:2: use of internal package github.com/atombender/gomock-issue/foo/internal/things not allowed

Expected behavior

The generated mock should actually refer to the original type:

func (m *MockStarter) Start(arg0 *foo.Thing) {

To Reproduce

Repository with full repro. Test with:

$ mockgen -package mocks -destination ./bar/mocks/mocks.go github.com/atombender/gomock-issue/bar Starter
$ go build ./...

Additional Information