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

source mode error "Loading input failed: loading package failed" #283

Closed
rgarcia opened this issue Apr 3, 2019 · 14 comments
Closed

source mode error "Loading input failed: loading package failed" #283

rgarcia opened this issue Apr 3, 2019 · 14 comments
Assignees

Comments

@rgarcia
Copy link
Contributor

rgarcia commented Apr 3, 2019

The commit to switch to go modules (837231f) leads to mockgen producing a "Loading input failed" error. I've confirmed that it works with the previous commit, bfa1e97. Repro script: https://gist.github.com/3d0e2b1e77a636851c1f51c10a9a15ef

Go version is go version go1.10 linux/amd64.

@rgarcia
Copy link
Contributor Author

rgarcia commented Apr 3, 2019

tested using go version go1.11.1 darwin/amd64 and it also produces the error

@bithavoc
Copy link

bithavoc commented Apr 3, 2019

I'm not able to use this package with Go modules on.

I've tried:

export GOFLAGS=-mod=vendor
export GO111MODULE=on

Any suggestions?

@bithavoc
Copy link

bithavoc commented Apr 3, 2019

Basically what happens is that when mockgen is executed in a project outside GOPATH it generates:

import (
	x "."
	context "context"
	gomock "github.com/golang/mock/gomock"
	reflect "reflect"
)

Then the compiler complains:

 local import "." in non-local package

@bithavoc
Copy link

bithavoc commented Apr 3, 2019

Oops, sorry, I'm new to go modules. Once I executed the following commands outside my go module-enabled project, it picked the updated mockgen binary with the fix:

go install github.com/golang/mock/mockgen

@poy
Copy link
Collaborator

poy commented Apr 5, 2019

@rgarcia Thanks for reporting this. I'll investigate.

@poy poy self-assigned this Apr 5, 2019
@poy poy added the type: bug label Apr 5, 2019
@poy
Copy link
Collaborator

poy commented Apr 5, 2019

This seems to be an artifact of golang.org/x/tools/go/packages having more strict rules about where the .go files are stored.

In the case of your linked script interface.go is not in a proper location (e.g., not in a repo with go.mod or even in a GOPATH). Therefore, packages.Load can't find a package (as its not in one).

I updated the script:

#!/usr/bin/env bash

set -ex

cd /tmp
rm -rf /tmp/mock
mkdir -p /tmp/mock/src/github.com/golang
cd /tmp/mock
export GOPATH=$PWD

pushd src/github.com/golang
    git clone https://github.com/golang/mock
    (cd mock
         git checkout bfa1e97ed7a159b123865559eabdec69b78fe1d5 &&
         go build -o /tmp/mock/mockgen-before ./mockgen)
    rm -rf mock && git clone https://github.com/golang/mock
    (cd mock &&
         git checkout 837231f7bb377b365da147e5ff6c031b12f0dfaa &&
         go build -o /tmp/mock/mockgen-after ./mockgen)
popd

mkdir -p $GOPATH/src/github.com/bar/foo

cat <<EOF >$GOPATH/src/github.com/bar/foo/interface.go
package foo

type Interface interface {
	Bar()
}
EOF

# works
./mockgen-before -package foo -destination interface_mock.go -source $GOPATH/src/github.com/bar/foo/interface.go Interface

# NOW works
if [ "$GO111MODULE" == "on" ]; then
    echo "module github.com/bar/foo" > $GOPATH/src/github.com/bar/foo/go.mod
    pushd $GOPATH/src/github.com/bar/foo/
        /tmp/mock/mockgen-after -package foo -destination interface_mock.go -source interface.go Interface
    popd
else
    ./mockgen-after -package foo -destination interface_mock.go -source $GOPATH/src/github.com/bar/foo/interface.go Interface
fi

@rgarcia
Copy link
Contributor Author

rgarcia commented Apr 5, 2019

@poy thanks for figuring out the cause! I'm fine closing this for now.

Now that I dig into this I think the error message that the script generates is slightly different than the actual error we're seeing at Clever. We build mockgen from a vendor/'d copy pulled down by dep. dep prunes the newly introduced vendor/ folder, and as far as I can tell does not interpret go.mod. This means that building from vendor/github.com/golang/mock/mockgen yields something slightly different than what gets built if you clone the repo. I think this is an issue with dep and how we are building mockgen, not with mockgen itself. Thanks for your help.

@undercover87
Copy link

Hello, I'm getting the same error message. To my understanding, this is a bug and still open .. right?
Were the changes on the OP's script proposed above a sort of workaround?

@balshetzer
Copy link
Collaborator

balshetzer commented Apr 24, 2019 via email

@undercover87
Copy link

Hello,
sorry for not posting details, I was confused on what to actually post.
It turns out it was a configuration issue for me as well, GOPATH (and GOROOT) had not been set properly.

In any case, thanks for the prompt response!

@poy poy closed this as completed Apr 26, 2019
@wanfintara
Copy link

Hi, currently I'm getting this error. After execute go mod vendor then go generate ./...

got this

can't load package: package github.com/golang/mock/mockgen: cannot find package "." in:
        *myrepo*/vendor/github.com/golang/mock/mockgen

but if I delete the vendor folder everything works. Do i need run above script sir ??

@Xanvial
Copy link

Xanvial commented Feb 2, 2021

there's a workaround of adding import to mockgen (or other requirement like model), so it will be added to vendor folder. for example adding this to one of test file

import (
	_ "github.com/golang/mock/mockgen/model"
)

@Higan
Copy link

Higan commented Mar 14, 2021

I just encountered exactly the same error message, and it turned out to be an import cycle issue.

@valters
Copy link

valters commented Apr 25, 2021

Please also see fixes and workarounds in #494

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants