-
Notifications
You must be signed in to change notification settings - Fork 29.5k
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
vscode.workspace.onDidCloseTextDocument callback not called #30820
Comments
The API just reflect what the workbench does. That might not be what you expect but in general it's up to the editor to decide when to close a document. When that happens the event will fire. |
So, what you are saying is that Why does it work as expected if I open the file using the menu options File > Open File? |
It seems that something has changed in version 1.13.1. I tested in version 1.12.2 and everything was working fine. |
@rafaelmaiolla is this a untitled file that you close after saving? I cannot reproduce with a normal file. |
@bpasero I will reproduce the issue again and add the details here. |
@bpasero, I can still reproduce the issue in the last VSCode version. You can check the code from my extension here. You should check the methods But basically, the code is doing the following steps:
What I could see is that the It seems that the event is triggered after some time, but I can't say it exactly. |
@rafaelmaiolla I would prefer a simple sample to verify this, here is how I try to reproduce (and cannot).
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
vscode.workspace.onDidOpenTextDocument(() => {
console.log("onDidOpenTextDocument");
});
vscode.workspace.onDidSaveTextDocument(() => {
console.log("onDidSaveTextDocument");
});
vscode.workspace.onDidCloseTextDocument(() => {
console.log("onDidCloseTextDocument");
});
let disposable = vscode.commands.registerCommand('extension.sayHello', () => {
vscode.workspace.openTextDocument(__filename).then(doc => {
vscode.window.showTextDocument(doc).then(() => {
console.log("Showing text document");
});
});
});
context.subscriptions.push(disposable);
}
export function deactivate() {
}
=> events are fired |
I've debugged this a bit (I'm trying to use the extension). I do see the onDidCloseTextDocument fire, so that might not be the problem. @rafaelmaiolla's extension doesn't seem to work because it keeps a reference to the text document it opened. When the close notification happens, it happens with a different text document ( The text document the extension keeps a reference to has the file name Maybe some git plugin is interfering here? |
Sometimes onDidCloseTextDocument seems to get called with a different document (something involving git?). This works around the problem by also watching the list of visible editors, and closing the session. Works around issue rafaelmaiolla#26, related to upstream issue microsoft/vscode#30820.
I will try to reproduce what you are saying in my environment and also check your workaround, thanks for that. But I have a few questions:
|
@rafaelmaiolla I'm just some random dude trying to use your extension (which is nice, thanks for it!), I have no deeper insight into what's going on here. The current behaviour with changing the document identity (and its filename) does seem like a bug to me. |
@rafaelmaiolla reproducible steps for what you see? Are you saving a new untitled file? If so, that behaviour is expected. |
@bpasero my repro is sadly a bit involved (check out the extension, debug it, set breakpoint at the line I'm referencing, run an |
@bpasero, the file is saved to disk before opening in vscode. What do you mean by untitled file? |
@rafaelmaiolla is it an existing file that is saved or a new file |
This issue has been closed automatically because it needs more information and has not had recent activity. Please refer to our guidelines for filing issues. Thank you for your contributions. |
The file is an existing file. |
… event instead * because you cannot be sure the open text document event is emitted by vscode * vscode sometimes keeps file open in background and event is not emitted * the same is for close file Fixes microsoft/vscode#30820
In my extension, I'm opening a local file using
vscode.workspace.openTextDocument
and thenvscode.window.showTextDocument
. And it seems to be working fine.I'm also listening for events using
vscode.workspace.onDidOpenTextDocument
,vscode.workspace.onDidSaveTextDocument
andvscode.workspace.onDidCloseTextDocument
.Both
vscode.workspace.onDidOpenTextDocument
andvscode.workspace.onDidSaveTextDocument
callbacks are called as expected. Howevervscode.workspace.onDidCloseTextDocument
callback is not called when I close the file in the UI.Am I missing anything?
Steps to Reproduce:
vscode.workspace.openTextDocument
and thenvscode.window.showTextDocument
;vscode.workspace.onDidCloseTextDocument
.vscode.workspace.onDidCloseTextDocument
callback is not called as expected.Reproduces without extensions: It is the extension code that is not working.
The text was updated successfully, but these errors were encountered: