How to Debugger in VSCode? - visual-studio-code

I added new extension in VSCode. I've added syntax and snippets. It's time for debugging. I searched a lot and couldn't get exactly what I wanted. I want to debug without using tasks.json.
My json files:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Run",
"request": "attach",
"windows": {
"preLaunchTask": "windows"
},
"linux": {
"preLaunchTask": "linux"
},
"osx": {
"preLaunchTask": "osx"
}
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "windows",
"command": "echo",
"args": ["hello windows"],
"type": "shell"
},
{
"label": "linux",
"command": "echo",
"args": ["hello linux"],
"type": "shell"
},
{
"label": "osx",
"command": "echo",
"args": ["hello macOS"],
"type": "shell"
}
]
}
Thanks for all your answers.

Related

vscode tasks.json transform pickfile

I have next vscode tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "${userHome}/build.py",
"args": ["param_def=${input:pickBuild}"]
}
],
"inputs": [
{
"id": "pickBuild",
"type": "command",
"command": "extension.commandvariable.file.pickFile",
"args": {
"include": "**/default_*.py",
"description": "Select build",
"display": "fileName",
}
}
]}
when run:
actual: /home/user/build.py param_def=/home/user/default_android.py
expected: /home/user/build.py param_def=default_android
P.S: I have tried use extension.commandvariable.transform, but did not success
You can use the following config in your tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "${userHome}/build.py",
"args": ["param_def=${input:pickBuild}"]
}
],
"inputs": [
{
"id": "pickBuild",
"type": "command",
"command": "extension.commandvariable.transform",
"args": {
"text": "${pickFile:build}",
"find": "^.*/(.*).py",
"replace": "$1",
"pickFile": {
"build": {
"include": "**/default_*.py",
"description": "Select build",
"display": "fileName",
}
}
}
}
]}

How to use the output of running a which command in vscode launch.json?

I would like to use the output of running which brownie as the value for "program" in a launch.json. E.g. in this snippet of launch.json
"configurations": [
{
"name": "Brownie: run deploy.js",
"type": "python",
"request": "launch",
"program": "/home/fanta/.local/virtualenv/python3.10/bin/brownie",
"console": "integratedTerminal",
"args": ["run", "scripts/deploy.js"]
},
I would like to replace the full path that I have hard-wired /home/fanta/.local/virtualenv/python3.10/bin/brownie with the output of which brownie. How can I do that?
You can setup a preLaunchTask that executes the which brownie and writes it to a temp file
{
"version": "2.0.0",
"tasks": [
{
"label": "which brownie",
"type": "shell",
"command": "which brownie > /tmp/brownie-loc.txt"
}
]
}
Using the extension Command Variable you can use the command extension.commandvariable.file.content to use the file content in the launch
{
"version": "0.2.0",
"configurations": [
{
"name": "Brownie: run deploy.js",
"type": "python",
"request": "launch",
"program": "${input:browniePath}",
"console": "integratedTerminal",
"args": ["run", "scripts/deploy.js"],
"preLaunchTask": "which brownie"
}
],
"inputs": [
{
"id": "browniePath",
"type": "command",
"command": "extension.commandvariable.file.content",
"args": {
"fileName": "/tmp/brownie-loc.txt"
}
}
]
}

How Do I Set Task as preLaunchTask in .code-workspace?

I have a multi-root workspace with launch configurations and tasks. The tasks run fine on their own, but they don't run when added as a preLaunchTask. VS Code throws the error "Could not find the task".
oving them to a tasks.json file is not an option for me.
Here's the relevant information from my .code-worspace file
{
"folders": [
{
"name": "App",
"path": "app"
},
{
"name": "API",
"path": "api"
},
],
"settings": {},
"launch": {
"configurations": [
{
"name": "Launch App",
"type": "chrome",
"request": "launch",
"preLaunchTask": "npm: start",
"cwd": "${workspaceFolder:App}",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder:App}/src",
"sourceMapPathOverrides": {
"webpack:///src/*": "${webRoot}/*"
}
}
]
},
"tasks": {
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "start",
"label": "npm: start",
"detail": "react-scripts start",
"options": {
"cwd": "${workspaceFolder:App}"
},
"problemMatcher": []
}
]
}
}
I don't know why, but this only happens if type = npm.
Change:
"type": "npm",
"script": "start",
to:
"type": "shell",
"command": "npm start",

How to pass input variable from launch.json to tasks.json in vscode

I can use input variables from launch.json in launch.json.
"configurations": [
{
...
"args": [${input:file_no}]
"preLanuchTask": "runPreTasks"
...
}
],
"inputs": [
{
"id": "file_no",
"type": "promptString"
}
]
Now, I want to get access to the same variable without entering input a second time in tasks.json.
{
"version": "2.0.0",
"tasks":[
{
"label": "runPreTasks",
"type": "shell",
"command": sh,
"args": [
"/path2script/scriptName.sh",
"${input:file_no}" // This does not work, without defining input again
]
}
]
}
Is there a way to pass input variables from launch.json to tasks.json in vscode?
You can use the extension Command Variable v1.21.0
It has a command extension.commandvariable.promptStringRemember that behaves the same as an ${input:name} promptString variable. By adding a key property the result is saved under this key and you can retreive it with the extension.commandvariable.rememberPick command.
The extension.commandvariable.rememberPick command can be used in a different task/launch than the extension.commandvariable.promptStringRemember command
{
"version": "2.0.0",
"tasks": [
{
"label": "Task 1",
"type": "shell",
"command": "dostuff1",
"args": ["-p", "${input:promptPath}"]
},
{
"label": "Task 2",
"type": "shell",
"command": "dostuff2",
"args": ["-p", "${input:rememberPath}"]
},
{
"label": "Do Task 1 and 2",
"dependsOrder": "sequence",
"dependsOn": ["Task 1", "Task 2"],
"problemMatcher": []
}
],
"inputs": [
{
"id": "promptPath",
"type": "command",
"command": "extension.commandvariable.promptStringRemember",
"args": {
"key": "path",
"description": "Enter a path"
}
},
{
"id": "rememberPath",
"type": "command",
"command": "extension.commandvariable.rememberPick",
"args": { "key": "path" }
}
]
}
Following #rioV8 answer, I edited my json files as shown below:
launch.json:
"configurations": [
{
...
"args": [${input:file_no}]
"preLanuchTask": "runPreTasks"
...
}
],
"inputs": [
{
"id": "file_no",
"type": "command",
"command": "extension.commandvariable.promptStringRemember",
"args": {
"key": "lastnumber",
"description": "Enter the number"
}
}
]
tasks.json:
{
"version": "2.0.0",
"tasks":[
{
"label": "runPreTasks",
"type": "shell",
"command": sh,
"args": [
"/path2script/scriptName.sh",
"${input:file_no}"
]
}
]
"inputs": [
{
"id": "file_no",
"type": "command",
"command": "extension.commandvariable.rememberPick",
"args": { "key": "lastnumber" }
}
]
}

VScode debug mode shows "Could not find the specified task"

When trying to start a Debug session in vscode, it throws an error about not finding a specified task. I've already tried the solutions of other SO questions like this but without success.
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
...
},
{
"name": "MSEventHubs_Audit",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/audit-events.py",
"console": "integratedTerminal",
"args": [
"config/config.ini",
],
"envFile": "${workspaceFolder}/.env",
"env": {"PYTHONPATH": "${workspaceRoot}"},
"justMyCode": false,
"preLaunchTask": {
"task": "audit_tunnel"
}
},
{
...
},
]
}
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"command": "",
"args": [],
"tasks": [
{
"label": "activate_key",
"type": "shell",
"command": "smartOS_card_pin.sh",
"args": [
"${inputs:cardos_pass}"
],
"group": "build",
},
{
"label": "audit_tunnel",
"type": "shell",
"group": "build",
"command": "ssh",
"args": [
"-NL",
"port1:127.0.0.1:port2",
"my_host"
],
"dependsOn": "activate_key"
},
{
...
}
],
"inputs": [
{
"id": "cardos_pass",
"type": "promptString",
"password": true
}
]
}
I've been looking at it for a while and cannot figure out what I'm doing wrong. The proof my task is known by vscode is when the pop-up message appears
I click 'Configure Task', the 'audit_tunnel' appears as an option to edit.
What am I missing?
Change:
"preLaunchTask": {
"task": "audit_tunnel"
}
To:
"preLaunchTask": "audit_tunnel"