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

x/tools/go/packages: "-: cannot find package "nonexist" in any of: ..." #38444

Open
mdempsky opened this issue Apr 14, 2020 · 6 comments
Open

x/tools/go/packages: "-: cannot find package "nonexist" in any of: ..." #38444

mdempsky opened this issue Apr 14, 2020 · 6 comments

Comments

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Apr 14, 2020

When I run go build nonexist, I get:

package nonexist: cannot find package "nonexist" in any of:
	/home/mdempsky/wd/go/src/nonexist (from $GOROOT)
	/home/mdempsky/go/src/nonexist (from $GOPATH)

But using go/packages.Load and go/packages.PrintErrors (full code below), I get:

-: cannot find package "nonexist" in any of:
	/home/mdempsky/wd/go/src/nonexist (from $GOROOT)
	/home/mdempsky/go/src/nonexist (from $GOPATH)

Is there some parser code that thinks "package nonexist:" is not a valid source position, so it gets mapped to "-:"?

Demo code:

package main

import "golang.org/x/tools/go/packages"

func main() {
	cfg := &packages.Config{Mode: packages.NeedName}
	pkgs, err := packages.Load(cfg, "nonexist")
	if err != nil {
		panic(err)
	}
	packages.PrintErrors(pkgs)
}

/cc @matloob @heschik

@gopherbot gopherbot added this to the Unreleased milestone Apr 14, 2020
@matloob
Copy link
Contributor

@matloob matloob commented Apr 14, 2020

cc @stamblerre

Errors are associated with positions in a file, and if there's no position, the error is printed with a -. See the documentation of error here. https://pkg.go.dev/golang.org/x/tools/go/packages?tab=doc#Error

The error's already associated with a package, so the error doesn't print its own package to avoid redundancy.

@mdempsky
Copy link
Member Author

@mdempsky mdempsky commented Apr 14, 2020

Can we make "go build" follow the same convention then? I.e., make it print "-:" instead of "package nonexist:"?

@matloob
Copy link
Contributor

@matloob matloob commented Apr 14, 2020

@jayconrod @bcmills

I don't know if that's the right thing to do here: the closer analogue to what go packages does is go list -e -json: and with those you get a very similar result:

$ go list -e -json nonexist
{
	"ImportPath": "nonexist",
	"Match": [
		"nonexist"
	],
	"Incomplete": true,
	"Error": {
		"ImportStack": [
			"nonexist"
		],
		"Pos": "",
		"Err": "package nonexist is not in GOROOT (/usr/local/go/src/nonexist)"
	}
}
@mdempsky
Copy link
Member Author

@mdempsky mdempsky commented Apr 14, 2020

Looks like cmd/go's "package nonexist:" output comes from:

return "package " + strings.Join(p.ImportStack, "\n\timports ") + optpos + ": " + p.Err.Error()

@bcmills
Copy link
Member

@bcmills bcmills commented Apr 15, 2020

I don't think -: is a particularly helpful prefix. (Helpful to tools, perhaps, but confusing to humans.)

That said, package nonexist: cannot find package "nonexist" is unpleasantly redundant too. This failure message is a lot smoother in module mode than in GOPATH mode:

example.com$ GO111MODULE=on go build nonexist
package nonexist is not in GOROOT (/usr/local/google/home/bcmills/sdk/gotip/src/nonexist)

example.com$ GO111MODULE=off go build nonexist
package nonexist: cannot find package "nonexist" in any of:
        /usr/local/google/home/bcmills/sdk/gotip/src/nonexist (from $GOROOT)
        /tmp/tmp.00bJBNCJEL/_gopath/src/nonexist (from $GOPATH)

If someone wants to fix the GOPATH-mode error message to match the module-mode one, that seems like it would be an improvement, but unless we see something similar in module mode I don't think it's worth spending much time on.

@matloob
Copy link
Contributor

@matloob matloob commented Apr 15, 2020

On the go/packages side, one thing we can do is just drop the "-:" on the error's String method. That might be nicer for users if the package?

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
5 participants