You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Unable to edit document because the file was not previously opened" when using "dart.onlyAnalyzeProjectsWithOpenFiles":true (and plugins) #4862
Splitting this from #4740 since it's not the same as the original issue and I'm not yet certain that it's plugin-related at all.
To reproduce:
Create a simple empty project that references an analyzer plugin (like custom_lint)
I'm not sure the plugin is relevant yet, but it seems to repro pretty reliably with and I couldn't repro without
Ensure "dart.onlyAnalyzeProjectsWithOpenFiles":true is set for the workspace and that no files are open
Run the Developer: Reload Window command to get a clean analyzer with no open files
Open analysis_options.yaml and press Save
Close analysis_options.yaml
Modifying analysis_options may or may not be relevant
Open a .dart file
Make some changes to the file
This open results in an error like:
Opening the file triggers rebuilding of analysis roots, but it also seems to remove the overlay:
1701185971585:Req:{"jsonrpc"::"2.0","method"::"textDocument/didOpen","params"::{"textDocument"::{"uri"::"file::///C%3A/Dev/Test%20Projects/dartcode_4740_repro/lib/dartcode_4740_repro.dart","languageId"::"dart","version"::1,"text"::"int calculate() {\n return 6 * 7;\n}\n"}},"clientRequestTime"::1701185971583}
1701185971585:Info:Setting overlay for C::\Dev\Test Projects\dartcode_4740_repro\lib\dartcode_4740_repro.dart
1701185971587:Info:Setting roots
1701185971588:Info:Done setting roots
1701185971588:Info:Done setting roots
1701185971588:Info:Removing overlay for C::\Dev\Test Projects\dartcode_4740_repro\lib\dartcode_4740_repro.dart
1701185972690:Req:{"jsonrpc"::"2.0","method"::"textDocument/didChange","params"::{"textDocument"::{"uri"::"file::///C%3A/Dev/Test%20Projects/dartcode_4740_repro/lib/dartcode_4740_repro.dart","version"::2},"contentChanges"::[{"range"::{"start"::{"line"::3,"character"::0},"end"::{"line"::3,"character"::0}},"rangeLength"::0,"text"::"d"}]},"clientRequestTime"::1701185972204}
1701185972690:Res:{"jsonrpc"::"2.0","method"::"window/showMessage","params"::{"message"::"Unable to edit document because the file was not previously opened:: C::\\Dev\\Test Projects\\dartcode_4740_repro\\lib\\dartcode_4740_repro.dart","type"::1}}
1701185972690:Res:{"jsonrpc"::"2.0","method"::"window/logMessage","params"::{"message"::"An unrecoverable error occurred.\n\nUnable to edit document because the file was not previously opened:: C::\\Dev\\Test Projects\\dartcode_4740_repro\\lib\\dartcode_4740_repro.dart\n\n-32099\n\nnull","type"::1}}
We always verify we have an overlay before processing a textDocument/didChange event so this results in us throwing.
This looks like a race condition and it may be caused by plugins.
When there are changes to plugins, we end up re-computing our capabilities and call client/unregisterCapability and client/registerCapability. It appears that this causes VS Code to tell us that the open files have been closed and then re-opened:
The issue is a bug in handling of closed files. There is an await that meant if a client closed and re-opened a file very close together, the close could be processed after the open, leaving the file in a "closed" state (which produces this error on the next change).
It isn't directly related to plugins or onlyAnalyzeProjectsWithOpenFiles, however using plugins can result in us re-registering capabilities with the client when analysis roots change (which results in VS Code's LSP client closing and re-opening all files since those events are tied to the capabilities) and using onlyAnalyzeProjectsWithOpenFiles causes additional work when files are opening/closing which makes it more likely for the bad await to cause the work to be re-ordered.
I have a fix open at https://dart-review.googlesource.com/c/sdk/+/338660 but since it'll only ship in a future SDK release my recommendation is to disable onlyAnalyzeProjectsWithOpenFiles as a workaround (I'd generally recommend not using that at all, but if you do feel you need it for some projects, set it only on those projects rather than in your global user settings).
Activity
DanTup commentedon Nov 28, 2023
This looks like a race condition and it may be caused by plugins.
When there are changes to plugins, we end up re-computing our capabilities and call
client/unregisterCapabilityandclient/registerCapability. It appears that this causes VS Code to tell us that the open files have been closed and then re-opened:But it appears that we process the close/open in the wrong order, so we first update the overlay and then remove it:
So it appears that there is a bug in the processing of closed files - they must be applied immediately and synchronously.
DanTup commentedon Nov 28, 2023
The issue is a bug in handling of closed files. There is an
awaitthat meant if a client closed and re-opened a file very close together, the close could be processed after the open, leaving the file in a "closed" state (which produces this error on the next change).It isn't directly related to plugins or
onlyAnalyzeProjectsWithOpenFiles, however using plugins can result in us re-registering capabilities with the client when analysis roots change (which results in VS Code's LSP client closing and re-opening all files since those events are tied to the capabilities) and usingonlyAnalyzeProjectsWithOpenFilescauses additional work when files are opening/closing which makes it more likely for the badawaitto cause the work to be re-ordered.I have a fix open at https://dart-review.googlesource.com/c/sdk/+/338660 but since it'll only ship in a future SDK release my recommendation is to disable
onlyAnalyzeProjectsWithOpenFilesas a workaround (I'd generally recommend not using that at all, but if you do feel you need it for some projects, set it only on those projects rather than in your global user settings).[analysis_server] Ensure textDocument/didClose removes overlays synch…
DanTup commentedon Nov 28, 2023
Fixed by dart-lang/sdk@4f0dc3b