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: unhelpful "unknown import path" error for 'go build' in an empty module directory #28459

Open
vdobler opened this Issue Oct 29, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@vdobler
Contributor

vdobler commented Oct 29, 2018

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

@myitcv

This comment has been minimized.

Member

myitcv commented Oct 29, 2018

@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

@myitcv myitcv changed the title from Problems if top-level directory of a module contains no Go code to cmd/go: problems if top-level directory of a module contains no Go code Oct 29, 2018

@myitcv myitcv removed the WaitingForInfo label Nov 2, 2018

@bcmills

This comment has been minimized.

Member

bcmills commented Nov 16, 2018

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.

@bcmills

This comment has been minimized.

Member

bcmills commented Nov 16, 2018

That said, we should improve the error message.

@bcmills bcmills changed the title from cmd/go: problems if top-level directory of a module contains no Go code to cmd/go: unhelpful "unknown import path" error for 'go build' in an empty module directory Nov 16, 2018

@bcmills bcmills added this to the Go1.13 milestone Nov 16, 2018

@gopherbot

This comment has been minimized.

gopherbot commented Nov 17, 2018

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

@gopherbot

This comment has been minimized.

gopherbot commented Dec 10, 2018

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

gopherbot pushed a commit that referenced this issue Dec 11, 2018

cmd/go: fix 'go test' and 'go fmt' with files outside a module
Use the actual loader result in findModule instead of making
assumptions about nesting in the build list.
As a side-effect, this produces much clearer error messages for
packages that (for one reason or another) failed to load.

Adjust the package and module path outside a module to
"command-line-arguments". That string already appears in the output of
a number of (module-mode and GOPATH-mode) commands for file arguments,
and as far as I can tell operation outside a module is currently the
only case in which the module path of a package is not actually a
prefix of the import path.

Fixes #28011
Fixes #27099
Fixes #28943
Updates #27102
Updates #28459
Updates #27063

Change-Id: I61d5556df7b1b7d1efdaffa892f0e3e95b612d87
Reviewed-on: https://go-review.googlesource.com/c/153459
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment