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

reflect mode: Relative import paths #192

Open
abhinav opened this issue Jul 9, 2024 · 0 comments
Open

reflect mode: Relative import paths #192

abhinav opened this issue Jul 9, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@abhinav
Copy link

abhinav commented Jul 9, 2024

Requested feature
The ability to invoke mockgen in reflect mode with relative import paths.
For context, users currently do the following for reflect mode:

mockgen [bunch of flags] github.com/myorg/myproject/foo/bar Iface1,Iface2

With go generate, this might take the form:

// In foo/bar/gen.go

//go:generate mockgen [bunch of flags] github.com/myorg/myproject/foo/bar Iface1,Iface2

The proposal suggests making the following possible:

// In foo/bar/gen.go
//go:generate mockgen [bunch of flags flags] . Iface1,Iface2

// In foo/gen.go
//go:generate mockgen [bunch of flags flags] ./bar Iface1,Iface2

(The first form seems already supported in the source code.)

Why the feature is needed
It's not uncommon to place gen.go files in your project source to generate helper mocks as subpackages or the like.
Not being able to use relative paths here results in extremely long, largely-unreadable go:generate lines.

(Optional) Proposed solution
There's currently logic in place that attempts to determine the import path of the current package if the requested import path is ".". In module mode, it looks for a go.mod file and extracts it from there.
I think this might be possible by making that logic a bit smarter and allowing it to handle other relative paths.

The tool will need to disambiguate between "mockgen foo/bar" meaning "relative directory foo/bar" or "import path foo/bar". This could be with a simple enforcement mechanism: relative directory foo/bar must be prefixed with "./".

@sywhang sywhang added the enhancement New feature or request label Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants