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

Missing import when -package with same package as the destination #505

Closed
keisar opened this issue Dec 21, 2020 · 3 comments · Fixed by #507
Closed

Missing import when -package with same package as the destination #505

keisar opened this issue Dec 21, 2020 · 3 comments · Fixed by #507

Comments

@keisar
Copy link

keisar commented Dec 21, 2020

Actual behavior
Assuming we have the following project structure:

  • /source/source.go -> package name is "pkg1" (import for this package is "source/pkg1")
  • /output/empty.go -> package name is "pkg1" (import for this package is "output/pkg1")
    When running mockgen -package pkg1 -destination ../output/source_mock.go -source=source.go the generated mock is created without imports to "source/pkg1" and the mock has syntax errors.

Expected behavior
The generated mock to include an import to "source/pkg1" and not have syntax errors.

To Reproduce Steps to reproduce the behavior
You can use this test to reproduce the issue

Additional Information
I believe this used to work fine in version 1.3.x, I couldn't pinpoint exactly when this started happening.
I am willing to contribute a fix but will need a little help/guidance as where to start looking, I started looking at how the packages are mapped but couldn't find where this might be happening.

  • gomock mode (reflect or source): source
  • gomock version or git ref: 1.4.x
  • golang version: 1.13 & 1.15

Triage Notes for the Maintainers

@keisar
Copy link
Author

keisar commented Dec 24, 2020

From debugging the test I mentioned above I believe the issue lies here , it seems pkg.PkgPath has the source package path and not the output package path, I'll take a closer look, if anyone can confirm this I will try to submit a fix.

@keisar
Copy link
Author

keisar commented Dec 24, 2020

I believe the issue is a combination of two problems:

  1. Building outputPackagePath (here) doesn't work for go modules as it relies on SrcDir().
  2. here it should use outputPackagePath instead of pkg.PkgPath and if outputPackagePath is empty we should set it to pkg.PkgPath at the start of Generate

From my testing fixing these two issues keeps existing functionality and fixes the issue above, I couldn't find a good way of generating outputPackagePath yet so for my testing I set it manually to the correct path.

If you agree with my findings I can try and come up with a solution for generating outputPackagePath and send a PR.

@codyoss
Copy link
Member

codyoss commented Jan 15, 2021

Thanks for filing the issue. Indeed it does seem like there is an issue here. I would need to look a little deeper but I wonder if you can see if the paths match too. Will take a look more later today and reply back.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants