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

Created on 29 Oct 2018  ยท  12Comments  ยท  Source: golang/go

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

GoCommand NeedsFix WaitingForInfo modules

Most helpful comment

That said, we should improve the error message.

All 12 comments

@vdobler - please can you edit the issue description to follow the full issue template? It makes it much easier to read and digest issues. Thanks

The fact that go build without arguments fails is working as designed. Per https://golang.org/cmd/go/#hdr-Package_lists_and_patterns:

If no import paths are given, the action applies to the package in the current directory.

That said, we should improve the error message.

Change https://golang.org/cl/149966 mentions this issue: modules: Fix incorrect error with modules containing no Go source

Change https://golang.org/cl/153459 mentions this issue: cmd/go: fix 'go test' and 'go fmt' with files outside a module

This issue appears to extend further - build.Import with build.FindOnly should return the directory path resolved for an import, even if there are no Go files in the directory, but this does not seem to be the case with Go modules.

Change https://golang.org/cl/185343 mentions this issue: cmd/go/internal/modload: make packageNotInModuleError reasonable for the Target module

Change https://golang.org/cl/185345 mentions this issue: cmd/go: rationalize errors in internal/load and internal/modload

Change https://golang.org/cl/189780 mentions this issue: cmd/go/internal/modload: propagate errors from Query for missing imports

Change https://golang.org/cl/194817 mentions this issue: cmd/go/internal/modload: add an Unwrap method on ImportMissingError

As far as I can tell this is fixed at head.

~/src/golang.org/x/mobile$ gotip version
go version devel +0915a19a Fri Dec 6 05:12:15 2019 +0000 linux/amd64

~/src/golang.org/x/mobile$ GO111MODULE=on gotip build
can't load package: package .: no Go files in /usr/local/google/home/bcmills/src/golang.org/x/mobile

~/src/golang.org/x/mobile$ GO111MODULE=off gotip build
can't load package: package golang.org/x/mobile: no Go files in /usr/local/google/home/bcmills/src/golang.org/x/mobile

@vdobler, is this also fixed for your specific example?

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

Was this page helpful?
0 / 5 - 0 ratings