119

Is there a way to execute an ssh command when debugging a project with .vscode/launch.json?

For example: ssh -i xxxxx.

Or is it possible to create a command that you can run from the F1 command palette pop-up? Something like RunCustomCommandxx.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3466947
  • 1,227
  • 2
  • 11
  • 13
  • there is also https://marketplace.visualstudio.com/items?itemName=gabrielgrinberg.auto-run-command – ctf0 Jul 20 '19 at 21:17

8 Answers8

141

You can define a task in your tasks.json file and specify that as the preLaunchTask in your launch.json and the task will be executed before debugging begins.

Example:

In tasks.json:

For version 0.1.0:

{
    "version": "0.1.0",
    "tasks": [{
        "taskName": "echotest",
        "command": "echo", // Could be any other shell command
        "args": ["test"],
        "isShellCommand": true
    }]
}

For version 2.0.0 (newer and recommended):

{
    "version": "2.0.0",
    "tasks": [{
        "label": "echotest",
        "command": "echo", // Could be any other shell command
        "args": ["test"],
        "type": "shell"
    }]
}

In launch.json:

{
    "configurations": [
        {
            // ...
            "preLaunchTask": "echotest", // The name of the task defined above
            // ...
        }
    ]   
}

Tasks documentation: https://code.visualstudio.com/docs/editor/tasks

Launch configuration: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations

Anthony
  • 804
  • 3
  • 12
  • 32
Saravana
  • 37,852
  • 18
  • 100
  • 108
  • Hi Saravana, thanks for the response, Im getting a error: Failed to launch external program ssh -i ~/.ssh/some_key -L 3307:127.0.0.1:3306 forge@xxx.203.107.80 . spawn ssh -i ~/.ssh/some_key -L 3307:127.0.0.1:3306 forge@xxx.203.100.80 ENOENT – user3466947 May 08 '17 at 12:41
  • Make sure ssh is in your path. Or give the absolute path in your command. – Saravana May 08 '17 at 12:52
  • 1
    @user3466947, isn't `Address already in use` saying you enough? Connect to the machine with ssh and try `netstat -lnpt` to see if the process is `already using the address`. – fiorentinoing Jan 30 '19 at 11:00
  • 1
    Which type of launch configuration do you use? The ones I have (e.g., python) require a "program" path...I guess I could just set it to a dummy python file. But, I don't really even want to engage the debugger, just run my task. – Hawkeye Parker Nov 30 '19 at 22:08
  • I tried this in GNU\Linux to set an environment variable. The task fired off as expected, but I don't think the environment variable is getting passed through to launch.json. Did I do something wrong or is what I'm trying to do impossible? https://stackoverflow.com/questions/51317136/how-can-i-get-a-task-to-set-a-variable-that-can-be-used-in-a-configuration – Shawn Eary Sep 16 '21 at 13:31
  • I believe this is out-of-date, now you can use `type: node-terminal` as described below. – Tom May 06 '23 at 20:36
40

The format changed. Visual Studio Code can create the tasks.json file for you. Inside your launch.json file and inside any configurations object, just define preLaunchTask to force auto-creation of the tasks.json template file:

{
  "version": "0.2.0",
  "configurations": [
      {
        "type": "node",
        "request": "launch",
        "name": "launch program",
        "skipFiles": [ "<node_internals>/**"],
        "preLaunchTask": "myShellCommand",
        "program": "${workspaceFolder}/test.js"
      }
  ]
}

If you do not have file tasks.json configured:

  • launch the debugger. Visual Studio Code will inform you: "could not find the task myShellCommand"
  • select Configure Task → Create tasks.json file from template → Others

Your tasks.json template file will then be created for you. Add your command to command and the name you put in preLaunchTask to label:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "myShellCommand",
      "type": "shell",
      "command": "echo goodfood"
    }
  ]
}
nullromo
  • 2,165
  • 2
  • 18
  • 39
Emmanuel N K
  • 8,710
  • 1
  • 31
  • 37
16

For me, I just needed an environment variable, which is different. You don't want a task for this because (at least for me) it doesn't work when the launcher is run.

Thanks to here, I got it working like this, inside my launcher (launch.json) entry:

"environment": [{
    "name": "ENVIRONMENT_VARIABLE_NAME",
    "value": "${workspaceFolder}/lib" //Set to whatever value you want.
}],
Andrew
  • 5,839
  • 1
  • 51
  • 72
8
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch app",
            "program": "lib/main.dart",
            "request": "launch",
            "type": "dart"
        },
        {
            "name": "Build an APK Release",
            "command": "flutter build apk --release",
            "request": "launch",
            "type": "node-terminal"
        },
        {
            "name": "Install an APK on a device",
            "command": "flutter install",
            "request": "launch",
            "type": "node-terminal"
        }
    ]
}
user16217248
  • 3,119
  • 19
  • 19
  • 37
Yauheni Prakapenka
  • 1,056
  • 11
  • 10
4

My version of the configuration allows to just run a defined task and carried on (in my case, the task is to run the currently open Groovy file):

"configurations": [
    {
        "name": "Launch groovy",
        "request": "launch",
        "type": "coreclr",
        "preLaunchTask": "groovy",
        "program": "cmd.exe",
        "args": ["/c"],
        "cwd": "${workspaceFolder}",
        "console": "internalConsole",
        "internalConsoleOptions": "neverOpen"
    }
]

And the task:

"tasks": [
    {
        "label": "groovy",
        "type": "shell",
        "command": "groovy ${file}"
    }
]
user16217248
  • 3,119
  • 19
  • 19
  • 37
Jakub Pawlinski
  • 442
  • 3
  • 16
  • 2
    I did this, and my tasks command is ran, however it does not carry on to launch the python file debugging. Any idea why? – Mymozaaa Oct 10 '20 at 16:21
  • in my case, `"cwd": "${workspaceFolder}"` parameter did the trick. – Walk Feb 22 '22 at 12:08
2

Yes, it's possible. Just use:

"type": "node-terminal"

(don't worry it says 'node-terminal', it should work out-of-the-box in VS Code)

and specify your command in "command" property:

"command": "ssh -i xxxx"

Hope it helps.

Lukasz Czerwinski
  • 13,499
  • 10
  • 55
  • 65
1

for flutter developers who is searching how to run flutter build commands. in tasks.json add

"tasks": [
        {
            "type": "flutter",
            "command": "flutter",
            "args": [
                "pub",
                "run",
                "build_runner",
                "build",
                "--delete-conflicting-outputs"
            ],
            "problemMatcher": [
                "$dart-build_runner"
            ],
            "group": "build",
            "label": "flutter: flutter pub run build_runner build --delete-conflicting-outputs"
        },
]

then from vscode you can try "run task" it will show you flutter: flutter pub run build_runner build --delete-conflicting-outputs

this way you don't need to memorize and type to terminal source code generation/build commands

sultanmyrza
  • 4,551
  • 1
  • 30
  • 24
0

To create a launch configuration, use the PowerShell Visual Studio extension and place your commands in the script.

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Build and run",
            "type": "PowerShell",
            "request": "launch",
            "script": " ${workspaceFolder}\\launch.ps1",
        }
    ]
}

launch.ps1

Start-Process -FilePath ssh -ArgumentList "-i","xxxxx" -NoNewWindow
AirToTec
  • 19
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 29 '23 at 14:42