“go: 在当前目录中找不到 go.mod 文件”,但该文件已经存在于该目录中。

huangapple go评论74阅读模式
英文:

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

问题

我正在尝试为围绕GCP的项目构建一个DockerFile。
我正在使用go版本1.17,在get命令中失败,提示找不到go.mod,但它与Dockerfile在同一个目录中。我已经尝试过go mod initgo mod tidy,但仍然遇到相同的错误。这是我的环境变量和文件:

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"

工作目录中的文件列表:

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

我的Dockerfile中的工作目录设置正确(gcs2bq),但在尝试构建时出现以下错误:

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'

我对go还比较新,主要使用C和Python,我了解到在这种语言中包的管理方式不同,但我认为我也正确设置了路径。如果我错了,请纠正我。

非常感谢您的帮助!如果需要更多细节,请告诉我。

英文:

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 !

答案1

得分: 1

好的,我为您翻译如下内容:

好的,我解决了我的问题。

首先,我的WORKDIR没有指向正确的目录:WORKDIR /go/src/github.com/rosmo/gcs2bq,而应该是WORKDIR /work/src/github.com/rosmo/gcs2bq,但这只是因为我在安装包时使用了/work而不是/go。

然后,在COPY main.go .命令之后,我添加了以下内容:

RUN  go mod init v1
RUN go mod tidy

这样创建了"missing"的go.mod文件,并正确安装了所需的依赖/包。

构建的其余部分进行得非常顺利。

感谢您的帮助。

英文:

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.

huangapple
  • 本文由 发表于 2021年12月4日 00:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/70217349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定