uber-go / mock

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

Error when mock generating dot import file with different package name #141

Open eyasy1217 opened 5 months ago

eyasy1217 commented 5 months ago

Actual behavior If your source file contains dot imports and the output package name is different, mockgen's output will be a file that cannot be compiled.

Expected behavior The output will be a file that can be compiled successfully.

To Reproduce

  1. The source file internal.go, which contains a dot import, is as follows:
    
    package internal

//go:generate go run go.uber.org/mock/mockgen -source=internal.go -destination=mocks/internal_gen.go

import ( . "context" )

type WithDotImports interface { ImportDot() Context }


2. Run `go generate ./...`. This produces the file `mocks/internal_gen.go`.

3. In `mocks/internal_gen.go`, the return type `Context` of the `ImportDot` method is incorrectly qualified as `internal.Context`, even though such qualification is unnecessary.
```go
package mock_internal

import (
    . "context"
    // ...
)

// ...

func (m *MockWithDotImports) ImportDot() internal.Context {
    m.ctrl.T.Helper()
    ret := m.ctrl.Call(m, "ImportDot")
    ret0, _ := ret[0].(internal.Context)
    return ret0
}

Additional Information

Triage Notes for the Maintainers

Dot import are seldom used. This bug may not be worth fixing. So how about adding the following code to after this line.

    if pkg.Name != outputPackageName && len(pkg.DotImports) > 0 {
        log.Fatalf("Different package name specifications including dot imports are not yet supported: %v != %v", pkg.Name, outputPackageName)
    }

Fundamentally, the issue seems to be that parseType method in mockgen/parse.go does not return the correct package name for dot import types.