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: "package not found" error is less useful than GOPATH's when the missing package could have been in the main module #37214

Open
heschik opened this issue Feb 13, 2020 · 4 comments

Comments

@heschik
Copy link
Contributor

@heschik heschik commented Feb 13, 2020

Given the following setup:

-- gopath/src/example.com/go.mod --
module example.com

go 1.14
-- gopath/src/example.com/main.go --
package main

import (
        "fmt"

        "example.com/mypkg"
)

func main() {
        fmt.Println(mypkg.Message)
}
-- gopath/src/example.com/mypackage/doc.go --
package mypackage

const Message = "sup"

and then running:

$ export GOPATH=$PWD/gopath
$ cd gopath/src/example.com
$ GO111MODULE=off go run main.go
main.go:6:2: cannot find package "example.com/mypkg" in any of:
	.../go/src/example.com/mypkg (from $GOROOT)
	../gopath/src/example.com/mypkg (from $GOPATH)

This is clear and actionable: there are two directories that could have contained the package, and neither did. You can ls the directories, compare the paths to the filesystem, etc.

In module mode:

$ GO111MODULE=on gotip run main.go
go: finding module for package example.com/mypkg
main.go:6:2: cannot find module providing package example.com/mypkg: unrecognized import path "example.com/mypkg": reading https://example.com/mypkg?go-get=1: 404 Not Found

This is much less actionable. Why is it going to the Internet for a package that should be on the local filesystem? Did it look anywhere first, and if so where? (If example.com were a GitHub URL the error would be different, but talking about private repos isn't any more helpful here.)

In case it wasn't obvious, the problem is a typo: the package is named mypackage, not mypkg. I think the GOPATH error gives a new user a fighting chance of finding their mistake; the module error needs at least a basic understanding of package-to-module resolution, and nested modules (!) to make any sense.

Concretely, I would like to see the module mode error look more like the GOPATH error:

main.go:6:2: cannot find package "example.com/mypkg" in any of:
   	.../go/src/example.com/mypkg (standard library)
	../gopath/src/example.com/mypkg (module example.com)
       https://example.com/mypkg (module example.com/mypkg): unrecognized import path "example.com/mypkg": reading https://example.com/mypkg?go-get=1: 404 Not Found

@bcmills @jayconrod

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Feb 13, 2020

main.go:6:2: cannot find package "example.com/mypkg" in any of:
	.../go/src/example.com/mypkg (standard library)

This line would be misleading: we would not even check that directory, because the first component of the missing import path contains a dot.

	../gopath/src/example.com/mypkg (module example.com)

This one we can perhaps fix. (Compare #33568 (comment).)

If we don't have any candidate modules to check in the first place, we produce an error message like:

package example.com/mypkg is not in the main module (example.com)

(That comes from here.)

And note that if example.com were some existing module other than the main module, we would produce something like:

module example.com@latest found (v1.2.3), but does not contain package example.com/mypkg

So probably we should mention the main module in some form here too. But we need to be careful about how we describe the file path, because it is entirely possible that ../gopath/src/example.com/mypkg does exist, but is in an unpublished nested module with its own go.mod file (rather than the main module).

@bcmills bcmills changed the title cmd/go: module mode's "package not found" error is less useful than GOPATH's cmd/go: "package not found" error is less useful than GOPATH's when the missing package could have been in the main module Feb 13, 2020
@bcmills bcmills added the NeedsFix label Feb 13, 2020
@bcmills bcmills added this to the Backlog milestone Feb 13, 2020
@heschik

This comment has been minimized.

Copy link
Contributor Author

@heschik heschik commented Feb 13, 2020

Sure, I didn't mean to dictate any specifics. I was more arguing for a philosophy of error reporting: for each location looked up, there should be some variety of not found error, and if resolution eventually fails, the resulting message should detail each location searched and why it didn't work. So for the case at the end of your comment, I would hope for something like ../gopath/src/example.com/mypkg (module example.com): is hidden by nested module example.com/mypkg. I'm assuming things about the structure of the code, of course.

@heschik

This comment has been minimized.

Copy link
Contributor Author

@heschik heschik commented Feb 14, 2020

Also, I'd like to see the same thing for replace targets, not just the main module.

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Feb 24, 2020

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

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>
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
3 participants
You can’t perform that action at this time.