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/internal/imports: fails with empty file on disk #39315

Closed
stamblerre opened this issue May 29, 2020 · 5 comments
Closed

x/tools/internal/imports: fails with empty file on disk #39315

stamblerre opened this issue May 29, 2020 · 5 comments

Comments

@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented May 29, 2020

The repro case for this issue can be found in a gopls regression test in https://golang.org/cl/235581.

I have noticed that when the file is empty on disk, but the source provided includes content with a missing import, the goimports behavior will not succeed. Similarly, if the file has content on disk, but is empty in the overlay, an import will be added (this looks very bizarre in the editor).

/cc @heschik

@gopherbot gopherbot added the Tools label May 29, 2020
@gopherbot gopherbot added this to the Unreleased milestone May 29, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented May 29, 2020

Change https://golang.org/cl/235581 mentions this issue: internal/lsp/regtest: add a test that reproduces golang/go#38878

gopherbot pushed a commit to golang/tools that referenced this issue Jun 1, 2020
This test partially reproduces some strange behavior with creating
new tests files. In particular, it creates a new x test in a package
that already has a test variant and adds content with a missing import.

In the test, the import is never added. However, in my own experience
debugging this in VS Code, I see the import get added but the diagnostic
never get removed. One thing at a time though...

Updates golang/go#39315

Change-Id: I724a145688b915d04abd1f21efc6f9a7506be043
Reviewed-on: https://go-review.googlesource.com/c/tools/+/235581
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
@stamblerre
Copy link
Contributor Author

@stamblerre stamblerre commented Jun 2, 2020

@heschik noted that the regression test doesn't appear to run any code actions. However, the log does contain code actions, just slightly out-of-order:

[Trace - 00:20:09.201 AM] Sending request 'textDocument/codeAction - (2)'.
Params: {"textDocument":{"uri":"file:///var/folders/c5/cdxx0hzn6v3cvfn42kwt2tj000crcx/T/goplstest-sandbox-regtest-012468612/work/hello/hello_x_test.go"},"range":{"start":{"line":0,"character":0},"end":{"line":0,"character":0}},"context":{"diagnostics":null,"only":["source.organizeImports"]}}


[Trace - 00:20:09.242 AM] Received response 'textDocument/codeAction - (2)' in 41ms.
Result: null


[Trace - 00:20:09.243 AM] Sending request 'textDocument/formatting - (3)'.
Params: {"textDocument":{"uri":"file:///var/folders/c5/cdxx0hzn6v3cvfn42kwt2tj000crcx/T/goplstest-sandbox-regtest-012468612/work/hello/hello_x_test.go"},"options":{"tabSize":0,"insertSpaces":false}}


[Trace - 00:20:09.243 AM] Received response 'textDocument/formatting - (3)' in 0ms.
Result: []


[Trace - 00:20:09.243 AM] Sending notification 'textDocument/didChange'.
Params: {"textDocument":{"version":3,"uri":"file:///var/folders/c5/cdxx0hzn6v3cvfn42kwt2tj000crcx/T/goplstest-sandbox-regtest-012468612/work/hello/hello_x_test.go"},"contentChanges":[{"text":"package hello_test\n\nimport (\n\t\"testing\"\n)\n\nfunc TestHello(t *testing.T) {\n\thello.Hello()\n}\n"}]}


[Trace - 00:20:09.243 AM] Sending notification 'textDocument/willSave'.
Params: {"textDocument":{"uri":"file:///var/folders/c5/cdxx0hzn6v3cvfn42kwt2tj000crcx/T/goplstest-sandbox-regtest-012468612/work/hello/hello_x_test.go"},"reason":1}


[Trace - 00:20:09.244 AM] Sending notification 'textDocument/didSave'.
Params: {"textDocument":{"version":3,"uri":"file:///var/folders/c5/cdxx0hzn6v3cvfn42kwt2tj000crcx/T/goplstest-sandbox-regtest-012468612/work/hello/hello_x_test.go"}}

The textDocument/didChange appears to have happened after the code actions, even though they should be triggered as part of the save, which happens after. I checked the file content that gopls sees, and it is the correct content, so I think it's only the log that has the incorrect ordering.

@heschik, do you see this behavior?

/cc @findleyr

@heschik
Copy link
Contributor

@heschik heschik commented Jun 12, 2020

Boy did I overthink this. goimports gives up on a directory if there's a parse error, and your empty file with no package statement causes a parse error.

Easy fix if you care.

@stamblerre
Copy link
Contributor Author

@stamblerre stamblerre commented Jun 12, 2020

I do, because if someone creates a new file and doesn't save it for whatever reason, they won't get goimports till it's saved. Plus I think it could play poorly with our package reloading and could lead to some bad state. I can make the fix if you like.

@gopherbot
Copy link

@gopherbot gopherbot commented Jun 12, 2020

Change https://golang.org/cl/237686 mentions this issue: internal/imports: continue past parse errors

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
4 participants
You can’t perform that action at this time.