Skip to content

cmd/go: unhelpful "unknown import path" error for 'go build' in an empty module directory #28459

@vdobler

Description

@vdobler

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

go version go1.11.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

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

At least amd64/darwin and amd64/linux, probably all of them.

What did you do?

If a module is just a wrapper for other packages some commands fail
unexpectedly. They also fail with error messages which are not really
helpful diagnosing the underlying problem.

I set up a module which is just a wrapper around two packages a and b
versioned together:

$ tree
.
├── a
│   └── a.go
├── b
│   └── b.go
└── go.mod
$ cat go.mod
module path/should/not/matter

Doing a go build ./... and works fine but go build results in

can't load package: package path/should/not/matter: unknown import path "path/should/not/matter": cannot find module providing package path/should/not/matter

Other commands like go clean -modcache result in the same error.

I think the error can be explained like this:

  1. go build or go clean -modcache are like go build . or go clean -modcache .
  2. Looking up the what . stands for in go.mod yields "path/should/not/matter"
  3. The go tool doesn't know what to do with this import path.
    (BTW: if the module is e.g. git.intern.ourcorp.net/shared/tooling
    the go tool will start querying Fetching https://git.intern.ourcorp.net/shared?go-get=1
    to no avail.)

What did you expect to see?

I would expect that a GO111MODULE=on go build fails with the same
error message like a GO111MODULE=off go build in a GOPATH layout
namely:

can't load package: package stuff: no Go files in $GOPATH/src/stuff

To reproduce:
$ cd /tmp
$ mkdir stuff
$ cd stuff/
$ mkdir a b
$ echo -e 'package a\nconst S="Package A"' > a/a.go
$ echo -e 'package b\nconst S="Package B"' > b/b.go
$ go mod init "path/should/not/matter"
go: creating new go.mod: module path/should/not/matter
$ go build ./... # fine
$ go clean -modcache
can't load package: package path/should/not/matter: unknown import path "path/should/not/matter": cannot find module providing package path/should/not/matter
$ go build .
can't load package: package path/should/not/matter: unknown import path "path/should/not/matter": cannot find module providing package path/should/not/matter

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeGoCommandcmd/goNeedsFixThe path to resolution is known, but the work has not been done.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.modules

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions