How to configure Mocha in VSCode for debugging - mocha.js

I have a simple LinkedList implementation in Node. I want to test it using Mocha -- simply exercise different append/delete operations. And most importantly, I want to be able to stepthrough/debug my linkedlist as called from the mocha tests.
This is my launch.json:
{
"version": "0.1.0",
"configurations": [
{
"type": "node",
"request": "launch",
"protocol": "inspector",
"name": "Mocha All",
"windows":{
"runtimeExecutable": "c:\\Users\\alern\\AppData\\Roaming\\npm\\_mocha.cmd"
},
"args": [
"--colors",
"${workspaceFolder}\\test\\test.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
when I press Run on my "Mocha All" configuration in VSCode,
global installation of mocha.cmd starts running, I see the following in the terminal:
cd 'd:\Projects\Algorithms\LinkedList'; & 'c:\Users\alern\AppData\Roaming\npm\_mocha.cmd' '--inspect-brk=30840' '--colors' 'D:\Projects\Algorithms\LinkedList\test\test.js'
(node:21524) ExperimentalWarning: The ESM module loader is experimental.
The above makes sense.
And then I see:
all of my tests simply rip through --- although I have setup some breakpoints in the Mocha scripts, they get ignored. So thats useless.
once all the tests are done (some successful, some not), at the end I get a popup that says:
"Cannot connect to runtime process, timeout after 10000ms - reason: Cannot connect to target: connect ECONNREFUSED 127.0.0.1:30840" This error feels like my process is done running and gone away, and VSCode or debugger are trying to connect to it?
In any case, what do I tweak to have my breakpoints stop execution? Thank you

The first thing I might try is to change the type to "pwa-node". I think mocha or VSCode recently made some changes that make that mandatory. You might also not want to set the runtime executable, but rather the "program". Generally the runtimeExecutable will be node, and if it's on your path VSCode should be able to find it automatically.
If all else fails I'd also try looking at the default configuration that ships with VSCode and see if that gives you any clues. That can be accessed by adding a new configuration through the "Add Configuration..." button. https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configurations-for-common-scenarios

Related

The Debugger in VS Code is starting but closing right after with no Terminal outputs

My Setup is the following:
I use a quite big python repository and code with VS Code with the following setup:
Version: 1.66.0 (user setup)
Commit: e18005f0f1b33c29e81d732535d8c0e47cafb0b5
Date: 2022-03-30T05:50:14.623Z
Electron: 17.2.0
Chromium: 98.0.4758.109
Node.js: 16.13.0
V8: 9.8.177.11-electron.0
OS: Windows_NT x64 10.0.19041
Since yesterday the debugger doesn't start anymore. When I start it with the buttons or with F5, the debug toolbar shows up for a second and then it vanishes again. The normal run works perfectly, as well as when I run my file from a terminal.
I didn't really change something that I can remember, only that VS Code did an automatic update on Monday evening.
There is no Terminal output that I can show, since it is not opening a Debugging Console.
I used the debugger always directly from the file that I needed to debug and didn't specify any launch.json files.
What I already tried, is as mentioned using not the Debugger, but the Run command and started the files from a terminal outside of VS Code. Then my code runs as intended. But since I need the debugger for my work, this is not a good solution.
I also went back a few commits to the last step where I know it was working, but this didn't change it at all.
I tried to debug a simple "hello World" python script but there I have the same problem.
I used the automatic add configuration from VS Code and it added the following launch.json file to the .vscode folder in the repo. Together with this file it added a configuration called "Python: Aktuelle Datei" (german for current file). This also didn't help when I'm trying to launch it with this configuration.
{
// 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: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
Is there anyone who encountered a similar issue?

How to know the run command that vscode creates from launch.json?

I have a launch.json like below:
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": ["server"]
}
]
}
When I launch it using the debug view on VScode it works fine but I need to change the program attribute to relative path of the respective file from my workspace location, strange behaviour is observed, other components in the same package start throwing undefined func error. Probably something goes wrong with current working dir or go module setup.
In order to investigate further, I need to know the command that is generated from this launch.json file. It should be something like go run ...
I have checked the output and debug console, both of them shows nothing about the launch command.
If you know how to see launch command, please help.
You can find the generated command in Visual Studio Code's debug output. When you have started a debug session, open the Debug Console by clicking on the Debug Console icon in the View Bar or by pressing Ctrl + Shift + Y. The debug output will include the generated command line.

Debugger runs for 1 or 2 seconds and disappears... not working

I'm trying to get the debugger working in vscode and it is failing to run. I have tried using the debugger on a normal python file and also in my Django project. Please help
//launch.json
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
I had the exact same problem after vs code updated to 1.39. I tried creating a new launch.json and all the normal restarts (computer, vs code etc...). Nothing was working so I started the process of downgrading vs code. I went into settings and changed the auto-update settings to "manual". This caused vs code to restart. Once it did this, the debugger problem went away. Not sure why this was related to the debugger, but it solved my problem.

Remote Debugging Go REST Endpoints

I am new to Go and new to VSCode. I have also never used Delve before. I am trying to set up remote debugging but i can't seem to hit breakpoints.
My project hosts REST endpoints on localhost:8080. What should the launch.json file look like in order to have delve attach and listen so that I can put breakpoints on my REST endpoints? Currently, this is what i have:
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "remote",
"remotePath": "",
"port": 8080,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": [],
"showLog": true
}
Thanks in advance!
This article mentioned:
Unfortunately, you won’t be able to debug an application when running with buffalo dev. You’ll need to build an executable that skips compiler optimizations like function invocation inlining.
If you skip these build flags Gogland won’t stop on your break points.
More on that on "Debugging Go Code with GDB ".
The code generated by the gc compiler includes inlining of function invocations and registerization of variables. These optimizations can sometimes make debugging with gdb harder.
After that, you can follow "Remote Debugging"
To remote debug using VS Code, you must first run a headless Delve server on the target machine. For example:
$ dlv debug --headless --listen=:2345 --log
Then your launcher can apply.

Cannot disable optimizations while compiling runtime

When I use VSCODE in my Ubuntu 16.10 to compile my go project, it can't succeed and prompt:
compile: cannot disable optimizations while compiling runtime
exit status 2
Process exiting with code: 1
I checked my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {
"GOPATH":"/home/bill/test/go",
"GOROOT":"/usr/local/go"
},
"args": [],
"showLog": true
}
]
}
What can I do to change this?
This error message seems to come from the Go compiler due to this change which fixes this bug
(the "compile: " prefix is apparently added by vscode).
My take on the reason is as follows:
You have Go runtime source code somehow modified since before you started to experience the present problem.
An attempt to build your program detects the runtime has changed
and needs to be rebuilt as well—simply because parts of it are included
into any program built with Go.
As to how to solve this, I have no clean idea.
Supposedly running
$ cd /usr/local/go/src
$ ./make.bash
should do it.
On a side note, you must not set the GOROOT env. variable
by hand—please leave it to the Go suite instead; since many versions ago,
it knows its GOROOT automatically based on where the go binary
is located.

Resources