uber-go / mock

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

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

Open eyasy1217 opened 10 months ago

eyasy1217 commented 10 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.

rusinikita commented 3 months ago

Isn't it duplicates 138?

eyasy1217 commented 3 months ago

I don't think it's a duplicate because the errors are slightly different. this issue is an error related to qualification. 138 is about an error with import not used.