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
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@stamblerre
Copy link
Contributor

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 This label describes issues relating to any tools in the x/tools repository. label May 29, 2020
@gopherbot gopherbot added this to the Unreleased milestone May 29, 2020
@gopherbot
Copy link

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

@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

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 2, 2020
@heschi
Copy link
Contributor

heschi 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 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

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

@golang golang locked and limited conversation to collaborators Jun 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants