Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
eyasy1217 opened this issue Jan 17, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@eyasy1217
Copy link
Contributor

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
}
  1. Run go generate ./.... This produces the file mocks/internal_gen.go.

  2. 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.

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.

@tchung1118 tchung1118 added the bug Something isn't working label Jan 30, 2024
@rusinikita
Copy link

Isn't it duplicates 138?

@eyasy1217
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants