golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.01k stars 17.68k forks source link

cmd/go: confusing error for non-existent go source files #48559

Open rsc opened 3 years ago

rsc commented 3 years ago
% go build notexist.go
cannot find package "." in:
    /Users/rsc/go/src/notexist.go
% 

For what it's worth, I'm not in /Users/rsc/go/src. That's just the error looking for a std package named notexist.go. It should show me an error more like 'no such file notexist.go'.

/cc @bcmills @jayconrod @matloob

heschi commented 3 years ago

Ping from the release team: any updates on this release blocker?

jayconrod commented 3 years ago

Briefly looked into this, but wasn't able to reproduce it. Outside a module in 1.17.1, I see this:

$ go build notexist.go
no required module provides package notexist.go: go.mod file not found in current directory or any parent directory; see 'go help modules'

Inside a module:

$ go build notexist.go
no required module provides package notexist.go; to add it:
        go get notexist.go

What version of Go was this? What does go env show?

ianlancetaylor commented 2 years ago

@bcmills This is in the 1.18 milestone; time to move to 1.19? Thanks.

(The current error isn't great since it treats nonexist.go as the name of a package. That is technically correct but maybe if it doesn't exist we can treat a trailing ".go" as a special case and give a different error message.)

ianlancetaylor commented 2 years ago

Whoops, sorry, didn't mean to close this.

bcmills commented 2 years ago

This was improved somewhat by the fix for #48907, but it's still not ideal.

bcmills commented 2 years ago

This also turns out to be another in the cluster of go list -e error-reporting bugs. There are a number of base.Fatalf calls in load.GoFilesPackage that could just as easily be returned as errors.

gopherbot commented 2 years ago

Change https://go.dev/cl/386496 mentions this issue: cmd/go: improve error handling for nonexistent files