Error when mock generating dot import file with different package name
eyasy1217 opened this issue · 2 comments
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
- 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
}
-
Run
go generate ./...
. This produces the filemocks/internal_gen.go
. -
In
mocks/internal_gen.go
, the return typeContext
of theImportDot
method is incorrectly qualified asinternal.Context
, even though such qualification is unnecessary.
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
- gomock mode (reflect or source): source (I haven't tried withe reflect, issue might also happen)
- gomock version or git ref: v0.4.0
- golang version: 1.21.3
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.
Isn't it duplicates 138?
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.