Home > front end >  "go: go.mod file not found in current directory" but it already exist in the directory
"go: go.mod file not found in current directory" but it already exist in the directory

Time:12-06

I'm trying to build a DockerFile for a project around GCP. I'm using go version 1.17 and it fails at the get command saying that go.mod isn't found but it exist in the same directory as the Dockerfile. I already tried go mod init and go mod tidy but I still got the same error. Here are my env variables and my files :

GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/name/.cache/go-build"
GOENV="/home/name/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/name/work/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/name/work"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.1"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g  "
CGO_ENABLED="1"
GOMOD="/home/name/workspace/professional-services/tools/gcs2bq/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3855548593=/tmp/go-build -gno-record-gcc-switches"

list of the files in my working directory :

name@vm-gcs2bq:~/workspace/professional-services/tools/gcs2bq$ ls
Dockerfile  bigquery.schema   datastudio.png           gcs2bq.avsc  go.mod  main.go
README.md   credentials.json  gcs2bq-custom-role.yaml  gcs2bq.yaml  go.sum  run.sh

My working directory in the Dockerfile is correctly set (the gcs2bq one) and when trying to build it I got :

Step 6/16 : RUN go get -v ./...
 ---> Running in ebaa284887cf
go: go.mod file not found in current directory or any parent directory; see 'go help modules'

I'm still kinda new to go, mostly did c and python and I read that packages are managed in a different way in this language but I think I set my paths correctly too. Correct me if I'm wrong.

Any help is appreciated, ask for more details if you need to.

Thanks in advance !

CodePudding user response:

Okay I solved my problem.

First, my WORKDIR wasn't pointing at the right directory : WORKDIR /go/src/github.com/rosmo/gcs2bq instead of WORKDIR /work/src/github.com/rosmo/gcs2bq but it's only because of me using /work instead of /go for the installed packages.

Then I added the follwing after the COPY main.go . command :

RUN  go mod init v1
RUN go mod tidy

Which created the "missing" go.mod file and properly installed the dependencies/packages needed.

The rest of the build went perfectly normal.

Thanks for your help.

  • Related