Open mvdan opened 3 years ago
While the error message is technically correct (the package could be in another module with a longer path, and go get
might add the correct requirement), it's far more likely that the package just doesn't exist. I agree that your proposed go build
error message is better. The go get
messages seem like they could be improved, too.
Change https://golang.org/cl/324470 mentions this issue: cmd/go: improve "invalid version" error messages
Compare #33568.
This is in the Go1.18 milestone. Is it likely to happen for 1.18? Thanks.
@bcmills This is in the 1.18 milestone; time to move to 1.19? Thanks.
no required module provides package golang.org/x/w32
Issue seems solved. It is difficult to locate which commit but on go1.18 with the case filed, the message is more explicit. The reference to go get
is replaced by the import causing the error (missing/path
in this case).
GOPATH\golang\x\tools\cmd\benchcmp>go version
go version go1.18.1 windows/amd64
GOPATH\golang\x\tools\cmd\benchcmp>set GO111MODULE
GO111MODULE=off
GOPATH\golang\x\tools\cmd\benchcmp>go build
code in directory C:\Users\Costa\Documents\Google\golang\x\tools\cmd\benchcmp expects import "golang.org/x/tools/cmd
/benchcmp"
benchcmp.go:10:2: cannot find package "missing/path" in any of:
C:\Program Files\Go\src\missing\path (from $GOROOT)
C:\Users\Costa\Documents\Google\src\missing\path (from $GOPATH)
GOPATH\golang\x\tools\cmd\benchcmp>set GO111MODULE=
GOPATH\Google\golang\x\tools\cmd\benchcmp>go build
benchcmp.go:10:2: cannot find package "." in:
C:\Users\Costa\Documents\Google\golang\x\tools\vendor\missing\path
GOPATH\golang\x\tools\cmd\benchcmp>
Just wanted to add a comment to this. If your dependency looked like github.com/my-org/my-repo/missing/package
You can get this as well if you have something like this in your go.work
:
go 1.22.3
toolchain go1.22.5
use (
./my-repo
)
My local version wasn't updated and this was driving me nuts. All I had to do is git pull
in my local copy of the referenced repo and the missing package started appearing. You'd never guess that from the error message.
Using https://pkg.go.dev/golang.org/x/tools/cmd/benchcmp as an example:
What I do here is add an import to a missing package that could be part of the current main module. The example I use is perhaps silly, but it's somewhat common to end up in this situation. For example:
I get why the errors are technically correct. When I run
go build
, the go tool doesn't know if there happens to be a module with pathgolang.org/x/tools/missing
orgolang.org/x/tools/missing/package
, which could possibly contain the package needed. And, when I run thego get
, it finds no such module, and the main module can't be upgraded to contain such a package.First, an assumption: nested Go modules are very rare, and should not be encouraged. So I don't think that we should be optimizing the error messages for that use case.
Now, some suggestions from the user's point of view, for this scenario:
1) The
go build
error shouldn't suggestgo get
. It's very unlikely that it will fix the problem. If the user really has nested modules that would be fixed with ago get
, I imagine they know what they're doing and can figure out what to do.For example, here's a suggestion:
2) The
go get
error is confusing. For example, see what happens if I try to upgrade the main module:Now, compare that with package paths:
If no nested module is found, I think it should just show the
can't request version
error, just likego get -d golang.org/x/tools@latest
.