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: "no required module provides package" is confusing for paths under the current module #44961

Open
mvdan opened this issue Mar 12, 2021 · 3 comments

Comments

@mvdan
Copy link
Member

@mvdan mvdan commented Mar 12, 2021

Using https://pkg.go.dev/golang.org/x/tools/cmd/benchcmp as an example:

$ go version
go version devel +cf59850466 Wed Mar 10 09:01:05 2021 +0000 linux/amd64
$ cd tools
$ git checkout -q 44abc2a71b321b77c0a579d8c5bf2704d648c9e0
$ cd cmd/benchcmp
$ sed -i 's@benchmark/parse@missing/package@g' *.go
$ go build
benchcmp.go:15:2: no required module provides package golang.org/x/tools/missing/package; to add it:
	go get golang.org/x/tools/missing/package
$ go get golang.org/x/tools/missing/package
go get golang.org/x/tools/missing/package: no matching versions for query "upgrade"

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:

  • Rebasing or applying an old patch that imports a package which is now moved or deleted
  • Writing an import manually and making a typo

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 path golang.org/x/tools/missing or golang.org/x/tools/missing/package, which could possibly contain the package needed. And, when I run the go 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 suggest go get. It's very unlikely that it will fix the problem. If the user really has nested modules that would be fixed with a go get, I imagine they know what they're doing and can figure out what to do.

For example, here's a suggestion:

$ go build
benchcmp.go:15:2: package golang.org/x/tools/missing/package does not exist in the main module
  1. The go get error is confusing. For example, see what happens if I try to upgrade the main module:
$ go get -d golang.org/x/tools@latest
go get: can't request version "latest" of the main module (golang.org/x/tools)

Now, compare that with package paths:

$ go get -d golang.org/x/tools/foo/bar
go get golang.org/x/tools/foo/bar: no matching versions for query "upgrade"
$ go get -d golang.org/x/tools/foo/bar@latest
go get golang.org/x/tools/foo/bar@latest: no matching versions for query "latest"

If no nested module is found, I think it should just show the can't request version error, just like go get -d golang.org/x/tools@latest.

@jayconrod
Copy link
Contributor

@jayconrod jayconrod commented Mar 12, 2021

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.

@gopherbot
Copy link

@gopherbot gopherbot commented Jun 5, 2021

Change https://golang.org/cl/324470 mentions this issue: cmd/go: improve "invalid version" error messages

@bcmills
Copy link
Member

@bcmills bcmills commented Jun 7, 2021

Compare #33568.

@jayconrod jayconrod removed this from the Go1.17 milestone Jul 29, 2021
@jayconrod jayconrod added this to the Go1.18 milestone Jul 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants