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

Imports of vendored packages add the vendor prefix to the import path #18

Closed
johanbrandhorst opened this issue Jul 11, 2017 · 38 comments · Fixed by #122
Closed

Imports of vendored packages add the vendor prefix to the import path #18

johanbrandhorst opened this issue Jul 11, 2017 · 38 comments · Fixed by #122

Comments

@johanbrandhorst
Copy link

I recently updated moq (imports, yay!) and tried regenerating one of my files and got the following imports generated:

package mocks

// AUTOGENERATED BY MOQ
// github.com/matryer/moq

import (
	"sync"
	"<myrepo>/vendor/golang.org/x/net/context"
)

This obviously doesn't work.

@johanbrandhorst
Copy link
Author

I'm also seeing a lot of weird errors when trying to use moq with a vendor folder:

file.go:36:37: cannot use bla.Bla (of type github.com/group/bla.Bla) as <myrepo>/vendor/github.com/group/bla.Bla value in argument to something.Something

These files used to work just fine with Moq until recently.

@johanbrandhorst
Copy link
Author

Almost certainly introduced by 1bd8336

@matryer
Copy link
Owner

matryer commented Jul 11, 2017

This one is surprisingly difficult to test

@matryer
Copy link
Owner

matryer commented Jul 11, 2017

@johanbrandhorst Could you quickly grab branch vendoring-18 and see if that user.go file works for you?

@matryer
Copy link
Owner

matryer commented Jul 11, 2017

For some reason, the vendoring stuff just isn't working here.

@matryer
Copy link
Owner

matryer commented Jul 11, 2017

OK it's working (turns out testdata in the path screws things up)... now I have a failing test, which proves this bug.

@matryer matryer mentioned this issue Jul 11, 2017
@matryer
Copy link
Owner

matryer commented Jul 11, 2017

@johanbrandhorst I think I fixed it... can you test it on your code once I merge #19 in?

@matryer
Copy link
Owner

matryer commented Jul 11, 2017

p.s. @johanbrandhorst you should do a fresh install... I've also changed the way it captures the calls etc. and it's a breaking change.

@johanbrandhorst
Copy link
Author

It has not fixed the 2nd problem, I'm still seeing things like

file.go:36:37: cannot use bla.Bla (of type github.com/group/bla.Bla) as <myrepo>/vendor/github.com/group/bla.Bla value in argument to something.Something

preventing me from testing this quickly without having to comment out all instances moq finds offensive.

@johanbrandhorst
Copy link
Author

Does not seem to have fixed the other vendoring error either, sorry.

@johanbrandhorst
Copy link
Author

This is still happening...

@matryer
Copy link
Owner

matryer commented Aug 2, 2017

I wonder if you can submit a PR that proves it with a failing test? I couldn't recreate it very easily. It'll probably be an easy fix once we know the actual problem.

@matryer matryer reopened this Aug 2, 2017
@johanbrandhorst
Copy link
Author

I'll see if I can make a minimal test case, thanks

@johanbrandhorst
Copy link
Author

https://github.com/johanbrandhorst/moq-vendor-bug/tree/master. This is exhibiting the incorrect import bug but (not yet?) the actual runtime error.

@ataliadvanstep
Copy link

ataliadvanstep commented Sep 2, 2017

I have same issue

image

I pulled the code from master but it seems the bug is still there

@jayd3e
Copy link

jayd3e commented Sep 17, 2017

I'm unfortunately seeing this issue as well. I'm getting import errors whenever I try to generate an interface. moq doesn't seem to be able to find my project-specific packages, when I'm using a vendor/ folder. For example let's say I have all of my packages under /go/src/github.com/org/project/{PACKAGE_NAME}, and I also have a vendor subfolder at /go/src/github.com/org/project/vendor, I get an error message like so:

file.go:8:2: could not import github.com/org/project/core (can't find import: github.com/org/project/core)
moq [flags] destination interface [interface2 [interface3 [...]]]
  -out string
    	output file (default stdout)
  -pkg string
    	package name (default will infer)
service/account.go:13: running "moq": exit status 1
make: *** [generate] Error 1

@jayd3e
Copy link

jayd3e commented Sep 19, 2017

If there is a hack or short-term fix for this, it would be much appreciated. I'm not a fan of any of the other mocking libraries besides moq. Is there a commit that we know the vendor/ directory was working for?

@matryer
Copy link
Owner

matryer commented Sep 19, 2017

I wonder if someone could make a PR simulating this as a failing test? I cannot reproduce it. It's probably an easy fix, but not if I can't see the error.

Perhaps @jayd3e?

@jayd3e
Copy link

jayd3e commented Sep 20, 2017

I'll try to create an isolated test to show this behavior. Will get back to you in the next couple days.

@jayd3e
Copy link

jayd3e commented Sep 22, 2017

@matryer I just created a repo around this issue, showing what I'm running into. Hope this helps:

https://github.com/jayd3e/moq-issue

There are notes in the README.

@johanbrandhorst
Copy link
Author

I made a repo which reproduces the error cause in August. I think Mat wants a test case for the tests, not a repo.

@jayd3e
Copy link

jayd3e commented Sep 26, 2017

@johanbrandhorst just to confirm, are you seeing the same issues as I did in that repo? In other words, does my issue appear to be the same one that prompted you creating this thread?

@jayd3e
Copy link

jayd3e commented Sep 26, 2017

I can try and create a test, but it would basically just be the same thing as that repo except programatic.

@johanbrandhorst
Copy link
Author

I've described the two different errors I've seen, the repo I submitted shows the first error (the vendor path pretended to the imported package). I think a programmatic test that reproduces the error is exactly what we want, ideally both of them.

@jayd3e
Copy link

jayd3e commented Oct 4, 2017

I'm not able to write a test for this. It might be an environment issue for me. If you guys are able to use moq for a large project, can you walk me through how you setup your environment successfully to use moq? I really want to use this lib, but I'm not quite sure where to go from here.

@johanbrandhorst
Copy link
Author

I have had a suspicion that it might be an environment issue as well... I don't know if it depends on your $GOPATH or the contents of vendor, but I think it might be where the problem lies.

@jayd3e
Copy link

jayd3e commented Oct 9, 2017

Alright, I'll try to do some more debugging on this tonight using my environment.

@matryer
Copy link
Owner

matryer commented Oct 10, 2017 via email

@jayd3e
Copy link

jayd3e commented Oct 10, 2017

@matryer are you able to use moq on a project with >10 dependencies using master? If so, I would love to hear how you usually setup your environment. In other words, what you set your GOPATH to and where you put your dependencies(vendor or in the standard GOPATH).

@johnchildren
Copy link

Hi, I work with @johanbrandhorst and after changing a few too many import paths I found a way to reproduce this issue. My test is in this commit: johnchildren@aa63789 .

Obviously it's not a great test but it seems to provide a starting point at least.

The problem seems to be with go generate (as moq works perfectly without it) which is I suppose why other mocking tools have the same problem, though it looks like a workaround might be possible based on some of the issues on MockGen (golang/mock#28 golang/mock#99).

@benma
Copy link

benma commented Dec 6, 2017

I just had the same issue. Is there any movement on this? This is practically a deal breaker.

@matryer
Copy link
Owner

matryer commented Dec 6, 2017

I just brought this test into the project and guess what? The test didn't fail.

The package it's importing is vendorerd (see it here).

It produced this:

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

package user

import (
	"github.com/matryer/somerepo"
	"sync"
)

var (
	lockServiceMockDoSomething sync.RWMutex
)

// ServiceMock is a mock implementation of Service.
//
//     func TestSomethingThatUsesService(t *testing.T) {
//
//         // make and configure a mocked Service
//         mockedService := &ServiceMock{
//             DoSomethingFunc: func(in1 somerepo.SomeType) error {
// 	               panic("TODO: mock out the DoSomething method")
//             },
//         }
//
//         // TODO: use mockedService in code that requires Service
//         //       and then make assertions.
//
//     }
type ServiceMock struct {
	// DoSomethingFunc mocks the DoSomething method.
	DoSomethingFunc func(in1 somerepo.SomeType) error

	// calls tracks calls to the methods.
	calls struct {
		// DoSomething holds details about calls to the DoSomething method.
		DoSomething []struct {
			// In1 is the in1 argument value.
			In1 somerepo.SomeType
		}
	}
}

// DoSomething calls DoSomethingFunc.
func (mock *ServiceMock) DoSomething(in1 somerepo.SomeType) error {
	if mock.DoSomethingFunc == nil {
		panic("moq: ServiceMock.DoSomethingFunc is nil but Service.DoSomething was just called")
	}
	callInfo := struct {
		In1 somerepo.SomeType
	}{
		In1: in1,
	}
	lockServiceMockDoSomething.Lock()
	mock.calls.DoSomething = append(mock.calls.DoSomething, callInfo)
	lockServiceMockDoSomething.Unlock()
	return mock.DoSomethingFunc(in1)
}

// DoSomethingCalls gets all the calls that were made to DoSomething.
// Check the length with:
//     len(mockedService.DoSomethingCalls())
func (mock *ServiceMock) DoSomethingCalls() []struct {
	In1 somerepo.SomeType
} {
	var calls []struct {
		In1 somerepo.SomeType
	}
	lockServiceMockDoSomething.RLock()
	calls = mock.calls.DoSomething
	lockServiceMockDoSomething.RUnlock()
	return calls
}

@benma
Copy link

benma commented Dec 6, 2017

Thanks for the quick response.

I double checked, and it seems it works, but not all of the time. After deleting my go/pkg folder, I got this error:

myfile.go:6:2: could not import github.com/btcsuite/btcutil (could not import github.com/btcsuite/btcd/btcec (/home/user/go/src/github.com/my/repo/vendor/github.com/btcsuite/btcd/btcec/genprecomps.go:10:1: package main; expected btcec))

The issue seems to be related to // +build: ignore being ignored. Any ideas?
https://github.com/btcsuite/btcd/blob/2e60448ffcc6bf78332d1fe590260095f554dd78/btcec/genprecomps.go#L8

@johnchildren
Copy link

johnchildren commented Dec 6, 2017

Strange, it's passing for me as well now. I have no idea what has changed, but even backdating my Go to 1.8 and using earlier versions of moq seem to have no effect.

However the good news is that moq seems to be working elsewhere as well while mockgen is still failing.

@coderanger
Copy link

This is back to being broken after #87

$ moq -pkg edit_test ../../../vendor/github.com/aws/aws-sdk-go/service/kms/kmsiface KMSAPI | head 
// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq

package edit_test

import (
        "context"
        "github.com/Ridecell/ridectl/vendor/github.com/aws/aws-sdk-go/service/kms/kmsiface"
        "github.com/aws/aws-sdk-go/aws/request"
        "github.com/aws/aws-sdk-go/service/kms"
        "sync"
)

@matryer
Copy link
Owner

matryer commented Feb 13, 2019 via email

@coderanger
Copy link

@matryer I'm guessing #87 used underlying data that was never fixed, so we should track down what caused this to be fixed some of the time and make it use that data instead? Or maybe it was never fixed and the cause is environmental?

@johanbrandhorst
Copy link
Author

It affected me as well when testing the branch. #87 (comment)

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