VSCode can't write to files in Bash for Windows - visual-studio-code

I've been using Bash for Windows recently (its the Ubuntu "app") and I can open VSCode from the bash command line but when I try to save any modifications I've made to the file, it just says permission denied.
I've tried creating the files directly from VS Code's terminal but in that case it doesn't print out an error message but it also doesn't save anything.

That happens because you dont have permissions to write in the system32 folder. If you run the Ubuntu application as an administrator, you'll get the permissions to write. However, you won't be really in that file, because of the way that the virtualization of files and errors might happear doing so, and I do not recommend to run the console in a privilege mode (for seccurity issues).
Fortunately, in version 1903 of Windows 10, this will change, and you'll be able to navigate the WSL folders via Windows Explorer, and for sure that issue will disappear.
Edit: the best practice, you do the work in a folder in the Windows File System, and cd to there in the WSL console. VSCode has this integrated very well, if you have an open file in VSCode you can just do New Terminal and choose a WSL terminal. The terminal will already be in the folder where you file is.

Related

Set the path in portable vscode installation powershell terminal

I have multiple versions of vscode installed on my system with portable mode. I start each of them via a desktop shortcut. Since they are in portable mode, their installation directories are not in the default PATH variable.
When I make a new powershell terminal window, the portable vscode directory is not on the path. I need the command "code" in the terminal window to run the same vscode version that the terminal shell is running in. In other words, if I am running a portable vscode, and I open a powershell terminal and type the command "code", I want it to run the code.exe file that is the same as the vscode I am running it in, and not a different one, and not get the "object not found" message. For various reasons I do not want to have to type the full path.
So, in essence, I want to add a directory to the path variable when vscode starts up, with that directory being the base directory of the vscode itself.
You need to set this in: File/Preferences/Settings then select Workspace, there go to: Terminal/Integrated:Cmd and there specify An explicit start path where the terminal will be launched.

Opening WSL terminal in current VSCode directory without using VSCode server

I've used Git Bash for most bash-required tasks before, but since I've found WSL to be much more feature-rich as it's basically an entirely new subsystem. However, when I used to use VSCode along with Git Bash, it would simply cd into the working directory of the project for ease of use. However, it doesn't do that here. The only solutions I've found online are to create an entirely new Remote WSL VSCode window, but this is way too high maintenance for what I'm trying to do as I'm literally just trying to get VSCode to automatically cd into the correct directory. Thanks in advance.

How does VSCode's Remote Extension open files in my local editor through its internal terminal?

I use VSCode's remote development extension fairly regularly. I can use the terminal in VS Code as if it were on my own machine, and even the code command works correctly. That is, when I'm in a remote VSCode session, I can type code path/to/some/file and it will open another editor tab with that file. The terminal session and the file being opened are on the remote machine to which I've connected.
I have VSCode installed on the remote machine, and the code executable is in my PATH. So my question is, how is this functionality implemented behind the scenes? That is, how does VSCode know that when I type code path/to/some/file it should open that file into another editor tab on my machine instead of trying to fire up VSCode on the remote machine?
Literally seconds after I wrote the question I found the answer.
If I run which code in the terminal, it doesn't resolve to the usual VSCode executable, but instead it resolves to one located at $HOME/.vscode-server/bin/a5d1cc28bb5da32ec67e86cc50f84c67cc690321/bin/code.
If I echo $PATH I can see that $HOME/.config/bin and $HOME/.vscode-server/bin/a5d1cc28bb5da32ec67e86cc50f84c67cc690321/bin has been appended to the beginning of the PATH env var that my bash profile generates.
I assume this means that VSCode is executing bash with a different profile script that
Sources my usual bash profile
Creates the directories above and copies some helper programs into them
Modifies my path to include these directories
I also assume that the injected code executable is communicating with my local instance of VSCode in some way, instructing it to open the file in its editor.

Problem with saving and reopening a vscode workspace initiated from WSL

I open my vscode with code . command from WSL terminal because I want to work directly in my WSL. It works fine and for example, the file explorer of vscode shows directory structures based on WSL machine and not based on my Windows machine.
My problem is however that, when I save a workspace using File -> Save Workspace As, it saves it again as a workspace in my Windows machine. So when I close and open my vscode again and use Open Workspace... to open the already saved workspace(in previous step), explorer reads directories from Windows machine.
You might ask what's the difference between saving files in /mnt/c/Users/[user]/Desktop or C:\Users\[user]\Desktop, but the problem is two things:
I might open a session from a directory in WSL that is (unlike Desktop) inaccessible from Windows.
Even if my Desktop is my current working directory, I may add another directory to my workspace (using Add Folder to Workspace...) and even if it is a path accessible from Windows, the next time I open the workspace, I will get an exclamation mark and such an error near that directory in explorer:
C:\mnt\d\Files_And_Downloads\Google Drive_Code . Can not resolve workspace folder
I suspect this is a bug, vscode has mixed the WSL path and Windows path. So anyone who uses Remote - WSL extension and can tell me what's going on here?

Visual Studio Code - Where is the terminal history saved?

I noticed that the command history is saved when using Visual Studio Code. Using the arrow up and down keys, I can toggle through previously issued commands in the integrated terminal.
Where is the terminal history saved on the drive? Is it possible to open a file in notepad (or notepad++) and investigate the complete history, delete it?
I'm using Windows 10, and I have concluded that deleting the folders C:\Users\john\.vscode and C:\Users\john\AppData\Roaming\Code will not delete the terminal history for a particular project.
VSCode terminal use external shell. For linux the default shell is bash, I am not so sure about window, but I guess the default shell for window is powershell.
If your terminal shell is indeed powershell, this should help you locate the log file.
On Linux & MacOS, type the following command to display the path of the history file:
echo $HISTFILE
The history file may be different between VSCode and your regular terminal.
So you should type this command within the integrated terminal.
On Ubuntu you can see your history using this command:
vi ~/.bash_history