golang / go

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

x/tools/gopls: weird files treated as packages #54815

Open pjweinb opened 2 years ago

pjweinb commented 2 years ago

If one edits a random text file, say /tmp/foof, go/packages/golist.go treats it as a Go file. The comment at line 349 is "// If we get nothing back from go list, // try to make this file into its own ad-hoc package. // TODO(rstambler): Should this check against the original response?"

One would think it should look to see if there is a .go file involved somehow. Otherwise the gopls output is polluted by a lot of messages like

[Error - 10:22:27 AM] 2022/09/01 10:22:27 analyzer "tests" failed: analysis tests for package /tmp/foof panicked: runtime error: invalid memory address or nil pointer dereference

adonovan commented 8 months ago

Do you remember anything about how this happened? Typical LSP clients like VS Code and Emacs+eglot won't consider an arbitrary text file as a Go file unless it has a .go suffix, so gopls isn't involved. Did you force the interpretation to Go using the menu at the lower right of VS Code? I tried this, and gopls became usable within the Go file.