vektra / mockery

A mock code autogenerator for Go
https://vektra.github.io/mockery/
BSD 3-Clause "New" or "Revised" License
6.07k stars 408 forks source link

mockery Error parsing file could not import golang.org/x/sync/semaphore (invalid package name: \"\") #514

Open anthonyGuo opened 1 year ago

anthonyGuo commented 1 year ago

golang version go1.17.6 linux/amd64 mockery dry-run=false version=v2.9.4

go.mod require golang.org/x/sync v0.1.0

go mod download

go.sum 
      golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
      golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
      golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

log.go import ( "golang.org/x/sync/semaphore" )

mockery --all --inpackage --keeptree --output /root/baisheng/

27 Oct 22 07:32 UTC INF Starting mockery dry-run=false version=v2.9.4 27 Oct 22 07:32 UTC INF Walking dry-run=false version=v2.9.4 27 Oct 22 07:33 UTC ERR Error parsing file error="/root/baisheng/ git/ log.go:11:2: could not import golang.org/x/sync/semaphore (invalid package name: \" \")" dry-run=false version=v2.9.4

shimron commented 1 year ago

did you fix it ? I have the same issue

anthonyGuo commented 1 year ago

add this to go.sum

golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=

fixed.

mieliespoor commented 1 year ago

Seeing the same with aws sdk

Error parsing file error="/src/aws/ecs.go:9:2: could not import github.com/aws/aws-sdk-go/aws (invalid package name: \"\")" dry-run=false version=v2.14.1

go.sum:

github.com/aws/aws-sdk-go v1.44.126 h1:7HQJw2DNiwpxqMe2H7odGNT2rhO4SRrUe5/8dYXl0Jk=
github.com/aws/aws-sdk-go v1.44.126/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=

Any ideas how to fix this?

sgargan commented 1 year ago

this is super frustrating, but is often solved by running a go mod tidy

domino14 commented 1 year ago

go mod tidy didn't work for me

shimron commented 1 year ago

have a try with go mod vendor

LandonTClipp commented 1 year ago

When running into this issue, please try using one of the binary distributions of mockery. A lot of these issues happen because of dependency issues in your go module, which is why go install is not recommended.

hsequeda commented 1 year ago

I get this error with v2.26.1 using docker version, I solved it by running the same command with the binary version after run go mod download.

PerMalmberg commented 10 months ago

Running go get on the module that, based on the error message, doesn't have a package name solved it for us.

Example: go get github.com/username/repo-name

LandonTClipp commented 10 months ago

If others could confirm if that solution above works, I'll add it to the FAQ ^

j1cs commented 9 months ago

using -v "${GOPATH:-$HOME/go}/pkg/mod:/go/pkg/mod" worked for me:

docker run --rm \
    -v "${PWD}:/src" \
    -v "${PWD}/scripts/mockery.yaml:/mockery.yaml" \
    -v "${GOPATH:-$HOME/go}/pkg/mod:/go/pkg/mod" \
    -w /src \
    --user "$(id -u):$(id -g)" \
    vektra/mockery \
    --config="/mockery.yaml"
apexskier commented 8 months ago

go get and go mod tidy did not work for me. I hit this when my effective $GOPATH being used by my container had go 1.20 files in it and I was running in a go 1.21 context. (I'm running within a more complex docker abstraction). I was able to fix by passing in the proper versioned go path.

yessaliyev commented 7 months ago
-v "${GOPATH:-$HOME/go}/pkg/mod:/go/pkg/mod"

thanks @j1cs

takanuva15 commented 4 months ago

I found this question while googling this very error occurring in one of my org's private repos, and it turns out I was using the wrong Github secret key to give access to my other private repos within my GitHub Action. (lol feel like a dummy after wasting 30 mins trying different random solutions before realizing I had the wrong key the whole time)

alexandear commented 4 days ago

@LandonTClipp seems like we can close the issue as #824 has been merged.