Callback function of vscode.workspace.onDidCloseTextDocument is working at development but not after packaging to vsix - visual-studio-code

In my extension, I'm opening a local file using vscode.workspace.openTextDocument and then vscode.window.showTextDocument and vscode.workspace.onDidCloseTextDocument to close the file.
This seems to be working fine if run the extension using the code.
Now I have created the vsix file using vsce package. Here comes the tricky thing
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.(I am facing this issue when the extension is installed.)
Am I missing anything?
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.
Code is present in this git repo https://github.com/akhilravuri1/hellovscode, please clone the code and create vsix package by typing vsce package command in the cmd. Install the extension using vsix file and select the hello world command then it will open a file but when you close it just closes but I expect a select option with YES or NO at command palette.
Thanks in Advance.

I tried your extension.
I get the OnClose-event when the Welcome Screen text editor closes and ddl.txt is loaded.
The docs say you should use the vscode.window on-events but they do not allow to monitor a document close.
Very strange that the OnClose-event is fired on language change (this happens). You can't tell if it is a language change or document close.
When I open the ddl.txt with File > Open and close it I get the OnClose-event.
I get the OnClose-event on all files opened with File > Open.
It looks like files opened with vscode.workspace.openTextDocument and vscode.window.showTextDocument do not fire an OnClose-event.
You should file a bug because these calls generate a normal TextDocument.

Related

Renaming files doesn't take effect in VScode while using WSL2

I have a very annoying problem in my VScode setup.
I'm using WSL as a terminal to work on my projects and occasionally, mistype the name of one of the folder or file that I'm working with.
For example:
I accidentally created the Mainheader.js file in layout folder (without the capitalized L) therefore, I decided to rename the folder with a capital L.
Now on my React app, any changes made to MainHeader.js file will not be reflected. I did update the related import.
So I decided to delete the entire folder and recreate it with MainHeader.js but this is what I end up with.
The file is there in my folder but when I click on it, I get
"Unable to open Mainheader.js - File not found"
So I try to create it then I get this error:
Unable to create file 'wsl\path]to\MainHeader.js' that already exists when overwrite flag is not set
Has anyone run into this type of behavior in WSL before? It's quite annoying because the only workaround I've found so far is to create a completely different folder with a different name...
Any help would be appreciated. I can't really work like this.
I've been having the same problem for the past 2 days. I presume its a permissions issue, but unfortunately I don't know how to permanently fix it.
I did find this work-around though:
Open a new VS Code window. (I'd recommend closing any VS Code window that had
your project directory open.)
Create a duplicate or copy the contents of the problem file so you don't lose
your code.
Delete the problem file.
Now create the file again using VS Code. Go to File > New Text File. Next paste
in your code.
Now save your new file. Go to File > Save As and save your new file with at the same path + filename + extension that was giving you problems previously. VS Code should allow you to save the new file without any issue.
Now you can open this new VS Code window to your project directory and you should be able to continuing accessing the file that was a problem before.
Basically we just deleted the problem file and then created it again from scratch in a new VS Code window.
Hope this work-around works for you!
EDIT 09/20/2022
Following Baza86's answer here solved the issue for me. Seems like it was a permissions issue of sorts, but if you use the Remote-WSL extension VS code can directly access the linux filesystem.
How to run VScode in sudo mode in WSL2?
You may need add the case option to you options in the wsl config. The default is set to off, however you can set this to off, dir or force.
Open your wsl.conf using sudo with any text editor while running window subsystem Linux. The config file resides in /etc/wsl.conf. This file is used to configure settings per-distribution for Linux distros running on WSL 1 or WSL 2.
My default config looked like this yours may be different:
[automount]
options = "metadata"
add
[automount]
options = "metadata,case=dir"
Here is the official Microsoft docs for Advanced settings configuration in WSL - https://learn.microsoft.com/en-us/windows/wsl/wsl-config

Run specific file always vscode-code-runner

Is there any setting for vscode-code-runner to setup a file that would be always run when I click on run button regardless which file is open in editor currently.
as of writing this comment i do not believe it's possible with this extension. The next best thing i can recommend is to execute the file you want to run with coderunner, then copy the command that it generated and executed. You can then paste this command in your setting.json file as a custom command like so
{
"code-runner.customCommand": "<created command>",
}
To execute the custom command from anywhere in your workspace, do ctrl+alt+K
You can also create a launch.settings file to run and debug your code from the debug tab which you can then execute with ctrl+shift+D
it's not as slick as a nice run button but it's better than nothing

How to change folder that opened by default in VSCode?

I have small problem with VSCode folder, that opened by default.
Problem description: I start new instance of VSCode (trough File->New Window), and then if I choose File->Open Folder it opens dialog with my Windows user folder as starting point (C:\Users\MyUser)
Question: How can I change that folder in settings (if it possible)? So by default it will show as start point for example D:\development\ ?
At the time I write this answer, this is not possible. There are two problems on Windows, and one problem on Mac and Linux:
VS Code does not provide a default path to the file dialog 1. It does remember the last folder that you opened a file in, but that path cannot be used as a default because it is overwritten constantly.
On Windows only, Electron ignores the default path when creating a file dialog if the default path is a directory 2.
An extension also cannot solve this, because extensions are not allowed to modify the File menu 3.
I think the best option at this point is to pin a folder to the Quick Access area in Windows Explorer, as suggested in a comment, or to put an actual shortcut in the user profile folder.
Workspaces and File > Open Recent may also be helpful if you often open the same folders.
Your main problem is that you are unable to open your specific folder in VScode.
To solve that you can simply open the terminal/cmd in that specific window by just typing cmd in your search bar or just by pressing shift+right-click in that folder.
Now your cmd is open and you just have to type "code ." in the cmd and press enter to open the current folder in your VSCode.
In case that code . doesn't work for you then you have to add the Vscode in the environment variables of your windows.
Visual Studio doesn't provide a specific feature to open a specific path. But there is a solution to your problem. You are saying that you want D:\develpment as a default when you open VS Code. You can go to that specific directory or create shortcut to desktop then click right click on that folder and then click on open with code. If you didnot see open with code then reinstall your VS code and check on open with code when you are reinstalling VS Code.
make a shortcut on the desktop for vscode and then modify it and add the folder after the .exe command. This will default open that folder when you double click on it.
Visual Studio Code can be installed in two ways - User setup and System setup. I strongly believe you have User setup installed in your PC. Try re-installing it System-wide. That should probably fix your problem.
For more information: https://code.visualstudio.com/docs/setup/windows#_user-setup-versus-system-setup
PS: A lot more information is needed, you can share a screenshot of the window and elaborate more on it.

How to open VSCODE with new file from command line and preserve prior state?

Using Visual Studio Code (latest version as well as prior versions), when using the command line to launch I am seeing some odd behavior. Trying to determine if I am missing something, or if this is a bug.
Here is the scenario.
Action 1: I have a workspace open with a couple of files open for editing. If I close VSCODE, and then just open it again the state is restored, I am in the workspace and the files are open for editing. Great.
Action 2: I have a workspace open with a couple of files open for editing. If I then from a command line do "vscode testfile.txt" then the new file gets opened in VSCODE and joins the other opened file. Great.
Action 3: I have a workspace open with a couple of files open for editing. I close VSCODE. I then from a command line do "vscode testfile.txt". VSCODE starts up, but only the new file gets opened in VSCODE. The workspace I had opened is not any more, and the files I had opened are no longer opened. Not so great.
I have tried adding the -r option on the command line but that didn't change anything.
This feels like a bug, but if not help me understand how I get the desired result, which is when VSCODE is not running, and I try to edit a file from the command line, I want VSCODE to restore to the state it was in when last closed, and then add that new file to the mix. Just like if it was already running.

How to open multiples files in one Code instance from command line?

If I open a file in Visual Studio Code from the command line or from within File Explorer, each file will open in a separate Code instance. Is there a way to make them all open in the same instance? I.e. add them all to the 'Working Files' list?
Upcoming version will have a new option files.openInNewWindow which you can set to off to always open files into the last active instance. This is then identical to starting VS Code with the -r option but is much nicer to use (e.g. when you open a file from the desktop).
Update for our VS Code 1.0 release:
The setting is available as window.openFilesInNewWindow
You can use the command line option of -r or --reuse-window to assure it opens in the last active VS Code window
You can review the other options here
code Folder1/File1.txt Folder2/File2.java Folder3/File3.css
This command opens three files in one instance of the editor. All files are going to be added to the working files list.
This even works with much more files in the parameter list.