Related
I'm trying to setup my debug environment on VS Code to run and debug a MERN app.
I currently have this launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Node: Nodemon",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"dev"
],
"outputCapture": "std",
},
]
}
It's working just fine, but with it I can only have breakpoints in the backend and not in the React app on the frontend.
The script yarn dev (package.json in the backend) runs both backend and frontend with concurrently:
Here's my scripts in package.json in the backend:
"scripts": {
"start": "node backend/server.js",
"server": "nodemon backend/server.js",
"client": "yarn --cwd frontend/ start",
"dev": "concurrently \"yarn server\" \"yarn client\""
}
What would be a working launch.json that would allow me to have breakpoints in the frontend as well?
One way to do this is to use VS Code's Multi-target debugging:
https://code.visualstudio.com/docs/editor/debugging#_multitarget-debugging
"Using multi-target debugging is simple: after you've started a first debug session, you can just launch another session. As soon as a second session is up and running, the VS Code UI switches to multi-target mode".
If you prefer to use launch.json, you can create a compound launch configuration, like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Node: Nodemon",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"dev"
],
"outputCapture": "std",
},
{
"type": "node",
"request": "launch",
"name": "Client",
"program": "${workspaceFolder}/client.js"
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": [
"Node: Nodemon",
"Client"
],
"preLaunchTask": "${defaultBuildTask}",
"stopAll": true
}
]
}
The configurations will be launched in parallel.
I'm trying to debug my strapi project (3.0.0 beta 16.6) in VS Code.
My launch.json:
{
"type": "node",
"request": "attach",
"name": "Attach to strapi",
"port": 9229
}
My package.json:
"scripts": {
"debug": "node --inspect=127.0.0.1:9229 ./node_modules/strapi/bin/strapi.js develop"
}
Debugger attaches to the process, but all my breakpoints become unverified (appear black, not red). What's wrong with my configs?
this answer come from the following strapi/strapi issue:
I have come up with next solution:
having next script in server.js file (my custom one):
const strapi = require('strapi');
strapi({ dir: process.cwd(), autoReload: true }).start();
I'm using nodemon by next command: nodemon --inspect=0.0.0.0:9228 server.js
Now I can attach to 9228 by debugger.
Just to add to #alxnkt comment.
I have encountered the same and solved it by changing the launch.json to port 9230
{
"type": "node",
"request": "attach",
"name": "Attach to strapi",
"port": 9230
}
while keeping the port on package.json as 9229
"debug": "node --inspect=127.0.0.1:9229 ./node_modules/strapi/bin/strapi.js develop"
There are somehow 2 processes running when calling the Strapi develop command (possibly the admin panel and its core server), the process we have to monitor becomes port 9230 instead.
Add VS configuration "NODE: launch via npm" in .vscode/launch.json "configurations" property and run
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"develop"
],
"port": 9229,
"skipFiles": [
"<node_internals>/**"
],
"console": "integratedTerminal"
}
This worked for me using Strapi v4
{
"version": "0.2.0",
"configurations": [
{
"name": "STRAPI Debug",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "node",
"runtimeVersion":"14.19.0",
"runtimeArgs": ["--lazy"],
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceRoot}/node_modules/#strapi/strapi/bin/strapi.js",
"args": [
"develop"
],
"protocol": "inspector",
"env": {
"NODE_ENV": "development"
},
"autoAttachChildProcesses": true,
"console": "integratedTerminal"
},
]
}
The problem has been solved by setting port number to 9203:
{
"type": "node",
"request": "attach",
"name": "Attach to strapi",
"port": 9229
}
But I have no idea about HOW it works...
I just launched it via NPM, here is my launch.json (in .vscode folder)
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"develop"
],
"port": 9229,
"skipFiles": [
"<node_internals>/**"
],
"console": "integratedTerminal"
}
]
}
You can also use NODE_OPTIONS:
NODE_OPTIONS='--inspect' yarn strapi develop
You'll get:
$ NODE_OPTIONS="--inspect" yarn strapi develop
Debugger listening on ws://127.0.0.1:9229/8564942a-6476-443a-9f64-87fa6d0055b7
For help, see: https://nodejs.org/en/docs/inspector
yarn run v1.22.5
$ strapi develop
Starting inspector on 127.0.0.1:9229 failed: address already in use
Debugger listening on ws://127.0.0.1:9230/7da840dd-cb89-493f-8ce0-e11530efdfbb
For help, see: https://nodejs.org/en/docs/inspector
Now you can use Debug: Attach to Node Process to attach to port 9230.
For strapi v4 you will have to add "sourceMap": true to compilerOptions of tsconfig.json file.
How to launch jest in debug mode on the current opened file, and only this one, regardless of the OS (windows, linux, mac).
The problem:
When using vscode under windows, it is impossible to jest the current (active) file, and only this one.
This debug config (from https://github.com/microsoft/vscode-recipes/tree/master/debugging-jest-tests):
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"${fileBasenameNoExtension}",
"--config",
"jest.config.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
Will not run only the active test if you have several tests with the same name (like index.test.js)
If I replace
"args": [
"${fileBasenameNoExtension}",
....
]
by
"args": [
"${file}",
...
]
It will not work at all as the first argument jest expects is a regex and ${file} returns the absolute path of the file, which, on a windows machine will contain \ backslashes which in turn will be interpreted as escaping the following chars in the pattern.
After reading carefully Jest's doc, it's impossible to specify a single file.
And VSCode is unable to convert a variable to a regexp.
The solution from this question, although almost similar, will not work either as it fails with files of the same name (case 1).
So either more tests than expected are run, or no test at all is run.
I've found a solution involving a separate script but any other solution would be appreciated.
Taken from here: https://github.com/Lemoncode/jest-vs-code-debugging-example/blob/master/custom-solution-config-package-json/01-implemented/.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest single run all tests",
"program": "${workspaceRoot}/node_modules/jest-cli/bin/jest.js",
"args": [
"--verbose",
"-i",
"--no-cache"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest watch all tests",
"program": "${workspaceRoot}/node_modules/jest-cli/bin/jest.js",
"args": [
"--verbose",
"-i",
"--no-cache",
"--watchAll"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest watch current file",
"program": "${workspaceFolder}/node_modules/jest-cli/bin/jest",
"args": [
"${fileBasename}",
"--verbose",
"-i",
"--no-cache",
"--watchAll"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
The accepted answer provides launch tasks to run all tests, watch all tests or watch a single file. If you just want to run the tests in the current file then the --testPathPattern option is what you want:
{
"type": "node",
"request": "launch",
"name": "Jest debug current file",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": ["--verbose", "-i", "--no-cache", "--testPathPattern", "${fileBasename}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
This does have the problem that similarly named files may be run by mistake, as ${fileBasename} is just the filename - it doesn't match the path at all.
On reflection this isn't a terribly good answer. I'm going to leave it up, more as a warning than as something helpful.
I don't think ${relativeFile} will work as it gives the relatvie path that Jest doesn't like either.
Instead, you should use ${fileBasename}
"args": [
...,
"${fileBasename}"
]
The solution mentionned in the above question (involves a separate script):
debug configuration:
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/src/utils/betterJestCli.js",
"args": [
"${relativeFile}",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/src/utils/betterJestCli.js",
},
"cwd": "${workspaceFolder}",
},
betterJestCli.js:
const { exec } = require('child_process');
const path = process.argv[2];
const child = exec(
['"./node_modules/.bin/jest"', path.replace(/\\/g, '/'), '--config', 'jest.config.js'].join(' '),
// eslint-disable-next-line prefer-arrow-callback, func-names
function (error/* , stdout, stderr */) {
if (error) {
// eslint-disable-next-line no-console
console.log(error);
}
}
);
// to see Jest's output
child.stderr.pipe(process.stderr);
So replacing \ by / on a relative path does the trick since / has no meaning from a regex point of view (escaping the . would be a good idea too).
The biggest issue is that I lose Jest's output colors in the terminal.
It's not yet tested under different environment (linux, mac) so I don't know if it's cross env.
Any other ideas?
I am using the following debug config in VSCode to run the current jest file
{
"name": "Debug Current Jest File",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"--runTestsByPath",
"${relativeFileDirname}/${fileBasename}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
Tested it on Windows and WSL (Ubuntu-18), not sure about mac.
I seem to be missing something simple, but I can't figure out why I am unable to debug python using Visual Studio Code on Windows. I've tried setting up the debugger as shown by Microsoft, and this youtube video. I've done a clean install of Visual Studio Code (including the python extension) and Python 3.6. I have no other python versions installed. I keep getting the following errors in the Python Debugger:
cd "c:\Users\xxx\Documents\Python Scripts" ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\python.exe" "C:\Users\xxx\.vscode\extensions\ms-python.python-2018.3.1\pythonFiles\PythonTools\visualstudio_py_launcher.py" "c:\Users\xxx\Documents\Python Scripts" 53746 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput "c:\Users\xxx\Documents\Python Scripts\.vscode\launch.json"
-bash: cd: c:\Users\xxx\Documents\Python Scripts: No such file or directory
env: ‘C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe’: No such file or directory
It's complaining that it can't find the python.exe file, but it clearly can since VS code indicates that it's attached:
My launch.json file is the default, and I've tried all the configurations:
// 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": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}"
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}",
"port": 3000,
"secret": "my_secret",
"host": "localhost"
},
{
"name": "Python: Terminal (integrated)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Terminal (external)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"debugOptions": [
"RedirectOutput",
"Django"
]
},
{
"name": "Python: Flask (0.11.x or later)",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "${workspaceFolder}/app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
]
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "module.name"
},
{
"name": "Python: Pyramid",
"type": "python",
"request": "launch",
"args": [
"${workspaceFolder}/development.ini"
],
"debugOptions": [
"RedirectOutput",
"Pyramid"
]
},
{
"name": "Python: Watson",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/console.py",
"args": [
"dev",
"runserver",
"--noreload=True"
]
},
{
"name": "Python: All debug Options",
"type": "python",
"request": "launch",
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"module": "module.name",
"env": {
"VAR1": "1",
"VAR2": "2"
},
"envFile": "${workspaceFolder}/.env",
"args": [
"arg1",
"arg2"
],
"debugOptions": [
"RedirectOutput"
]
}
]
}
Pylinting works fine, and I have not custom user settings in Visual Studio Code that are related to python. I've tried setting the complete file path to python.exe in settings.json but it makes no difference.
Any help is greatly appreciated. Thanks in advance
I had the same problem but finally got out of it after several adjustment to my settings.
make sure all other settings are in place as you've mentioned especially pythonPath in settings.json and also get "python" installed from the "extension" toolbar in VS code.
Simply change your debug configuration from "Python: Scripts" (or any other configuration you've selected previously) to "Python: Terminal(external)" and you are good to go!
I know, is a pain and I'm experiencing the same.
Apparently python.exe is executing correctly. You will keep seeing that banner because "This is necessary to bootstrap the debugger."
At least this is what Microsoft says.
I can't manage to debug my Jest tests in VS Code when I use Electron. My tests should run with Electron, not with Node (due to the use of native modules).
{
"name": "Jest Unit Tests",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"program": "${workspaceRoot}/node_modules/.bin/jest",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"env": {
"ELECTRON_RUN_AS_NODE": "1",
"NODE_ENV": "test",
"BABEL_DISABLE_CACHE": "1"
},
"args": [
"-i",
"--verbose",
"-c test/config/jest.unit.json"
],
"internalConsoleOptions": "openOnSessionStart"
},
It is based on the usual config used to debug Jest with Node (which works fine), but I can't get it to work with Electron. The Jest command is correct, but the --debug-brk --inspect option added by VSCode seems to mess out with Jest.
I was able to get my config to work based on your question. Thanks!
{
"type": "node",
"request": "launch",
"name": "Debug Current Jest File:Electron",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
"--verbose",
"-i",
"--no-cache",
"--testPathPattern",
"${fileBasename}",
"--coverage",
"false"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"env": {
"ELECTRON_RUN_AS_NODE": "true"
}
}