How do you stop VS Code from loading a corrupt file? - visual-studio-code

I accidently opened a file that didn't have a suffix, just "file.", that doesn't even exist and now whenever I try to close it, VS Code crashes. When I reopen Code, it always reopens the file. I've scoured settings and tried these fixes but nothing helps.
Visual Studio Code always reopens previous file or folder
How can I completely clear the cache for VS code and stop reopening files? It should be possible outside of VS Code because I get the feeling settings aren't be preserved because of the corrupt file.

FWIW, I was able to track down the call to the file in $HOME/.config/Code/Backups. Once I removed the offending Backup file, I can open Code once again without loading the corrupt file.

Related

VScode: Show a prompt when loaded files are modified externally on disk

Which is the final outcome of the VScode [issue] #14298 (https://github.com/microsoft/vscode/issues/14298) ?
Is it: "No, we will NOT issue a warning, if an external app modifies a VScode opened file, like ALL other editors do ? (except Notepad)"
Up to now, I cannot find such a setting...
I have never seen a prompt when an open file is changed elsewhere. What I have seen is a warning when I try to save a file that has been changed elsewhere. I have seen this: preventing dirty writes
VS Code will show you an error message when you try to save a file
that cannot be saved because it has changed on disk. VS Code blocks
saving the file to prevent overwriting changes that have been made
outside of the editor.
In order to resolve the save conflict, click the Compare action in the
error message to open a diff editor that will show you the contents of
the file on disk (to the left) compared to the contents in VS Code (on
the right):
Until those issues have been resolved I believe that is as close as you are going to get.

How to prevent VS Code remembering files outside work folder?

My workflow with VS Code is to always use project folders on projects. This works great, but there's one thing that's messing things up right now;
When opening just a single file from another project while working in another project, lets's say webpack.config.js, I use Windows Explorer rightclick to open that file in VS Code. However, this always opens the file in the currently open VS Code instance.
This adds the file from that other folder now to the memory of the previously opened files in the open project while it has nothing to do with that project.
When using the quick file open of VS Code now it happens a lot VS Code opens a file from that other folder, only because that outside file has been opened once while I was working on this project.
This is error prone as now it happens a lot, especially when working fast, that the wrong, for instance, package.json or webpack.config.js file gets opened, because VS Code now also suggests files that are outside this project. That's pretty frustrating and causes changes to be made in the wrong files pretty quickly.
I know we can clear the editor history and have to do it a lot now. But to me that's the other way around, doesn't solve the real problem and when not done often the issue remains.
So I want to get rid of this issue and have the following questions:
1) How can we change the 'Open with Code' rightclick menuitem in Explorer to open files always in a new instance of VS Code?
2) Is there a config settings to disable the behaviour that VS Code remembers files that are outside of the current work folder?
Thanks in advance, this thing is bothering me for quite some time now!
How can we change the 'Open with Code' rightclick menuitem in Explorer to open files always in a new instance of VS Code?
I assume you are a Windows user. You can edit the context menu behavior from Regedit.
Follow the below steps:
Press Windows + R
Type regedit and press OK
Go to Computer\HKEY_CLASSES_ROOT\*\shell\VSCode\command in regedit
Add -n parameter to the command like below
It will help you to open every file in a new window when you use Open with Code.
For your second question, Is there a config settings to disable the behavior that VS Code remembers files that are outside of the current work folder?
Open VS Code settings
Search window.openFilesInNewWindow
Make it off to on like below
With this configuration, you can't open new files in the current workspace therefore technically you prevent VS Code remember outside files. Not directly but it helps.
I don't like how "recently opened" files are shown in VS Code Quick Open (Ctrl+P). I think OP is describing this behavior...
The behavior to remember "recently opened" files causes me problems when I mistakenly open a temporary/copy of my file, i.e.
C:/users/temp/code.js
... instead of the true file (i.e. located inside the currently-open VS Code project)
C:/git/source-control/code.js
...my confusion happens because because both files are listed among the options when I use Quick Open Ctrl+P and type "code"
For me , the Quick Open list also has the answer, any "recently-open" files should have an "X" icon on the right side. You may need to hover your mouse over the file you want to remove.
Click the "X" icon to "Remove from recently open". Quick Open will always be able to find files in your current project, even if you remove them from recently-open:
Please note I don't have access to regedit as suggested in the accepted answer; my answer here does not require regedit

How to recover binary file opened and saved in VSCode?

I'm working with a new student who's getting started with Python programming in Visual Studio Code. She's exploring and getting to know programming by feel and using VS Code and kitao/pyxel to learn the ropes. In that project, one creates .pyres files (that are binary/zip files). When that file appeared in the editor, the user clicked on the file, acknowledged the "view this file as text even though it's binary" warning, and then saved it, thereby corrupting the file. And since VS Code and Windows are uncompromising on these sorts of mistakes, all of the work in generating the assets appears to be lost.
Is there any way given a binary file that was opened and re-saved unchanged in VS Code to reverse the changes that VS Code makes to that file? Any insight into what changes VS Code makes to a binary file when opened as text and saved?

Keeping files in cache VSCode/Prevent erase after closing

I recently moved to VSCode from Notepad++, and I just found an annoying difference.
When I had for example n files opened in Notepad++ and I closed it(also if restarting computer), when re-opening it I used to find those files again, also if they were deleted from folder or edited.
In VSCode, just some files are keeping opened(it depends if you have got them locally or download once and then delete or refresh them), and that's an annoying thing for me.
I guess this function is related to cache system, so I am wondering: Is there any method to make the VSCode cache work like Notepad++'s does in VSCode? Are there any extensions to do it?
I mean: to keep files always in cache, except when I close them.
Thank you in advance

How to prevent auto save when executing code in Visual Studio

When I run my code in Visual Studio, it auto saves my code -without prompting (!)- even though I have auto-save set to off. Is there a way to prevent this?
I've made sure auto-save is off in preferences. I haven't seen any setting that applies specifically to "on execute"
As an example of how this made me lose some hair, I opened a file that had some unknown characters (which opened just fine in Powershell ISE) and I didn't know until the code failed when I executed it. And since it auto-saved on execute, it wrote the bad characters to the file, corrupting it for all eternity. Not to mention, if I modify a few characters to test something, I don't want it to auto-save for me.
Search for autosave under File-> Preferences - Settings and turn it off