如何使用 launch.json 在 Visual Studio Code 中运行命令

在用 .vscode/launch.json调试项目时,是否有执行 ssh命令的方法?

例如: ssh -i xxxxx

或者是否可以创建一个命令,您可以从 F1命令面板弹出窗口运行该命令?就像 RunCustomCommandxx

114995 次浏览

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

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.
}],

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 TaskCreate tasks.json file from templateOthers

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"
}
]
}

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}"
}
]

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

{
"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"
}
]
}