How to set and use a random number in launch.json - visual-studio-code

TL;DR
I'm looking for a way to set and use a random environment variable each time I launch the debugger. Specifically, I'd like to be able to use a random port number for the GDB server and client. My configuration currently looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "app",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/app/app.elf",
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"MIMode": "gdb",
"miDebuggerServerAddress": "localhost:3333",
"miDebuggerPath": "/home/me/intelFPGA/20.1/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/bin/nios2-elf-gdb",
"debugServerPath": "/home/me/intelFPGA/20.1/quartus/bin/nios2-gdb-server",
"debugServerArgs": "--tcpport 3333 --reset-target --tcptimeout 5",
}
]
}
Context
I'm using the Nios ii Embedded Design Suite via Visual Studio Code in order to avoid using Eclipse. Everything works quite nicely via vscode including debugging, however, there's an annoying bug in nios2-gdb-server; when it exits, it doesn't always kill the TCP connection. So, if you want to start a new debug session shortly thereafter, it will fail as the port is still in use (the port eventually closes after a few minutes). The Eclipse side of the tools avoids this by always using a random port for each debug session. I'm trying to find a convenient way to replicate that in vscode.

You can use extension Command Variable v1.42.1
Use the commands: extension.commandvariable.number, and extension.commandvariable.remember
In the example I used a random port number that is different from the 10 previous numbers.
Depending on which string is evaluated first by VSC you might need to swap the 2 ${input} variables.
{
"version": "0.2.0",
"configurations": [
{
"name": "app",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/app/app.elf",
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"MIMode": "gdb",
"miDebuggerServerAddress": "localhost:${input:randomPort}",
"miDebuggerPath": "/home/me/intelFPGA/20.1/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/bin/nios2-elf-gdb",
"debugServerPath": "/home/me/intelFPGA/20.1/quartus/bin/nios2-gdb-server",
"debugServerArgs": "--tcpport ${input:rememberRandomPort} --reset-target --tcptimeout 5",
}
],
"inputs": [
{
"id": "randomPort",
"type": "command",
"command": "extension.commandvariable.number",
"args": {
"name": "randomPort",
"range": [1500, 60000],
"random": true,
"uniqueCount": 10
}
},
{
"id": "rememberRandomPort",
"type": "command",
"command": "extension.commandvariable.remember",
"args": { "key": "number-randomPort" }
}
]
}

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.

Stuck waiting for prelaunch task

I'm trying to run my debug environment as a Poetry session, so I can properly debug encapsulated.
So, my launch.json is straightforward:
{
"name": "Poetry",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5710
},
"preLaunchTask": "poetryDebugSession",
"localRoot": "${workspaceFolder}"
}
and I adapted my tasks.json to try to launch debugpy in Poetry first (this works if I manually run some code in the terminal)
{
"label": "poetryDebugSession",
"type": "shell",
"command": "poetry",
"args": [
"run",
"python",
"-m",
"debugpy",
"--log-to-stderr",
"--wait-for-client",
"--listen",
"5710",
"${relativeFile}",
"&"
],
"presentation": {
"panel": "dedicated",
"clear": true
},
"group": "test",
"isBackground": true,
"runOptions":{
"instanceLimit": 1
},
// This task is run before the launch.json task. Since it needs to run in the
// background and not wait for completion, though, we need to jump through hoops
"problemMatcher": [
{
"owner": "python",
"fileLocation": "absolute",
"pattern": [
{
"regexp": "^\\s+File \"(.*)\", line (\\d+), in (.*)$",
"file": 1,
"line": 2
},
{
"regexp": "^\\s+(.*)$",
"message": 1
}
],
"background": {
"activeOnStart": true,
"beginsPattern": "^D[0-9\\.: \\+]+wait_for_client",
"endsPattern": ".*",
}
}
]
}
When I start debugging, the task is properly launched, and debugpy gets all the way to the message I am waiting for that I want the preluanch task to be marked as "ready":
> Executing task: poetry run python -m debugpy --log-to-stderr --wait-for-client --listen 5710 d:\path\to\myfile.py <
# stuff
I+00000.344: pydevd is connected to adapter at 127.0.0.1:61443
D+00000.344: wait_for_client()
I could have sworn I had this working last week but as of 1.58.2 after a restart this morning it doesn't progress past wait_for_client() display, so the debugger never attaches. I'm also a little suspicious that ${relativeFile} includes a full path in my output but that probably doesn't matter.
It should be clear from the code above that I derived my initial implementation from https://stackoverflow.com/a/54017304/1877527 , but still no dice.
The debugpy team helped me find an answer:
https://github.com/microsoft/debugpy/issues/676#issuecomment-886041838
{
"name": "Python: Poetry current file",
"type": "python",
"request": "launch",
"program": "${env:USERPROFILE}/.poetry/bin/poetry",
"python": "<path/to/bare/bones/python>",
"args": ["run", "python", "${file}"],
"console": "integratedTerminal",
}
It worked for me even without the python argument, since my primary dev machine just has one active Python installation.

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

VSCode debugger not working in Jest tests

I'm struggling to get the Visual Studio Code debugger working in with Jest tests.
Here is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"sourceMaps": true
}
]
}
Here are my Jest tests with a couple of breakpoints:
When I hit the green play button to run the tests with the debugger, the breakpoints are never hit.
Any help would be appreciated
Personnally I use this configuration
{
"name": "Launch e2e test",
"type": "node",
"request": "launch",
"env": {
"NODE_ENV": "test"
},
"args": [
"--colors",
"--config=${workspaceFolder}/jest-e2e.config.js",
"--runInBand",
"--coverage"
],
"runtimeArgs": [
"--nolazy"
],
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
},
"outputCapture": "std",
"internalConsoleOptions": "openOnSessionStart"
}
Change jest-e2e.config.js by your configuration file. And remove or keep coverage
Like Laura Slocum said you will certainly have problem with line number. In my case personnaly think that the problem come from the jest configuration, the transform :
transform: {
"^.+\\.(t|j)s$": "ts-jest"
},
This configuration let's me debug the jest test. Unfortunately hitting a breakpoint in the component does not show the correct line, even though it is stepping through the correct code. I believe this is probably a VSCode error though
{
"name": "Jest", // This is the configuration name you will see in debug sidebar
"type": "node",
"request": "launch",
"port": 5858,
"address": "localhost",
"stopOnEntry": false,
"runtimeExecutable": null,
"env": {
"NODE_ENV": "development"
},
"console": "integratedTerminal",
"preLaunchTask": "compile",
"runtimeArgs": [
"--inspect-brk", // node v8 use debug-brk if older version of node
"${workspaceRoot}/node_modules/.bin/jest",
"--watch",
"--bail",
"--runInBand"
],
"cwd": "${workspaceRoot}"
},
I had the same problem with line numbers being off. In the source file I had almost 30 lines of requires, and the test file that loaded in the debugger added a blank space between each require. So the file that got loaded in vscode was about 60 lines longer.
I found this post that fixed my problem: Debugging Jest Tests in VS Code: Breakpoints Move
The problem for me was the value of the program attribute in launch.json. If your launch.json is as follows:
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
Check if ${workspaceFolder}/node_modules/jest/bin/jest is actually valid. For me, the node_modules did not exist here, but in a subdirectory of workspaceFolder.
The following is the only launch.config that worked for me after trying out everything else :|
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
"-i"
],
"skipFiles": [
"<node_internals>/**/*.js", "node_modules",
]
}
]
}
If you are using transformers like babel or swc to transform your tests before running the actual tests, the debugger in vscode may not work.
For me I'll just use the debugger.

Running two projects at once in Visual Studio Code

I develop web project. Server is node.js application written in TypeScript. Client also written in Typescript. I need two ability:
to compile different projects with different compiler options in different folders.
to debug both projects at the same time.
How can I do this?
See our documentation on multitarget debugging: https://code.visualstudio.com/Docs/editor/debugging#_multitarget-debugging
In your launch.json, just create a compounds section that contains the targets you want to debug
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Server",
"program": "${workspaceRoot}/server.js",
"cwd": "${workspaceRoot}"
},
{
"type": "node",
"request": "launch",
"name": "Client",
"program": "${workspaceRoot}/client.js",
"cwd": "${workspaceRoot}"
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": ["Server", "Client"]
}
]
}