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

Test explorer: leaves open files in the LSP session following on-disk changes #2570

Closed
findleyr opened this issue Dec 13, 2022 · 7 comments
Closed
Labels
FrozenDueToAge go-test issues related to go test support (test output, test explorer, ...) NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@findleyr
Copy link
Contributor

Just observed this behavior very reliably when testing branch switches in vscode-go:

When I switch branches, vscode-go sends a lot of didOpen notifications to gopls for test files (_test.go). This can lead to spurious "no packages for open file foo_test.go" diagnostics from gopls. Worse, switching branches again leaves the open file state, which can lead to type-checking errors as the open contents no longer compile.

Setting "go.testExplorer.enable": false reliably avoids these problems.

@gopherbot gopherbot added this to the Untriaged milestone Dec 13, 2022
@findleyr findleyr added the NeedsFix The path to resolution is known, but the work has not been done. label Dec 14, 2022
@findleyr findleyr modified the milestones: Untriaged, vscode-go/later Dec 14, 2022
@hyangah
Copy link
Contributor

hyangah commented Dec 15, 2022

I think this is one of the problematic logic -

await this.documentUpdate(await this.workspace.openTextDocument(file));

When switching branches and noticing new files, vscode fires onDidCreateFile events.
openTextDocument loads the file in the buffer which isn't necessarily visible to users.
We shouldn't openTextDocument just because file creation events were detected.

I also observed that when branch switch causes folder move or deletion, the tests under deleted folder should be removed but don't since currently the code relies on onDidDeleteFile events to remove the corresponding test items from the tree, but vscode does not always trigger deletion events when folder deletion occurs outside of vscode (e.g. git change or rm from terminal).

@hyangah hyangah added the go-test issues related to go test support (test output, test explorer, ...) label Dec 15, 2022
@suzmue
Copy link
Contributor

suzmue commented Dec 15, 2022

Relevant upstream discussion about extensions wanting to get file contents without sending didOpen notifications to language servers: microsoft/vscode#15723. The conclusion from that thread was that the API is fine as is and this is just the way the world works.

The interesting thing is that we don't get see the 'didOpen' notifications in the language server for non-test files even though we called openTextDocument on those as well. Maybe its because we aren't holding on to them for long enough or it may be some way that we use the TextDocument later in the code.

Edit: Right, never mind. The reason we are only seeing the didOpen is because the TestExplorer is only watching for test files.

@findleyr
Copy link
Contributor Author

I think it's still a bug that the files remain open after being read, causing subsequent errors when switching branches.

Furthermore, a didOpen is not a no-op for gopls: the set of open files is significant to gopls (for example, to warn about orphaned files). If we could avoid the didOpen notifications altogether, that would be ideal.

@hyangah
Copy link
Contributor

hyangah commented Dec 16, 2022

We don't need to read the file contents at all until the file is actually open by the user and vscode fires a didOpen notification.

When initializing the tests explorer runs documentUpdate code only for

  • files that are already open (which is ok)
  • the user expanded the file item (which will still openTextDocument which isn't ideal but damage is more contained).

Making the onDidCreateFile handler not attempt to inspect the file content is consistent with the behavior and I think that can be a short term solution to improve the stability.

It looks like Test explorer needs the contents because it needs to call DocumentSymbol provider which is still partly implemented on the extension side and run the code that filters/processes document symbol results

private async processSymbol(
to support testify and other popular test utilities, etc. This is actually the same code used to place the test code lenses in vscode-go.

As a long term solution, I think we need this test function identification logic to be implemented inside gopls properly.

@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/458999 mentions this issue: src/goTest: do not open text document on didCreateFile

@hyangah hyangah modified the milestones: vscode-go/later, v0.37.1 Jan 17, 2023
@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/462376 mentions this issue: src/goTest: do not open text document on didCreateFile

@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/462376 mentions this issue: [release] src/goTest: do not open text document on didCreateFile

gopherbot pushed a commit that referenced this issue Jan 17, 2023
The Test Explorer watches all _test.go files in the workspace in
order to keep the list of tests updated in the test explorer ui.
On branch switches, there may be many test files that are created,
causing the test explorer to process these. Previously, part of the
processing resulted in opening the file. This led to issues due to
the interaction with the language server. This change updates the
logic on file creation, to add the parent test item only, which will
not require opening the document.

Fixes #2570

Change-Id: I595f34daee257c85bafd3e5706176f73f891fdf1
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/458999
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/462376
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Robert Findley <rfindley@google.com>
@golang golang locked and limited conversation to collaborators Feb 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge go-test issues related to go test support (test output, test explorer, ...) NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants
@hyangah @suzmue @gopherbot @findleyr and others