Use environment variables in VSCode - visual-studio-code

This question is related to this question about conda, and it is pretty straightforward:
How can I use an external environment variable inside launch.json?
For instance, selecting the python executable inside my home with $HOME, or the executable:
{
"name": "Python: From Home",
"type": "python",
"request": "launch",
"program": "$HOME/Documents/a.py", // nor does "${HOME}" work
"console": "internalConsole",
"cwd": "${workspaceFolder}"
}
or
{
"name": "Python: With Anaconda",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole",
"python": "${HOME}/anaconda3/bin/python3",
"cwd": "${workspaceFolder}"
}
This would simplify sharing launch.json with coworkers.

You can use Environment variables
The syntax is like ${env:USERNAME}

Related

How to use standard input redirection to a file while debugging using cppdbg in Visual Studio Code

{
"version": "0.2.0",
"configurations": [
{
"name": "Run GDB",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/wrfchembcCT",
"args": [],
"stopAtEntry": false,
"cwd": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/",
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "make",
}
]
}
Above is the launch.json file used for debugging a Fortran code. I am able to start debugging but without an input file with a Fortran namelist. What should I add here such that the debugger accepts the input file too.
Actually the executable takes in the input file as follows:
wrfchembcCT < wrfchembc_namelist
But I am not able to debug while passing the data file to the Fortran code.
all arguments with redirection symbols <>| are quoted, so redirection will not work in VSC
Option is to add a CLI argument to the application that will open the file in the argument as stdin
{
"version": "0.2.0",
"configurations": [
{
"name": "Run GDB",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/wrfchembcCT",
"args": ["--stdin", "wrfchembc_namelist"],
"stopAtEntry": false,
"cwd": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/",
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "make",
}
]
}
Or you can start the program with a task, maybe starting a shell script that has redirection. And attach the debugger to this running program.
I got this solved by providing complete path to the executable in program section. Its now working for some reason.

VSCode: how to disable the validation of launch.json file?

Here is my launch.json file in VSCode:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Arquivo Atual",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"python": "${command:python.interpreterPath}",
"cwd": "${fileDirname}"
},
{
"name": "Debug Tests",
"type": "python",
"request": "test",
"console": "integratedTerminal",
"python": "${command:python.interpreterPath}", //Property python is not allowed.
"justMyCode": false
}
]
}
The commented line appears as a problem. I want to disable it, so my code have zero problems.
I believe it is a VSCode bug (already opened a issue), because it isn't marked as a problem in the first item, and if I change for the accept atribute of pythonPath the debugger does not work.
How do I disable the problem message just for this file?

Prevent vscode-jest-tests from opening new terminal

I am using vscode-jest and the "debug" code lens to run individual tests. However, when running a test like this it spawns a new terminal every time. How can I prevent this by modifying the launch configuration in the launch.json file?
For reference, here is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"name": "vscode-jest-tests",
"request": "launch",
"args": [
"--runInBand"
],
"cwd": "${workspaceFolder}/data-utils/",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"program": "${workspaceFolder}/data-utils/node_modules/jest/bin/jest"
}
]
}

Debug Jest tests for Lerna project in VS Code

I'm wanting to debug specific Jest tests in VS code for a project that uses Lerna, so there are multiple folders each with their own node_modules folder. With help from this answer I've got the following launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest watch",
"program": "${workspaceRoot}/my/specific/module/node_modules/jest/bin/jest.js",
"args": ["--verbose", "-i", "--no-cache", "--watchAll"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"cwd": "${workspaceFolder}/my/specific/module"
}
]
}
The problem is that I've had to put the specific path to the module in the launch config so I have to change it every time I want to debug something else.
Is there a better way to do this? Maybe use the folder that's selected in the Explorer? Maybe have some way of launching debug by right-clicking the test file?
With much thanks to dlac for the idea, I now have a working launch config:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest watch",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--verbose", "-i", "--no-cache", "--watchAll"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
},
"cwd": "${fileDirname}"
}
]
}
Lerna allows you to have npm packages in the root node_modules that are hoisted to your packages. That said, if you have jest as a dependency in your root package.json file you should be able to run tests in every package.
I have my launch.json configured as below. The first config runs all tests, and the second runs the test that is currently opened in VS Code.
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--runInBand", "--watchAll"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"${relativeFile}","--watchAll"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
]

How to debug Cucumber in Visual Studio Code (VSCode)?

I was trying to debug Cucumber scenarios in Visual Studio code and made below changes in the launch.json.
{
"name": "e2e",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}\\node_modules\\.bin\\cucumber-js",
"stopOnEntry": false,
"args": ["--no-timeouts", "--colors"],
"cwd": "${workspaceRoot}",
"runtimeExecutable": null,
"outFiles": [
"${workspaceRoot}\\features\\step_definitions\\*.js"
]
},
However, I am not able run a debug session using the above configuration. The step def. files I created in JavaScript.
So, just need a help on the script above if that looks fine?
You could try below configuration to make your debug working in VS Code. In the outFiles give your feature file path.
{
"name": "e2e",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/cucumber/bin/cucumber.js",
"outFiles": [
"${workspaceRoot}/features/*.feature"
]
}
============================================
UPDATE AS OF cucumber ^5.0.2:
{
"name": "NPM Cukes",
"type": "node",
"request": "launch",
"console": "integratedTerminal",
"program": "${workspaceRoot}/node_modules/cucumber/bin/cucumber-js",
"args": [
"path/to/features/**/*.feature",
"-r",
"path/to/steps/**/*",
"--tags",
"#your-tags"
]
}
If you want to debug only CURRENT feature, add this to launch.json
{
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/node_modules/.bin/cucumber-js",
"args": ["${relativeFile}"],
"name": "Cukes current",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/cucumber/bin/cucumber"
}
}
When working with Ruby, it could be used on this way to run specific feature files:
{
"name": "Cucumber",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/bin/cucumber",
"args": [
"--tags", "#Mytags",
]
}
Tweaking the answer from Mukesh Rawat plus ensuring additional file paths were correct, got it working for me, :
Launch.json
{
"name": "DebugMode",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/cucumber/bin/cucumber-js",
"args": [
"${workspaceRoot}/features/*.feature",
"--tags", "#debug"
]
}
Workspace.json
{
"cucumberautocomplete.steps": [
"features/steps/*.js"
],
"cucumberautocomplete.syncfeatures": "features/*.feature",
"cucumberautocomplete.strictGherkinCompletion": true,
"settings": {},
"folders": [
{
"path": "/Users/{me}/Documents/{project folder}/{project name}"
}
]
}
Package.json
"scripts": {
"debug": "node --inspect=1337 --debug-brk --nolazy node_modules/cucumber/bin/cucumber-js --tags #debug --format json:./reports/report.json",
CucumberTest.feature
#debug
Scenario: Validate I can get debug working
This works
{
"name": "DebugMode",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/cucumber/bin/cucumber-js",
"args": [
"${workspaceRoot}/features/*.feature",
"--tags", "#debug"
]
}
Here's the simplest way I've found to run Cucumber.js in the VS Code debugger:
Set JavaScript debugger auto attach to "onlyWithFlag" (Ctrl+Shift+P, type "Toggle Auto Attach")
Run Cucumber.js as follows: node --inspect ./node_modules/.bin/cucumber-js <args...>
For convenience, set an NPM run script in your test project for "debug" so you can run this as npm run debug -- <args...>
with the latest Cucumber, Playwright, typescript as of January 2023 - F5 (run in VSCode) - set debugger in ts step files and use .vscode/launch.json (you might tweak your reports location)
{
"version": "0.1.0",
"configurations": [
{
"name": "debugMode",
"type": "node",
"request": "launch",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"program": "node_modules/#cucumber/cucumber/bin/cucumber-js",
"args": [
"./features/*.feature",
"--require-module",
"ts-node/register",
"--require",
"./steps/*.steps.ts",
"--tags",
"#demoX",
"--format", "progress",
"--format", "json:./Reports/cucumber_report.json"
]
}
]
}