go: cmd/go: go mod init fails to determine module path in subdirectory
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version
)?
1.11
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=""
GOCACHE="/Users/andig/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/andig/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/andig/htdocs/goelster/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=/var/folders/73/89ycv7qn51j4kbm04jsz9b840000gn/T/go-build453007721=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
Trying to init go modules, I’m seing it fail from one folder but working from another. Both folders only contain two simple go files:
~/htdocs/canprogs/go master*
❯ go mod init
go: cannot determine module path for source directory /Users/andig/htdocs/canprogs/go (outside GOPATH, no import comments)
~/htdocs/goelster master
❯ go mod init
go: creating new go.mod: module github.com/andig/goelster
❯ ls
elster.go main.go
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 27 (14 by maintainers)
Commits related to this issue
- Issue (https://github.com/golang/go/issues/27951) is closed — committed to iwdgo/testinghello by iwdgo 5 years ago
Have you tried
go mod init <module_name>
? (e.g.go mod init goelster
)Being new to go mod, I run into the exact same issue today. It did take me a while googling, with the right combination of key words to finally land in here. The documentation makes sense to me and I’m OK with the implicit dependency of go mod and git. However, I’d suggest (at least) make the error message more meaningful or explicit. It will save quite a bit of time for n00bs. Cheers,
I have been able to resolve this with code in a repo subdir with
Surely this can be automated?!
go mod init
with no arguments does it’s best to determine a module path if the current directory belongs to a VCS repository that has a remote:You can alternatively provide an explicit argument, as you outlined in https://github.com/golang/go/issues/27951#issuecomment-429533293.
The bug you correctly identified is that
go mod init
with no arguments does not do the correct thing in a subdirectory of the VCS repo root:This has bitten me for the second time today. Needs a meaningful error message at the very least
This solved it for me! Thank you @joshuamkite!!
That creates a go.mod:
is this the expected behavior?
It’s good enough for people who already know the idiosyncrasy, not good enough for n00bs like me coming from dep. There will be lots of n00bs.
It seems to me that
go mod init
is completely dependent on the existence of a git remote named “origin”. I got the OP’s output here because I only had the origins “upstream” and “rfay”. Once I added an “origin” (equivalent to upstream), thengo mod init
worked. This doesn’t seem like the behavior I’d expect.maybe a hint towards the correct command?
I’d 👍 for expanding
go help mod init
as an alternative.Killing the github.com special case seems fine. I would like to leave all the other, better, higher-priority auto-deriving of module paths, since those are much more accurate and helpful.
The remote name “origin” is absolutely arbitrary (although ever-so-widely used), and should not be a fundamental assumption. Just a --remote=<something> arg would solve the problem and make it explicit that
go mod init
is dependent on the remote (and on a specific named remote). It would be fine for “origin” to be the default.