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

Fix moq for go 1.13 and avoid creating empty directory #105

Merged
merged 2 commits into from
Nov 3, 2019

Conversation

pdrum
Copy link
Contributor

@pdrum pdrum commented Oct 3, 2019

This fixes #103 and also fixes #102

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found some fixes!

P.S. share your ideas, feedbacks or issues with us at https://github.com/fixmie/feedback (this message will be removed after the beta stage).

pkg/moq/moq.go Outdated Show resolved Hide resolved
@pdrum
Copy link
Contributor Author

pdrum commented Oct 3, 2019

packages.Load in golang.org/x/tools/go/packages fails in go 1.9.X and 1.10.X with an error like:

unsupported version of go: exit status 2: flag provided but not defined: -compiled

therefore I removed them from travis CI. Similar discussions can be found here and here.

I added go 1.13 to travis too.

@pdrum
Copy link
Contributor Author

pdrum commented Oct 3, 2019

By the way the reason for failure with go 1.13 was very similar to the reason empty directories were created. pkgPath was inferred incorrectly. I made a repo illustrating the issue with go 1.13.

For the scenario given in the repo, second call to pkgInfoFromPath in New function of moq.go failed with the message:

Couldn't load mock package: go [list -e -json -compiled -test=false -export=false -deps=false -find=true --]: exit status 1: build .: cannot find module for path .

The reason why this happened is that in second call to pkgInfoFromPath, src had a value of ../foo/mocks, the folder was just created (the bug regarding creation of empty directory) and therefore empty. this call caused the error.

My solution is to

  • Never create the empty directory.
  • If the directory in which the command is executed is a valid go package and its name matches the name given as -pkg, that directory is set as pkgPath
  • Else if the package in which the interface resides is the same as what is given as -pkg, that dir is considered as pkgPath
  • Else if in the package containing the interface, there is a subdirectory with the same name as value of -pkg flag, that is set as the value of pkgPath
  • Otherwise pkgPath is set to zero and packageQualifier never returns ""

I also did some refactoring to make New function simpler to understand

@pdrum
Copy link
Contributor Author

pdrum commented Oct 7, 2019

@matryer @breml Will you consider taking a look at this please?

@thoeni
Copy link

thoeni commented Oct 29, 2019

LGTM 👍 - it'd be great to get this merged to unleash the full power of Go 1.13

@matryer matryer merged commit 5e2b3bd into matryer:master Nov 3, 2019
@sudo-suhas
Copy link
Collaborator

Please note that it might be necessary to delete empty folders inside pkg/moq/testpackages/example for those who have the repo cloned prior to this merge.

@iamfortune
Copy link

So how do i fix this error cos i still get it on my VS code

@sudo-suhas
Copy link
Collaborator

Try updating the moq binary which is installed on your system.

$ go get -u github.com/matryer/moq

You might need to run this outside of a Go module project or add the GO11MODULE=off flag while running the command.

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

Successfully merging this pull request may close these issues.

Failing to generate mocks on 1.13 Empty directory created by mock
5 participants