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

vscode.workspace.onDidCloseTextDocument callback not called #30820

Closed
rafaelmaiolla opened this issue Jul 16, 2017 · 16 comments
Closed

vscode.workspace.onDidCloseTextDocument callback not called #30820

rafaelmaiolla opened this issue Jul 16, 2017 · 16 comments
Assignees
Labels
info-needed Issue requires more information from poster
Milestone

Comments

@rafaelmaiolla
Copy link

rafaelmaiolla commented Jul 16, 2017

In my extension, I'm opening a local file using vscode.workspace.openTextDocument and then vscode.window.showTextDocument. And it seems to be working fine.

I'm also listening for events using vscode.workspace.onDidOpenTextDocument, vscode.workspace.onDidSaveTextDocument and vscode.workspace.onDidCloseTextDocument.

Both vscode.workspace.onDidOpenTextDocument and vscode.workspace.onDidSaveTextDocument callbacks are called as expected. However
vscode.workspace.onDidCloseTextDocument callback is not called when I close the file in the UI.

Am I missing anything?

  • VSCode Version: 1.14.1
  • OS Version: Windows 10 Version 1703

Steps to Reproduce:

  1. Open the file using vscode.workspace.openTextDocument and then vscode.window.showTextDocument;
  2. Listen for events in vscode.workspace.onDidCloseTextDocument.
  3. Close the file in the editor;
  4. Check that vscode.workspace.onDidCloseTextDocument callback is not called as expected.

Reproduces without extensions: It is the extension code that is not working.

@rafaelmaiolla rafaelmaiolla changed the title onDidCloseTextDocument not called vscode.workspace.onDidCloseTextDocument callback not called Jul 16, 2017
@jrieken jrieken assigned bpasero and unassigned jrieken Jul 17, 2017
@jrieken jrieken removed the api label Jul 17, 2017
@jrieken
Copy link
Member

jrieken commented Jul 17, 2017

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.

@bpasero bpasero added this to the Backlog milestone Jul 17, 2017
@rafaelmaiolla
Copy link
Author

So, what you are saying is that vscode.workspace.onDidCloseTextDocument callback is not guaranteed to be called?

Why does it work as expected if I open the file using the menu options File > Open File?

@rafaelmaiolla
Copy link
Author

It seems that something has changed in version 1.13.1.

I tested in version 1.12.2 and everything was working fine.
I tested again in version 1.13.1 and it was not working as expected.

@bpasero
Copy link
Member

bpasero commented Sep 28, 2017

@rafaelmaiolla is this a untitled file that you close after saving? I cannot reproduce with a normal file.

@bpasero bpasero added the info-needed Issue requires more information from poster label Sep 28, 2017
@rafaelmaiolla
Copy link
Author

@bpasero I will reproduce the issue again and add the details here.

@rafaelmaiolla
Copy link
Author

@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 openInEditor and handleChanges.

But basically, the code is doing the following steps:

  1. Receive a file through a socket connection;
  2. Save it in the temp folder;
  3. Open that file using vscode.workspace.openTextDocument and vscode.window.showTextDocument;
  4. Listen for the events Save, Close and Open;

What I could see is that the Save and Open events are working as expected. However, the Close is not working as expected.

It seems that the event is triggered after some time, but I can't say it exactly.

@bpasero
Copy link
Member

bpasero commented Sep 30, 2017

@rafaelmaiolla I would prefer a simple sample to verify this, here is how I try to reproduce (and cannot).

  • generate a hello world typescript extension via (npm install -g generator-code and yo code)
  • paste the following code into extension.ts:
'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() {
}
  • run "Hello World" command
  • close the file

=> events are fired

@mprobst
Copy link

mprobst commented Oct 6, 2017

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 (textDocument !== closedTextDocument).

The text document the extension keeps a reference to has the file name "/var/folders/9h/hrqcx5mn6xx8pr33qwddwxmw003zyt/T/cgDcg0Qm3i/foo.txt". The text document that is closed has the file name "/var/folders/9h/hrqcx5mn6xx8pr33qwddwxmw003zyt/T/8IuAZeqcfX/foo.txt.git".

Maybe some git plugin is interfering here?

mprobst added a commit to mprobst/remote-vscode that referenced this issue Oct 6, 2017
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.
@rafaelmaiolla
Copy link
Author

rafaelmaiolla commented Oct 6, 2017

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:

  • Why the open and save events are fired as expected and just close is in a new document?
  • This was working fine in 1.12.2 and stopped working in 1.13.1. Is there any change between these two versions that could be affecting this?

@mprobst
Copy link

mprobst commented Oct 6, 2017

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

@bpasero
Copy link
Member

bpasero commented Oct 10, 2017

@rafaelmaiolla reproducible steps for what you see? Are you saving a new untitled file? If so, that behaviour is expected.

@mprobst
Copy link

mprobst commented Oct 10, 2017

@bpasero my repro is sadly a bit involved (check out the extension, debug it, set breakpoint at the line I'm referencing, run an rmate command to trigger behaviour). The file that's being closed is an existing file on disk, in the temp folder.

@rafaelmaiolla
Copy link
Author

@bpasero, the file is saved to disk before opening in vscode.

What do you mean by untitled file?

@bpasero
Copy link
Member

bpasero commented Oct 11, 2017

@rafaelmaiolla is it an existing file that is saved or a new file

@vscodebot vscodebot bot closed this as completed Oct 18, 2017
@vscodebot
Copy link

vscodebot bot commented Oct 18, 2017

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.

@mprobst
Copy link

mprobst commented Oct 18, 2017

The file is an existing file.

@vscodebot vscodebot bot locked and limited conversation to collaborators Dec 2, 2017
dev-signageOS pushed a commit to signageos/vscode-sops that referenced this issue Oct 16, 2020
… 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

5 participants