Skip to content
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

Closed
vdobler opened this issue Oct 29, 2018 · 12 comments
Labels
FrozenDueToAge GoCommand cmd/go modules NeedsFix The path to resolution is known, but the work has not been done. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@vdobler
Copy link
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
Copy link
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 added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. modules labels Oct 29, 2018
@myitcv myitcv changed the title Problems if top-level directory of a module contains no Go code cmd/go: problems if top-level directory of a module contains no Go code Oct 29, 2018
@myitcv myitcv removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 2, 2018
@bcmills
Copy link
Contributor

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
Copy link
Contributor

bcmills commented Nov 16, 2018

That said, we should improve the error message.

@bcmills bcmills changed the title cmd/go: problems if top-level directory of a module contains no Go code cmd/go: unhelpful "unknown import path" error for 'go build' in an empty module directory Nov 16, 2018
@bcmills bcmills added NeedsFix The path to resolution is known, but the work has not been done. GoCommand cmd/go labels Nov 16, 2018
@bcmills bcmills added this to the Go1.13 milestone Nov 16, 2018
@gopherbot
Copy link

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

@gopherbot
Copy link

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
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>
@paralin
Copy link
Contributor

paralin commented Mar 2, 2019

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.

@gopherbot
Copy link

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

@gopherbot
Copy link

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

@gopherbot
Copy link

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

gopherbot pushed a commit that referenced this issue Sep 11, 2019
Updates #30748
Updates #28459

Change-Id: I1c34b3dae0bf9361dba0dae66bb868901ecafe29
Reviewed-on: https://go-review.googlesource.com/c/go/+/189780
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
@gopherbot
Copy link

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

gopherbot pushed a commit that referenced this issue Sep 12, 2019
Jay suggested this in CL 189780, and it seems semantically correct.

As far as I can tell this has no impact one way or the other right
now, but might prevent confusion (or at least give us more experience
with error handling!) in future changes.

Updates #30748
Updates #28459
Updates #30322

Change-Id: I5d7e9a08ea141628ed6a8fd03c62d0d3c2edf2bb
Reviewed-on: https://go-review.googlesource.com/c/go/+/194817
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@bcmills
Copy link
Contributor

bcmills commented Dec 6, 2019

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?

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Dec 6, 2019
@gopherbot
Copy link

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.)

gopherbot pushed a commit that referenced this issue Feb 27, 2020
…the Target module

Updates #28459
Updates #32917

Change-Id: Iced562cb7c2e0ac075d8345f1e4ad3b073842dcf
Reviewed-on: https://go-review.googlesource.com/c/go/+/185343
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
gopherbot pushed a commit that referenced this issue Feb 28, 2020
This change is a non-minimal fix for #32917, but incidentally fixes
several other bugs and makes the error messages much more ergonomic.

Updates #32917
Updates #27122
Updates #28459
Updates #29280
Updates #30590
Updates #37214
Updates #36173
Updates #36587
Fixes #36008
Fixes #30992

Change-Id: Iedb26d2e0963697c130df5d0f72e7f83ec2dcf06
Reviewed-on: https://go-review.googlesource.com/c/go/+/185345
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
@golang golang locked and limited conversation to collaborators Jan 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge GoCommand cmd/go modules NeedsFix The path to resolution is known, but the work has not been done. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

7 participants