matryer / moq

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

Custom importer may be causing errors #34

Closed kwoodhouse93 closed 6 years ago

kwoodhouse93 commented 7 years ago

I see in #18 that @johanbrandhorst mentioned he was getting errors that look like the one below when importing 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

I also encountered this issue the other day, and wanting to find a solution so I could still use moq, I spent a few minutes playing with the code. The result was the following minimal patch, which stopped the errors I was seeing and allowed moq to generate the mocks I was after.

diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go
index f4b43c0..d9a75b2 100644
--- a/pkg/moq/moq.go
+++ b/pkg/moq/moq.go
@@ -6,6 +6,7 @@ import (
        "fmt"
        "go/ast"
        "go/format"
+       "go/importer"
        "go/parser"
        "go/token"
        "go/types"
@@ -79,7 +80,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
                        files[i] = file
                        i++
                }
-               conf := types.Config{Importer: newImporter(m.src)}
+               conf := types.Config{Importer: importer.Default()}
                tpkg, err := conf.Check(m.src, m.fset, files, nil)
                if err != nil {
                        return err

I don't have a very deep understanding of this issue, but it seems the custom importer moq is currently using may be outdated. The importer in the stdlib seems to work without the errors mentioned above.

matryer commented 7 years ago

There was a reason why we had to add a custom importer. Do all the tests pass with that default importer?

HaraldNordgren commented 6 years ago

@kwoodhouse93 @matryer Implementing this fix and then running against one of my own projects indicates that it breaks the importing. Also the unit tests are failing.

However, here is an alternative implementation that helped me: https://github.com/matryer/moq/pull/45

HaraldNordgren commented 6 years ago

Likely fixed by https://github.com/matryer/moq/pull/45

johanbrandhorst commented 6 years ago

Yeah it's fixed my problems :).