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

mocking dependencies located in a vendor directory #4

Closed
rgarcia opened this issue Jul 30, 2015 · 12 comments · Fixed by #28
Closed

mocking dependencies located in a vendor directory #4

rgarcia opened this issue Jul 30, 2015 · 12 comments · Fixed by #28

Comments

@rgarcia
Copy link
Contributor

rgarcia commented Jul 30, 2015

If you vendor a dependency into vendor/, what's the best way to generate mocks for it?

If you use mockgen to generate mocks for that specific version of it, e.g. mockgen a/vendor/somedependency, the generated mock has the wrong import: import somedependency "a/vendor/somedependency".

The workaround I'm currently using is making sure the version in $GOPATH/src/a/vendor/somedependency matches the version in $GOPATH/src/somedependency, and then running mockgen somedependency generates the correct code. This is cumbersome!

@dsymonds
Copy link
Contributor

Interesting. I haven't played with GO15VENDOREXPERIMENT yet, so I don't know what's involved.

@dsymonds
Copy link
Contributor

dsymonds commented Aug 6, 2015

After a quick skim of mockgen (but not actually trying it out in practice), I think it should just work as long as you pass the same import path to mockgen as you write in your app. You should be able to run mockgen somedependency Foo, since mockgen will simply run go build with a generated program using that import path. What fails if you do that?

@dsymonds
Copy link
Contributor

dsymonds commented Aug 6, 2015

Ah, yes, golang/go#12019 is certainly interesting and might be the cause of the breakage.

@rgarcia
Copy link
Contributor Author

rgarcia commented Aug 7, 2015

@dsymonds yeah, in order for it to "just work" you also need to change mockgen to generate the temp program in the current directory. Then the call to go build will correctly import the vendored version. I made this change but then ran into golang/go#12019.

@stevvooe
Copy link

I experienced something similar using Go 1.6 with vendored packages. From the error output, it looks like the vendored packages are being ignoring in the exec phase:

$ mockgen -destination mocked.go github.com/vendored/package SomeInterface
prog.go:13:2: cannot find package "github.com/vendored/package" in any of:
    /usr/local/go/src/github.com/vendored/package (from $GOROOT)
    /home/ubuntu/.go_workspace/src/github.com/vendored/package (from $GOPATH)

Basically, $GOPATH is sanitized and we are trying to build against all vendored packages. github.com/vendored/package is in vendor/ but seems to be ignored by mockgen.

I didn't have time to debug the environment further, but I can provide a reproduction if this isn't enough information to track this down.

If this is a different issue, please let me know and I'll file another.

@rgarcia rgarcia changed the title mocking dependencies vendored with GO15VENDOREXPERIMENT mocking dependencies located in a vendor directory Mar 11, 2016
@rgarcia
Copy link
Contributor Author

rgarcia commented Mar 11, 2016

@stevvooe that is the same issue that I have experienced--gomock not reading dependencies from the vendor/ directory. I've changed the title of this issue to be more accurate.

@rgarcia
Copy link
Contributor Author

rgarcia commented Mar 14, 2016

@stevvooe I've made a PR (#28) that I believe resolves this issue. Feel free to test using that branch.

@stevvooe
Copy link

@rgarcia I just gave the fix a try and it works. Thanks for the PR.

@nizsheanez
Copy link

guys, can i help somehow with this ticket?

@stevvooe
Copy link

@nizsheanez Help get #28 or a similar solution merged.

I am about drop this package as it has been nothing but a headache.

@sputnik13
Copy link

This issue has been open for about a year, if there's a PR and it works, why is it still open? Where are the maintainers?

@stevvooe
Copy link

@sputnik13 I'd recommend steering clear of this package, if you can. I'd suggest people just do something similar to the technique described in #35 (comment).

anfernee added a commit to anfernee/mock that referenced this issue Aug 15, 2017
anfernee added a commit to anfernee/mock that referenced this issue Aug 15, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants