Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: "cannot find main module" when running 'go build' on a .go source file #28867

Closed
greenpau opened this issue Nov 19, 2018 · 14 comments
Closed
Labels
FrozenDueToAge modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@greenpau
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.11.2 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

$ go env

GOARCH="amd64"
GOBIN="/root/dev/go/bin"
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/dev/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/root/dev/go/src/github.com/mholt/caddy/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-build980719409=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I am compiling caddy.

unset GO111MODULE
go get github.com/mholt/caddy/caddy
cd $GOPATH/src/github.com/mholt/caddy/
# enable go modules
export GO111MODULE=on
# generates go.mod and go.sum
go mod init
# download detected dependencies
go mod download
# build caddy
cd $GOPATH/src/github.com/mholt/caddy/caddy/
go build -v -x main.go

What did you expect to see?

Successful compilation.

What did you see instead?

This results in go not being able to find standard library modules, i.e. "bufio".

WORK=/tmp/go-build483698734
build github.com/mholt/caddy/caddy: cannot find module for path bufio
@mvdan
Copy link
Member

mvdan commented Nov 19, 2018

Is your Go installation broken by any chance? What does go list bufio print? What if you do go build main.go, with a very simple program that imports bufio?

@mvdan mvdan added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. modules labels Nov 19, 2018
@greenpau
Copy link
Author

Is your Go installation broken by any chance?

@mvdan, it is unlikely. I took the latest from https://golang.org/dl/. It is just go binary does not look for the modules inside $GOROOT.

go list bufio
can't load package: package bufio: unknown import path "bufio": cannot find module providing package bufio

@greenpau
Copy link
Author

@mvdan , specifically go1.11.2.linux-amd64.tar.gz

@mvdan
Copy link
Member

mvdan commented Nov 19, 2018

Certainly seems like your install is confused or weird. The command works for me in module mode.

$ go version
go version go1.11.2 linux/amd64
$ go env GOMOD
/home/mvdan/src/gunk/go.mod
$ go list bufio
bufio

/cc @bcmills

@greenpau
Copy link
Author

GOMOD is only there if GO111MODULE is set.

@greenpau
Copy link
Author

Here, the issue that go does not look for packages in GOROOT.

@mvdan
Copy link
Member

mvdan commented Nov 19, 2018

If go env GOMOD is empty, you're not in module mode, so this issue should have nothing to do with modules.

Whatever environment you're in, go list should work with standard library packages. Have you tried other commands like go list std or go list io? What about building a simple program, like I explained above?

@greenpau
Copy link
Author

@mvdan , the below shows the standard library modules. For now, I will just switch back to 1.10.5.

$ go list std
archive/tar
archive/zip
bufio
...

@bcmills
Copy link
Contributor

bcmills commented Nov 19, 2018

@greenpau, I'm not able to reproduce the problem with a go command built near HEAD.

From the previous comments it's not clear to me which commands you're running and whether module mode is actually enabled. Please post a complete transcript of the commands you invoked and their output (ideally within a <details> tag). See the below example for my attempt.

$ GO111MODULE=off go get github.com/mholt/caddy/caddy

$ cd $GOPATH/src/github.com/mholt/caddy

_gopath/src/github.com/mholt/caddy$ go mod init
go: creating new go.mod: module github.com/mholt/caddy
go: copying requirements from vendor/manifest

_gopath/src/github.com/mholt/caddy$ cd caddy

_gopath/src/github.com/mholt/caddy/caddy$ go build .
go: finding github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf
go: finding github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568
go: finding github.com/xenolf/lego v0.0.0-20180420000218-fad2257e11ae
go: finding github.com/golang/mock v0.0.0-20180820161358-600781dde9cc
go: finding github.com/gorilla/websocket v0.0.0-20170718202341-a69d9f6de432
go: finding github.com/golang/protobuf v0.0.0-20170726212829-748d386b5c1e
go: finding github.com/russross/blackfriday v0.0.0-20170610170232-067529f716f4
go: finding github.com/dustin/go-humanize v0.0.0-20170228161531-259d2a102b87
go: finding github.com/miekg/dns v0.0.0-20170721150254-0f3adef2e220
go: finding github.com/codahale/aesnicheck v0.0.0-20140702143623-349fcc471aac
go: finding github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc
go: finding github.com/jimstudt/http-authentication v0.0.0-20140401203705-3eca13d6893a
go: finding github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da
go: finding golang.org/x/oauth2 v0.0.0-20170719200132-b53b38ad8a64
go: finding golang.org/x/text v0.0.0-20170714085652-836efe42bb4a
go: finding google.golang.org/appengine v0.0.0-20170707203349-ad2570cd3913
go: finding gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531180850-df99d62fd42d
go: finding google.golang.org/api v0.0.0-20170727025548-66dba45b0682
go: finding golang.org/x/sys v0.0.0-20180830151530-49385e6e1522
go: finding gopkg.in/square/go-jose.v2 v2.1.4
go: finding github.com/hashicorp/go-syslog v0.0.0-20170829120034-326bf4a7f709
go: finding github.com/google/uuid v0.0.0-20171129191014-dec09d789f3d
go: finding github.com/lucas-clemente/quic-go v0.10.0-no-integrationtests
go: finding github.com/naoina/toml v0.1.1
go: finding github.com/klauspost/cpuid v0.0.0-20180102081000-ae832f27941a
go: finding gopkg.in/yaml.v2 v2.0.0-20170721122051-25c4ec802a7d
go: finding github.com/naoina/go-stringutil v0.1.0
go: finding golang.org/x/crypto v0.0.0-20180313195238-c4a91bd4f524
go: finding golang.org/x/net v0.0.0-20180826012351-8a410e7b638d
go: finding gopkg.in/alecthomas/kingpin.v2 v2.2.5
go: finding go4.org v0.0.0-20170525132722-034d17a462f7
go: finding cloud.google.com/go v0.0.0-20170727181841-7a4ba9f439fb
go: downloading gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531180850-df99d62fd42d
go: downloading github.com/klauspost/cpuid v0.0.0-20180102081000-ae832f27941a
go: downloading github.com/google/uuid v0.0.0-20171129191014-dec09d789f3d
go: downloading github.com/jimstudt/http-authentication v0.0.0-20140401203705-3eca13d6893a
go: downloading github.com/xenolf/lego v0.0.0-20180420000218-fad2257e11ae
go: downloading github.com/russross/blackfriday v0.0.0-20170610170232-067529f716f4
go: downloading golang.org/x/crypto v0.0.0-20180313195238-c4a91bd4f524
go: downloading github.com/lucas-clemente/quic-go v0.10.0-no-integrationtests
go: downloading github.com/dustin/go-humanize v0.0.0-20170228161531-259d2a102b87
go: extracting gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531180850-df99d62fd42d
go: downloading github.com/gorilla/websocket v0.0.0-20170718202341-a69d9f6de432
go: extracting github.com/google/uuid v0.0.0-20171129191014-dec09d789f3d
go: downloading github.com/hashicorp/go-syslog v0.0.0-20170829120034-326bf4a7f709
go: extracting github.com/jimstudt/http-authentication v0.0.0-20140401203705-3eca13d6893a
go: extracting github.com/dustin/go-humanize v0.0.0-20170228161531-259d2a102b87
go: downloading github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568
go: extracting github.com/hashicorp/go-syslog v0.0.0-20170829120034-326bf4a7f709
go: downloading gopkg.in/yaml.v2 v2.0.0-20170721122051-25c4ec802a7d
go: downloading github.com/naoina/toml v0.1.1
go: extracting github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568
go: downloading golang.org/x/net v0.0.0-20180826012351-8a410e7b638d
go: extracting github.com/gorilla/websocket v0.0.0-20170718202341-a69d9f6de432
go: extracting github.com/klauspost/cpuid v0.0.0-20180102081000-ae832f27941a
go: extracting github.com/russross/blackfriday v0.0.0-20170610170232-067529f716f4
go: extracting github.com/xenolf/lego v0.0.0-20180420000218-fad2257e11ae
go: extracting github.com/naoina/toml v0.1.1
go: extracting gopkg.in/yaml.v2 v2.0.0-20170721122051-25c4ec802a7d
go: downloading github.com/naoina/go-stringutil v0.1.0
go: downloading gopkg.in/square/go-jose.v2 v2.1.4
go: downloading github.com/miekg/dns v0.0.0-20170721150254-0f3adef2e220
go: extracting github.com/naoina/go-stringutil v0.1.0
go: extracting github.com/miekg/dns v0.0.0-20170721150254-0f3adef2e220
go: extracting gopkg.in/square/go-jose.v2 v2.1.4
go: extracting github.com/lucas-clemente/quic-go v0.10.0-no-integrationtests
go: extracting golang.org/x/crypto v0.0.0-20180313195238-c4a91bd4f524
go: finding github.com/hashicorp/golang-lru v0.5.0
go: finding github.com/cheekybits/genny/generic latest
go: finding github.com/bifurcation/mint latest
go: finding github.com/lucas-clemente/aes12 latest
go: extracting golang.org/x/net v0.0.0-20180826012351-8a410e7b638d
go: downloading golang.org/x/text v0.0.0-20170714085652-836efe42bb4a
go: finding github.com/lucas-clemente/quic-go-certificates latest
go: finding github.com/cheekybits/genny v1.0.0
go: downloading github.com/hashicorp/golang-lru v0.5.0
go: downloading github.com/lucas-clemente/aes12 v0.0.0-20171027163421-cd47fb39b79f
go: downloading github.com/cheekybits/genny v1.0.0
go: extracting github.com/hashicorp/golang-lru v0.5.0
go: extracting github.com/lucas-clemente/aes12 v0.0.0-20171027163421-cd47fb39b79f
go: extracting github.com/cheekybits/genny v1.0.0
go: downloading github.com/bifurcation/mint v0.0.0-20181105071958-a14404e9a861
go: downloading github.com/lucas-clemente/quic-go-certificates v0.0.0-20160823095156-d2f86524cced
go: extracting github.com/bifurcation/mint v0.0.0-20181105071958-a14404e9a861
go: extracting github.com/lucas-clemente/quic-go-certificates v0.0.0-20160823095156-d2f86524cced
go: extracting golang.org/x/text v0.0.0-20170714085652-836efe42bb4a

_gopath/src/github.com/mholt/caddy/caddy$ go build main.go

_gopath/src/github.com/mholt/caddy/caddy$ go list bufio
bufio

_gopath/src/github.com/mholt/caddy/caddy$

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 19, 2018
@bcmills bcmills added this to the Go1.13 milestone Nov 19, 2018
@bcmills bcmills changed the title go mod issue: standard library modules cmd/go: "cannot find main module" when running 'go build' on a .go source file Nov 19, 2018
@greenpau
Copy link
Author

@bcmills, try taking a system where go was never present. Downlod the tar.gz file i mentioned above. Unpack it to /usr/local/go ... “tar xvzf” would do it automatically. ADD GOBIN GOROOT GOPATH.

I am not cloning from git. i literally download it, unpacking it, and using it.

@greenpau
Copy link
Author

@bcmills, i will the entire process shorlty.

@greenpau
Copy link
Author

@bcmills , @mvdan, it worked now! 😄 The issue was outdated clock.

I resolved the issue by running ntpdate -s time.nist.gov. For whatever reason the clock being behind (it is a brand new system) cause the issues above.

I guess one could set a clock to be -1day and return the go build. weird ...

@xiaoyaoyou
Copy link

don't set the $GOROOT to go install path but {go-install-path}/libexec, then append $PATH: $PATH:$GOROOT/bin

@aohanhongzhi
Copy link

maybe you should add a file named go.mod in your project
https://blog.csdn.net/yuexiazhufeng/article/details/84570680

@golang golang locked and limited conversation to collaborators Oct 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

6 participants