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

stamblerre opened this issue May 29, 2020 · 5 comments

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

stamblerre opened this issue May 29, 2020 · 5 comments


Copy link

@stamblerre stamblerre commented May 29, 2020

The repro case for this issue can be found in a gopls regression test in

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
Copy link

@gopherbot gopherbot commented May 29, 2020

Change 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
Run-TryBot: Rebecca Stambler <>
TryBot-Result: Gobot Gobot <>
Reviewed-by: Heschi Kreinick <>
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

Copy link

@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.

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.

Copy link

@gopherbot gopherbot commented Jun 12, 2020

Change 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
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.