Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

-self_package does not work when -package is different from the base dir of package path #342

Closed
linzhp opened this issue Nov 5, 2019 · 6 comments

Comments

@linzhp
Copy link
Contributor

linzhp commented Nov 5, 2019

Steps to reproduce:

  1. Clone git@github.com:linzhp/go_examples.git to GOPATH
  2. Run mockgen -package core -self_package github.com/linzhp/go_examples/interfaces github.com/linzhp/go_examples/interfaces Methods

What do I expect:

The generated mock should not import github.com/linzhp/go_examples/interfaces because it is specified in -self_package

What do I see instead:

import (
	gomock "github.com/golang/mock/gomock"
	interfaces "github.com/linzhp/go_examples/interfaces"
	reflect "reflect"
)
@codyoss
Copy link
Member

codyoss commented Nov 20, 2019

Thanks for the report, I will check out your PR soon here!

@codyoss
Copy link
Member

codyoss commented Dec 27, 2019

I don't think there is an issue here, or at least not with the example you have given. In this example your interface returns a struct, Info, from the core package. Because of this, the mock must include interfaces "github.com/linzhp/go_examples/interfaces"

The output that makes this required:

// getInfo mocks base method
func (m *MockMethods) getInfo() interfaces.Info {
	m.ctrl.T.Helper()
	ret := m.ctrl.Call(m, "getInfo")
	ret0, _ := ret[0].(interfaces.Info)
	return ret0
}

Thoughts?

@codyoss codyoss added status: needs more info This issue need more information from the author. and removed status: in progress labels Dec 27, 2019
@linzhp
Copy link
Contributor Author

linzhp commented Jan 1, 2020

Because of this, the mock must include interfaces "github.com/linzhp/go_examples/interfaces"

This is only true when the mock is going to be compiled in a different package. However, the intention here, by specifying -self_package github.com/linzhp/go_examples/interfaces, is to generate the mock and compile it as part of the package github.com/linzhp/go_examples/interfaces. In other words, the mock and Info will be in the same package, so I don't want the import github.com/linzhp/go_examples/interfaces.

Is this a valid use of the -self_package argument?

@codyoss codyoss self-assigned this Jan 3, 2020
@codyoss
Copy link
Member

codyoss commented Jan 3, 2020

Thanks for the clarification. I did not realize the import path did not match package name

For now as a workaround this should work:
mockgen -package core -self_package github.com/linzhp/go_examples/interfaces -source=types.go

Still thinking about the change, just want to make sure if does not break other things.

@codyoss codyoss added status: investigating and removed status: needs more info This issue need more information from the author. labels Jan 3, 2020
@linzhp
Copy link
Contributor Author

linzhp commented Jan 3, 2020

Yes, source mode works as a workaround. However, source mode is slower under bazel_gomock rule, as it requires constructing a GOPATH in the sandbox.

Thanks for looking into this.

@codyoss
Copy link
Member

codyoss commented Jan 14, 2020

Fixed with #343.

@codyoss codyoss closed this as completed Jan 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants