How to define a global variable in launch.json in VSCode? - visual-studio-code

In the VSCode's lauch.json file, I use multiple configurations for debugging different files. And some of these variables are common between configurations.
I wonder if this could be possible to define a global variable in the launch.json and use it in different locations of the file?
For example, I'm looking for a way to define variable_common once and use it in config 1 and config 2:
"configurations": [
{
"name": "Python: config 1",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": ["variable_1", "variable_2", "variable_common"]
},
{
"name": "Python: config 2",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": ["variable_3", "variable_4", "variable_common"]
}
]

With the extension Command Variable you can create custom variables
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: config 1",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": ["variable_1", "variable_2", "${input:VAR_COMMON}"]
},
{
"name": "Python: config 2",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": ["variable_3", "variable_4", "${input:VAR_COMMON}"]
}
],
"inputs": [
{
"id": "VAR_COMMON",
"type": "command",
"command": "extension.commandvariable.transform",
"args": { "text": "common_arg" }
}
]
}

Related

VSCode passing variables in args field for launch.json file

I'm trying to pass the variable "foo" which contains the "bar" in the args field, but no syntax seems to work.
I've tried either for the syntax "${foo}" or "${env:foo}".
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"env": {
"foo": "bar",
},
"args": [
"${foo}", // output: \$\{foo\}
"${env:foo}" // output: ""
]
},
],
}

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

Unable to write into user settings. I can not turn on autosave

please help,
This is what the settings in my json has. I am unable to turn on autosave.
the error seems to be where the colon is after "launch" on the first line.
I am no sure if that is where the actual error is.
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Local Node File",
"console": "integratedTerminal",
"program": "${file}"
},
{
"name": "Launch index.html",
"type": "chrome",
"request": "launch",
"file": "${workspaceFolder}/index.html"
},
]
}{
"files.autoSave": "afterDelay",
"editor.minimap.enabled": false,
"window.zoomLevel": 1,
"security.workspace.trust.untrustedFiles": "open",
"editor.tabSize": 2
}
This should work - this is the ENTIRE settings.json file:
{
"launch": {
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Local Node File",
"console": "integratedTerminal",
"program": "${file}"
},
{
"name": "Launch index.html",
"type": "chrome",
"request": "launch",
"file": "${workspaceFolder}/index.html"
}
]
},
"files.autoSave": "afterDelay",
"editor.minimap.enabled": false,
"window.zoomLevel": 1,
"security.workspace.trust.untrustedFiles": "open",
"editor.tabSize": 2
}
The launch setting goes within the outer json {} delimiters. And then the other settings follow also within those outer brackets as I have shown it.
You had another { before the other settings. And no "launch: { before the version, etc. part (although you mention it you don't show it). launch is part of that setting.

VSCode Gatsy Debug Error with SourceMaps

I was trying to set up VSCode to be able to debug Gatsby code.
I am new to Javascript sourcemaps which seem to be the cause of the problem.
I am seeing the following error on launch:
Cannot launch program "c:\Gatsby\myprogram\node_modules\.bin\gatsby" because corresponding Javascript cannot be found.
I verified that the path to the file gatsby in the error exists.
This is the file that I am using for launch.json:
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"protocol": "inspector",
"program": "${workspaceRoot}/node_modules/.bin/gatsby",
"args": ["develop", "-p", "7777"],
"stopOnEntry": false,
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development",
"DEBUG": "gatsby:*"
},
"console": "integratedTerminal",
"sourceMaps": true,
"outFiles": []
}
]
}
I was able to get this working by using globally installed gatsby-cli's gatsby instead of the one in node_modules. So:
npm install --global gatsby-cli
and then (since I use node/npm etc under nvm):
{
"type": "node",
"request": "launch",
"name": "Launch 'gatsby develop'",
"protocol": "inspector",
"program": "${env:HOME}/.nvm/versions/node/v8.11.3/bin/gatsby",
"args": [
"develop"
],
"stopOnEntry": false,
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development",
"DEBUG": "gatsby:*"
},
"console": "integratedTerminal",
"sourceMaps": true,
"outFiles": []
}
worked for me.
I'm on OSX though, more changes may be needed for your Windows setup.
Also: to use node under nvm with VSCode, I used the default alias method from here: Visual Studio Code to use node version specified by NVM
Quoted from the documentation
VS Code Debugger (Auto-Config)
Using VS Code’s integrated terminal run node --nolazy node_modules/.bin/gatsby develop --inspect-brk instead of gatsby develop or node --nolazy --inspect-brk node_modules/.bin/gatsby build instead of gatsby build
VS Code Debugger (Manual Config)
Linux
{
"version": "0.2.0",
"configurations": [
{
"name": "Gatsby develop",
"type": "pwa-node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/.bin/gatsby",
"args": ["develop"],
"env": {
"PARCEL_WORKERS": "0",
"GATSBY_CPU_COUNT": "1",
},
"runtimeArgs": ["--nolazy"],
"console": "integratedTerminal"
},
{
"name": "Gatsby build",
"type": "pwa-node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/.bin/gatsby",
"args": ["build"],
"env": {
"PARCEL_WORKERS": "0",
"GATSBY_CPU_COUNT": "1",
},
"runtimeArgs": ["--nolazy"],
"console": "integratedTerminal"
}
]
}
Windows
{
"version": "0.2.0",
"configurations": [
{
"name": "Gatsby develop",
"type": "pwa-node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/.bin/gatsby",
"windows": {
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby"
},
"args": ["develop"],
"env": {
"PARCEL_WORKERS": "0",
"GATSBY_CPU_COUNT": "1",
},
"runtimeArgs": ["--nolazy"],
"console": "integratedTerminal"
},
{
"name": "Gatsby build",
"type": "pwa-node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/.bin/gatsby",
"windows": {
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby"
},
"args": ["build"],
"env": {
"PARCEL_WORKERS": "0",
"GATSBY_CPU_COUNT": "1",
},
"runtimeArgs": ["--nolazy"],
"console": "integratedTerminal"
}
]
}

How to configure vs code working directory in the launch.json

I use goland(same as webstorm/intellij etc) IDE and in debug configuration there is a place when you can configure working directory Now I try to work with VSCODE and I dont find this configuration , after a bit research I find the following json which should handle this but dont find the right place for working directory
e.g. this is my working directory
/Users/i022226/go/src/myapp
"configurations": [{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}"
},
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}"
},
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": [],
"showLog": true
}
In the launch.json there is add configuration button and when I type cwd I dont get any entry, any idea ?
In this post the cwd is under the option but I dont find the option
https://github.com/Microsoft/vscode/issues/856
Here's an example launch.json to run a Python module in a project subfolder based on Tals's answer:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Launch",
"type": "python",
"request": "launch",
"module": "module_source_folder.filename",
"cwd": "${workspaceFolder}/examples/folder_with_test_files",
"args": ["-f", "input_filename"]
}
]
}
Note that cwd must come before args or it won't work.
You should add it like following
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"cwd": "Your Path",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": [],
"showLog": true
}