Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Bash and Visual Studio Code: How can I launch bash as a run task?

how can I run the Windows Bash from Visual Studio code as a run task?

Here are some of my many attempts at tasks.json to do this.

{
    "version": "0.1.0",
    "command": "cmd",
    "isShellCommand": true,
    "args": [ "RunShellScript.bat" ],
    "showOutput": "always"
}

RunShellScript.bat has only this line: bash myShellScript.sh. Which if you just open cmd from start, and type that line, it will execute. It also runs perfectly if you just double click the file as well. However, when launched from VSCode, this output just hangs until I terminate it.

Attempt number 2:

{
    "version": "0.1.0",
    "command": "cmd",
    "isShellCommand": true,
    "args": [ "bash myShellScript.sh" ],
    "showOutput": "always"
}

This also hangs, just like above.

Attempt number 3:

{
    "version": "0.1.0",
    "command": "C:/Windows/System32/bash",
    "isShellCommand": false,
    "args": [ "myShellScript.sh" ],
    "showOutput": "always"
}

This gives me:

Failed to launch external program C:/Windows/System32/bash myShellScript.sh
spawn C:/Windows/System32/bash ENOENT

Ive also experimented with setting the "isShellCommand" variable to true and false in some of these cases, but to no avail.

Does anyone know how to do this?

like image 243
RestlessAPI Avatar asked May 16 '16 06:05

RestlessAPI


3 Answers

I too was looking to see how I could do the same thing.

andlrc - I've tried your two options and neither of them seem to do the trick. The issue seems to be in that bash is not recognised in whatever context Visual Studio Code runs it in. I've tried various approaches:

  • "command": "C:/Windows/System32/bash.exe"
  • "command": "bash"
  • "command": "bash.exe"

I'll keep trying and post back if I get something working.

Edit: Got it. Credit to this man - https://aigeec.com/using-windows-10-anniversary-bash-with-visual-studio-code/

In summary, what it boils down to is that I am on a 64 bit operating system which means bash.exe is located in C:\Windows\System32. Once i'd copied bash.exe into SysWOW64 as the blog post suggests, the default build action (Ctrl+Shift+B) of Visual Studio Code runs as you'd expect it to.

This seems a bit of a hacky workaround - it's probably worth raising with MSFT to see if there's something we can do about it.

Edit: Here's my tasks.json as requested:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "0.1.0",
  "command": "bash",
  "isShellCommand": true,
  "args": [ "./gulp.sh" ],
  "showOutput": "always",
  "tasks": [{
    "taskName": "scripts",
    "isBuildCommand": true,
    "problemMatcher": "$gulp-tsc",
    "isWatching": true
  }]

}

gulp.sh contains only one command; 'gulp' :-) but you could essentially put whatever you want in there, I suppose you could have several .sh scripts to cater for the different tapes of tasks

In response to the use of Sysnative, unfortunately it doesn't seem to understand that and the build task does not run.

like image 112
Jason Dunbar Avatar answered Nov 11 '22 10:11

Jason Dunbar


On my Windows 10 machine that has a freshly installed bash, this works fine for me:

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args": [ "shellscript.sh" ],
    "showOutput": "always"
}

The shebang version of shellscript.sh as command doesn't work for me.

You can also use bash -c "<command>" to run an arbitrary command:

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args": ["-c", "echo \"test\""],
    "showOutput": "always"
}

enter image description here

like image 44
Daniel Imms Avatar answered Nov 11 '22 10:11

Daniel Imms


Try:

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args": [ "myShellScript.sh" ],
    "showOutput": "always"
}

Or you can add a shebang to your file:

#!/bin/bash

and then write the path to the script:

{
    "version": "0.1.0",
    "command": "/path/to/script.sh",
    "isShellCommand": true,
    "args": [],
    "showOutput": "always"
}
like image 1
Andreas Louv Avatar answered Nov 11 '22 09:11

Andreas Louv