matryer / moq

Interface mocking tool for go generate
http://bit.ly/meetmoq
MIT License
2k stars 127 forks source link

Vendor dir breaks moq (sometimes) #44

Closed warmans closed 6 years ago

warmans commented 6 years ago

This has been driving me crazy for about a day. Essentially it would appear that the presence of a vendor directory causes some kind of a problem related to checking the validity of packages. Here is some code to illustrate.

package foo

import _ "github.com/lib/pq"

type Foo interface {
}

generate the mock moq . Foo

// Code generated by moq; DO NOT EDIT
// github.com/matryer/moq

package foo
<...snip...>

OK all is good. Now run dep ensure and re-run the mock moq . Foo:

foo.go:3:10: could not import github.com/lib/pq (/home/.../moq-test/vendor/github.com/lib/pq/buf.go:7:2: could not import github.com/lib/pq/oid (/home/.../moq-test/vendor/github.com/lib/pq/oid/gen.go:5:1: package main; expected oid))

That's weird. The pq project does indeed have a mix of packages in that directory but moq only cares about this if the package is vendored.

I've tried digging into the code but it's not at all clear what's causing this. Just disabling some vendor related code doesn't seem to help.

The error is returned here:

conf := types.Config{Importer: newImporter(m.src)}
tpkg, err := conf.Check(m.src, m.fset, files, nil)
if err != nil {
    return err
}

Extra information: moq version: latest master go version go1.9.2 os: ubuntu

Extra Mystery: A colleague on osx can still generate mocks on a larger project which fails for me on linux, but CAN re-produce the error with the example above.

SebastiaanKlippert commented 6 years ago

I can confirm I am also getting the same error on oid/gen.go when used in packages which use the pq import as import _ "github.com/lib/pq" .

Removing this import before generating the moq interface works and should not break things because it is a blank import name, but is annoying.

We have pq in our vendor directory as well, I will investigate if I have time. Until then some extra info: go 1.10beta2 os windows 10 latest moq

warmans commented 6 years ago

if anyone is blocked by this you can use my fork from here https://github.com/warmans/moq

I've been using it for a couple of weeks without any further issues.

Only thing is you need to check it out to the same directory as this repo since I didn't change any import paths. i.e. use GOPATH/src/github.com/matryer/moq rather than GOPATH/src/github.com/warmans/moq.

HaraldNordgren commented 6 years ago

Thanks for the fork, I think this might be the solution. Good to hear that you have been using it for weeks without problems!

HaraldNordgren commented 6 years ago

Fixed by https://github.com/matryer/moq/pull/45 I guess.

warmans commented 6 years ago

yep this should be fixed now.