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/cmd/gopls: CodeAction fails with "no file information for file" #31307

Closed
myitcv opened this issue Apr 6, 2019 · 8 comments
Closed

x/tools/cmd/gopls: CodeAction fails with "no file information for file" #31307

myitcv opened this issue Apr 6, 2019 · 8 comments
Assignees
Milestone

Comments

@myitcv
Copy link
Member

@myitcv myitcv commented Apr 6, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.2 linux/amd64
$ go list -m golang.org/x/tools
golang.org/x/tools v0.0.0-20190406165526-0d5674b3111a

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN="/home/myitcv/gostuff/src/github.com/myitcv/govim/cmd/govim/.bin"
GOCACHE="/home/myitcv/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/myitcv/gostuff"
GOPROXY=""
GORACE=""
GOROOT="/home/myitcv/gos"
GOTMPDIR=""
GOTOOLDIR="/home/myitcv/gos/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/myitcv/gostuff/src/github.com/myitcv/govim/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build226898939=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Vim has the concept of a buffer. A buffer is the in-memory text of a file. Therefore it is possible in Vim to create a new buffer for a Go file that does not exist on disk (yet).

Consider the following govim testscript in which we create a new Go buffer, populate it with some contents, then attempt to save the file. The "pre" action on saving the file is to call CodeAction to effectively run goimports on the code pre-save.

# Test completions for a file that does not yet exist on disk

vim ex 'e main.go'
vim ex 'r main.txt | 0d_'
vim ex 'w main.go'
cmp main.go main.txt

-- go.mod --
module mod.com

-- main.txt --
package main

func main() {
}
-- main.go.golden --
package main

import "os/exec"

func main() {
}

For every buffer in govim, textDocument/didOpen is called followed by textDocument/didChange, so at the point of attempting to write the file gopls will "know" exactly the contents that will be written to disk.

What did you expect to see?

This to succeed.

What did you see instead?

An error returned from gopls:

no file information for file://$WORK/main.go

cc @stamblerre @ianthehat

@myitcv
Copy link
Member Author

@myitcv myitcv commented Apr 6, 2019

I should probably note I see the same error if I ask for completions etc

@stamblerre stamblerre self-assigned this Apr 7, 2019
@stamblerre stamblerre added the gopls label Apr 7, 2019
@pwaller
Copy link
Contributor

@pwaller pwaller commented Apr 10, 2019

I'm getting this on vscode, trying gopls for the first time on a new basically empty project. go build works fine. No interesting diagnostics.

[Error - 21:56:31] Request textDocument/codeAction failed.
  Message: no file information for file:///home/pwaller/.local/src/github.com/pwaller/myproject/main.go
  Code: 0 
@pwaller
Copy link
Contributor

@pwaller pwaller commented Apr 10, 2019

OK, wondering if it could be to do with where I opened up vscode - the project I opened with wasn't in the same folder as the module; closing the window and reopening in the same directory as the module seems to be the workaround.

This failure mode might be unrelated to @myitcv's problem, if so apologies for the noise.

@ianthehat
Copy link

@ianthehat ianthehat commented Apr 11, 2019

As far as the original bug is concerned, we rely on go/packages underneath, and from the docs:

// Overlays provide incomplete support for when a given file doesn't
// already exist on disk.

specifically I think we do nothing at all with files that are part of a package that go list cannot yet see.
In theory, this can be improved by adding more support to go/packages.

@myitcv
Copy link
Member Author

@myitcv myitcv commented Apr 11, 2019

Ah yes I'd forgotten that nuance @ianthehat.

How should an editor therefore go about adding a new file?

Because immediately prior to creating the file, by definition it does not exist on disk. It will then be created but quite possibly without a package clause. Then the package will be added etc...

What sequence of calls/events is gopls in this scenario? Because even if I modify the above test to touch the file prior to opening it, gopls still reports that it knows nothing about the file.

@doxxx
Copy link

@doxxx doxxx commented Apr 11, 2019

I have a similar problem on Windows. I created a go module (go mod init ...) in a subfolder of my main (non-Go) project and only when I open VS Code specifically on that folder do I get gopls features working. Otherwise, with the main project's root folder open in VS Code, when I open a .go file from the subfolder, I get no file information errors.

@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Apr 11, 2019

@doxxx: your issue sounds more like #29174. You will need to manually add your folder to the VSCode workspace to get gopls to work in those cases.

@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Apr 17, 2019

Closing in favor of #31522

@stamblerre stamblerre closed this Apr 17, 2019
@golang golang locked and limited conversation to collaborators Apr 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.