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
heschi opened this issue Feb 13, 2020 · 5 comments
Labels
GoCommand cmd/go modules NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@heschi
Copy link
Contributor

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

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 The path to resolution is known, but the work has not been done. label Feb 13, 2020
@bcmills bcmills added this to the Backlog milestone Feb 13, 2020
@heschi
Copy link
Contributor Author

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

@heschi
Copy link
Contributor Author

heschi commented Feb 14, 2020

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

@gopherbot
Copy link

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

iwdgo commented Jul 19, 2021

Message seems now inline with expectations:

>gotip version
go version devel go1.17-aa4e0f528e Fri Jul 16 02:43:48 2021 +0000 windows/amd64

>gotip run main.go
main.go:6:9: no required module provides package example.com/mypkg; to add it:
        go get example.com/mypkg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants