Skip to content

cmd/go: go mod init fails to determine module path in subdirectory #27951

Closed
@andig

Description

@andig

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

Activity

bcmills

bcmills commented on Oct 1, 2018

@bcmills
Contributor

Please attach the output of go env (as requested by the issue template).

bcmills

bcmills commented on Oct 1, 2018

@bcmills
Contributor

Which directory (if any) is at the root of your VCS checkout, and what is your GOPATH?

added
WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Oct 1, 2018
added this to the Go1.12 milestone on Oct 1, 2018
changed the title [-]go mod init fails[/-] [+]cmd/go: go mod init fails in subdirectory[/+] on Oct 1, 2018
changed the title [-]cmd/go: go mod init fails in subdirectory[/-] [+]cmd/go: go mod init fails to determine module path in subdirectory[/+] on Oct 1, 2018
andig

andig commented on Oct 1, 2018

@andig
ContributorAuthor

Please attach the output of go env

Sorry, only noticed the "operating system" question, added go env above. ~/htdocs/canprogs is a git root, so is ~/htdocs/goelster. Neither is part of the gopath.

andig

andig commented on Oct 4, 2018

@andig
ContributorAuthor

@bcmills I see this has been scheduled- seems I've hit an actual issue here?

xswordsx

xswordsx commented on Oct 10, 2018

@xswordsx

Have you tried go mod init <module_name>? (e.g. go mod init goelster)

andig

andig commented on Oct 13, 2018

@andig
ContributorAuthor

That creates a go.mod:

❯ go mod init goelster
go: creating new go.mod: module goelster

❯ cat go.mod
module goelster

is this the expected behavior?

myitcv

myitcv commented on Nov 13, 2018

@myitcv
Member

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:

$ cd $(mktemp -d)
$ git init
Initialized empty Git repository in /tmp/tmp.T8elQyoqel/.git/
$ git remote add origin https://github.com/myitcv/gobin
$ go mod init
go: creating new go.mod: module github.com/myitcv/gobin

You can alternatively provide an explicit argument, as you outlined in #27951 (comment).

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:

$ mkdir inner
$ cd inner
$ go mod init
go: cannot determine module path for source directory /tmp/tmp.T8elQyoqel/inner (outside GOPATH, no import comments)
added
NeedsFixThe path to resolution is known, but the work has not been done.
and removed
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Nov 14, 2018

12 remaining items

mxork

mxork commented on Mar 28, 2019

@mxork

maybe a hint towards the correct command?

cannot guess module path for source directory ...
try specifying module path explicitly: 

    go mod init <PATH>

I'd 👍 for expanding go help mod init as an alternative.

iwdgo

iwdgo commented on Mar 31, 2019

@iwdgo
Contributor

go get is supporting several VCS including other git platforms. It is unclear from the online documentation about modules and its FAQ answer that support seems currently limited to github as the regex and its use above implies. Mentioning the current limit would improve the understanding of the message.

bcmills

bcmills commented on Apr 1, 2019

@bcmills
Contributor

@iwdgo, to be honest, I'm not sure that inferring github.com paths is really the right behavior either. Some of those are served through other import paths (via redirects and/or <meta> tags), so go mod init can very easily pick the wrong path.

The right solution might be to remove the github.com special-case and always require an explicit path when outside of GOPATH.

iwdgo

iwdgo commented on Apr 3, 2019

@iwdgo
Contributor

I agree that best effort to locate path is confusing and removal seems most appropriate.

A default value set to the folder name where the module resides could be considered but might be the subject of a separate issue. The behavior would become similar to the $GOPATH related code which sets a value only when the module resides in $GOPATH/src/<module-path>

The discussed message would be irrelevant and could be replaced with "default module path set to ".

rsc

rsc commented on Apr 9, 2019

@rsc
Contributor

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.

gopherbot

gopherbot commented on Apr 14, 2019

@gopherbot
Contributor

Change https://golang.org/cl/172019 mentions this issue: cmd/go: remove auto-deriving module path for github.com

joshuamkite

joshuamkite commented on Apr 14, 2019

@joshuamkite

I have been able to resolve this with code in a repo subdir with

go mod init `pwd` 

Surely this can be automated?!

added a commit that references this issue on Apr 17, 2019
SunSparc

SunSparc commented on May 17, 2019

@SunSparc

I have been able to resolve this with code in a repo subdir with

go mod init `pwd` 

This solved it for me! Thank you @joshuamkite!!

qhkm

qhkm commented on Sep 28, 2019

@qhkm

I have been able to resolve this with code in a repo subdir with

go mod init `pwd` 

Surely this can be automated?!

This solved for me too. Thanks!!

mrchuanxu

mrchuanxu commented on Nov 14, 2019

@mrchuanxu

why is go mod init pwd solved? why?

bcmills

bcmills commented on Nov 15, 2019

@bcmills
Contributor

go mod init `pwd`​ is highly unlikely to be correct: the argument to go mod init is a module path, but pwd is generally an absolute filesystem path. (See #28389.)

If the go command cannot determine an appropriate module path, the general solution is no pass one explicitly. See https://golang.org/cmd/go/#hdr-Defining_a_module and https://blog.golang.org/using-go-modules.

locked as resolved and limited conversation to collaborators on Nov 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.modules

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rsc@rfay@jayconrod@andig@quite

        Issue actions

          cmd/go: go mod init fails to determine module path in subdirectory · Issue #27951 · golang/go