Using Windows 10 Anniversary bash with Visual Studio Code

So you might be enjoying the new bash console that comes with Windows 10 preview build 14316.

You might also be enjoying the recently released version 1.0 of Visual Studio Code.

One of the features of VS Code I wanted to try was the debugging feature. I had been using vagrant for running my node processes so my actual windows env is not set up for running the app, node/npm versions are out of date etc. As a result the default debug launch config did not work.

I did not want to set up node etc, one of the reasons I was using vagrant was to have an isolated env for doing my dev stuff without messing up my pc.

I had been playing with the new bash feature and knew my app was working fine there so the question was how to get VS Code to use the new bash feature.

After a bit of head scratching as to why VSC could not see the bash command I realised that VSC is 32bit and so when it was looking as System32 it was getting redirected to SysWOW64. There was no bash.exe in SysWOW64. On the off chance that it might work I simply copied the bash.exe file from System32 to SysWOW64 and viola VSC could now see it.

When referring to bash.exe use "C:/Windows/sysnative/bash.exe"

Hat-tip to this guy https://divillysausages.com/2015/07/13/hacking-how-nodejs-launches-in-visual-studio-code/ who had done some work with the runtimeExecutable in the lauch.json and I reused his idea of creating a batch file for running commands.

You can do this with one batch file that sets you evn variables inline with the exec node command e.g. C:/Windows/sysnative/bash.exe -c "PORT=3000 exec node %*"

The first one you need is to use bash (I called mine bash-node.bat):

C:/Windows/sysnative/bash.exe ./env.sh %*  

The %* is important here as it passes the arguments from the launch profile through to o the env.sh.

The second is one run by bash (I called mine env.sh) as referenced in the first file:

#!/bin/bash
echo "Adding env vars"  
export PORT=3000  
exec node [email protected]  

Here you can add whatever env properties you need and launch your app by passing the values through to the node app using [email protected]. The exec is important as it will cause the node process to be terminated when the bash process is ended by the debugger other wise your node process would keep running and you would have to kill it before launching again.

In your launch.json set the following:

"runtimeExecutable": "${workspaceRoot}/bash-node.bat",
"externalConsole": true,

externalConsole can be false too.

Done.