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

counterfeiter does not check vendor directory when generating using import path #75

Closed
nkovacs opened this issue Jun 23, 2017 · 11 comments · Fixed by #136
Closed

counterfeiter does not check vendor directory when generating using import path #75

nkovacs opened this issue Jun 23, 2017 · 11 comments · Fixed by #136

Comments

@nkovacs
Copy link
Contributor

nkovacs commented Jun 23, 2017

When you have a vendored library in your project and you use a fully qualified import path to generate a fake, counterfeiter won't search for it in the vendored directory.

E.g.:

mkdir -p vendor/foo/bar/baz/
echo "package baz\ntype Baz interface {\nBaz() string\n}\n" > vendor/foo/bar/baz/baz.go
counterfeiter foo/bar/baz.Baz

Package 'foo/bar/baz' not found on GOPATH
@tjarratt
Copy link
Collaborator

tjarratt commented Jun 24, 2017

So if I understand the use case correctly here ...

There is a vendored library in your project, and it exports an interface. It's their interface that you want to generate a fake for?

I'm not totally opposed to supporting this behavior, but I am amazed at how the complexity of vendor paths and gopaths makes it hard to tell someone simply how counterfeiter searches for code, when you give it a path name. It's getting very, very, very complicated.

One possible alternative to what you've suggested here would be to have your project define a type alias for the interface you want to mock. That way the interface would be in your project, and easier to find. From my own experience, and the teams within cloud foundry, the preferred Go coding style is to define interfaces closer to where you consume them, in the way I've described here.

@nkovacs
Copy link
Contributor Author

nkovacs commented Jun 24, 2017

Yes.

@tjarratt
Copy link
Collaborator

and just a quick note on the PR for this, #76 - I think this could be useful for other people, but I'm a little concerned about the difficulty of maintaining these changes.

Are you interested in backfilling some unit tests for the changes to the locator package for PRs #74 and #76 ?

@nkovacs
Copy link
Contributor Author

nkovacs commented Jun 26, 2017

You're right, it would become more complex, but on the other hand, having it find the package in $GOPATH/src when you're using vendoring is also confusing, and if you have an older, incompatible version of a library in $GOPATH/src, counterfeiter would create the fake, but it wouldn't build, and it wouldn't be obvious why counterfeiter is generating the wrong fake.

I'll try to write some tests, but I don't know when I'll have time to do that. For now I've forked counterfeiter, because I need these fixes for a project I'm working on.

@sysradium
Copy link

Is there any progress in solving this?

@chendrix
Copy link

chendrix commented Oct 5, 2017

Yeah I'd definitely love this behavior :)

@cholick
Copy link

cholick commented Nov 13, 2017

Just ran into this issue and your suggested type alias workaround isn't a solution. In my case, I'm pulling in two difference dependencies whose interfaces interact.

@akshaymankar
Copy link

Here is an ugly workaround to this problem:

//go:generate counterfeiter -o ./fakes/fake_interface.go ../vendor/github.com/user/library/pkg Interface
//go:generate gsed -i "s|<my-pkg>/vendor/||g" fakes/fake_interface.go

After this running go generate ./... will generate the right fakes.

Note: gsed only works on osx (when gnu-sed is installed), if you want this to work on linux, use sed.

alext added a commit to alext/heating-controller that referenced this issue Mar 31, 2018
This replaces the mock from the gpio package, and removes the last usage
og gomock.

It's been necessary to use a somewhat ugly workaround to generate the
fake from the vendored gpio package due to an outstanding counterfeitter
issue[1]. I've used perl instead of sed for the substitution so that
it's portable across Mac and Linux.

[1] maxbrunsfeld/counterfeiter#75
alext added a commit to alext/heating-controller that referenced this issue Apr 6, 2018
This replaces the mock from the gpio package, and removes the last usage
og gomock.

It's been necessary to use a somewhat ugly workaround to generate the
fake from the vendored gpio package due to an outstanding counterfeitter
issue[1]. I've used perl instead of sed for the substitution so that
it's portable across Mac and Linux.

[1] maxbrunsfeld/counterfeiter#75
alext added a commit to alext/heating-controller that referenced this issue Apr 6, 2018
This replaces the mock from the gpio package, and removes the last usage
og gomock.

It's been necessary to use a somewhat ugly workaround to generate the
fake from the vendored gpio package due to an outstanding counterfeitter
issue[1]. I've used perl instead of sed for the substitution so that
it's portable across Mac and Linux.

[1] maxbrunsfeld/counterfeiter#75
alext added a commit to alext/heating-controller that referenced this issue Apr 8, 2018
This replaces the mock from the gpio package, and removes the last usage
og gomock.

It's been necessary to use a somewhat ugly workaround to generate the
fake from the vendored gpio package due to an outstanding counterfeitter
issue[1]. I've used perl instead of sed for the substitution so that
it's portable across Mac and Linux.

[1] maxbrunsfeld/counterfeiter#75
jchesterpivotal pushed a commit to jchesterpivotal/concourse-build-resource that referenced this issue Aug 19, 2018
- Fixed some bugs
  - Fixed for empty responses
  - Fixed for responses with no new versions
- Some changes to wording
- Add counterfeiter-generated fakes for go-concourse. Due to a known
  bug, I had to edit these manually to use a non-vendor import path
  in the generated code. See: maxbrunsfeld/counterfeiter#75

[#4]

Signed-off-by: Jacques Chester <jchester@pivotal.io>
jchesterpivotal pushed a commit to jchesterpivotal/concourse-build-resource that referenced this issue Aug 19, 2018
As with pkg/check, there are a few changes.

- Fixed trying to read error message from nil errors on 404s
- Some changes to wording
- Added counterfeiter-generated fakes for eventstream. Due to a known
  bug, I had to manually edit the fake file to use a non-vendor import
  path in the generated code. This also arose in bbc3538.
  See: See: maxbrunsfeld/counterfeiter#75

[#4]

Signed-off-by: Jacques Chester <jchester@pivotal.io>
@joefitzgerald
Copy link
Collaborator

I think this will be fixed by #94, please check out that PR.

@nkovacs
Copy link
Contributor Author

nkovacs commented Jul 15, 2019

This does not seem to be working as expected.

Counterfeiter now uses golang.org/x/tools/go/packages, which does not resolve vendored packages correctly for me. I have to specify the full vendored path, e.g. github.com/someuser/someproject/vendor/github.com/someotheruser/somelibrary.SomeType. If I specify github.com/someotheruser/somelibrary.SomeType, it'll fail if it's not in GOPATH, and if it's in GOPATH, I believe it will use that version, which is also incorrect.

@nkovacs
Copy link
Contributor Author

nkovacs commented Jul 15, 2019

This is an issue with go/packages, there's workaround: golang/go#30289

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 a pull request may close this issue.

7 participants