How to debug Go tests with param "./..." in VS Code - go

I usually run go test ./... to run all tests in my project.
How can I set up launch.json to debug every tests that normally go test ./... runs?

Assuming you're using the Go extension for VSCode:
As the vscode-go's documentation says, you could use the following:
{
"name": "Launch test package",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}"
}
Note that you're specifying a new mode called "test".
You have to install delve in order to debug code using VSCode. You can install it by yourself or use the Go: Install/Update Tools command from VSCode. Read the documentation I mention first for more information.

This one should work
{
"name": "Launch test package",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/controllers"
}

Related

Cannot debug Rust in Visual Studio Code?

I am trying to debug a Rust program in VS Code, but I get an error:
After clicking OK, VS Code opens "settings.json":
I have these extensions installed:
My program is a simple "hello world" app.
Unfortunately VS Code can't debug Rust out of the box :( But no need to worry, just few steps of configuration will do the work :)
Steps
Install C/C++ extension if you are on windows and CodeLLDB if on OS X/Linux
Click Debug -> Add Configuration, a launch.json file should open, you need to change the program name here manually
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/target/debug/foo.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true
},
{
"name": "(OSX) Launch",
"type": "lldb",
"request": "launch",
"program": "${workspaceRoot}/target/debug/foo",
"args": [],
"cwd": "${workspaceRoot}",
}
]
}
Make sure Allow setting breakpoints in any file is checkend under File -> Preferences -> Settings
For detailed steps and more you can refer the article I used to answer this
Credits- Forrest Smith
From your screenshots, you're on Windows, so here's how to proceed. This assumes you already took care of the basics:
VsCode has the recommended rust-analyzer extension installed.
Your project folder was initialized with cargo init. (Your project's folder name must be the same as the name of your package in Cargo.toml.)
You can cargo run from within your project directory and it works.
As indicated by various locations on the 'Net, you need to install another VsCode extension to make it so you can debug. Since you're on Windows, you want to use the MS C++ DevTools extension for VsCode, instead of the CodeLLDB one.
Next, you need a "launch" configuration setup. Select VsCode's Run >>> Add Configuration... menu item. Choose the C/C++: (Windows) launch option in the drop-down. You'll now have a launch.json file with a single configuration object.
You'll need to change the program property to "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe"; this depends on your package name being the same as the project folder's name. (I also changed the cwd property to "${workspaceFolder}", though I'm not sure it matters.) To be clearer, here's the configuration I have presently in my launch.json file (the preLaunchTask property is for later):
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
"preLaunchTask": "rust: cargo build",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal"
}
At this point, as long as you've already built your project at least once, you can hit F5 and debug.
If you want F5 to also save your changes and rebuild your project before debugging, then you also have to add a build task and configure it to run before debugging starts.
To do that, add the build task by opening the Show All Commands box (either F1 or Ctrl+Shift+p) and choosing Tasks: Configure Task. Select rust: cargo build. It'll create a tasks.json file next to your launch.json; the defaults are all you need. My file looks like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "cargo",
"command": "build",
"problemMatcher": [
"$rustc"
],
"group": "build",
"label": "rust: cargo build"
}
]
}
Then, to hook everything up, you just need to manually add the preLaunchTask property to your launch configuration with a value equal to the label in your task. E.g. "preLaunchTask": "rust: cargo build",, like what I have in my example launch.json up above.
At this point, whenever you press F5, VsCode will save your work, rebuild your project, then start debugging it.
Visual Studio Code is a general editor, but it can be configured to debug rust code.
Step 1.
Assuming that Visual Code, rust and cargo are installed, the first step is to enable the required extensions:
rust-analyzer
CodeLLDB
These only need to be installed one.
Step 2
The second step is to create the rust code. I have a folder called Rust, in which I keep the rust code. Changing to that folder, I use cargo new hello_world to create a new rust project.
Step 3
The third step is to change the folder for the project. There are two plausible options, but only one of them will work.
I changed to my Rust folder, and then I can edit the source code by following ``hello_world - src`.
To debug the code, it is necessary to create a launch.json file, using Run - Add configuration... However, the file isn't correct, with <your program> where the correct name should be. This is the wrong approach.
{
// 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": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/<your program>",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
The documentation is a bid thin at this point. The correct approach is to pick a different folder, the top level of the project hello_world. The Cargo.toml file is available.
Now, when Run - Add configuration... is used, and the option of LLDB is selected -
the Cargo.toml file can be picked up -
and then the Cargo.toml file is used to correctly build the launch.json file -
{
// 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": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'hello_world'",
"cargo": {
"args": [
"build",
"--bin=hello_world",
"--package=hello_world"
],
"filter": {
"name": "hello_world",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'hello_world'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=hello_world",
"--package=hello_world"
],
"filter": {
"name": "hello_world",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
Now, both Run - Start debugging and Run - Run without Debugging both work properly.

how to debug golang cobra cli app in vscode

I have a golang cobra cli app. have configured my vscode for debugging. I want to debug a specific command using vscode for my application.
I am using this launch.json
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}"
}
If I select main.go and start debugging it just prints the help for my command. How can I debug a particular cli subcommand in vscode? Like say abc create or abc version
If I select a subcommand package and then debug it says : Failed to launch :could not launch process:not an executalbe file
You can configure the VSCode launch configuration to pass arguments using:
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}"
"args": ["arg1", ...]
}
Note that you may prefer to write the path to your main go file in the program field so that you can run/debug no matter what file you are currently on.
Reference:
launch.json: https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
VSCode Variables: https://code.visualstudio.com/docs/editor/variables-reference

VSCode run all go tests in folder with launch configuration

I'm trying to make a launch configuration that will run all of the go tests I have in a specific folder in my repo.
I can successfully run go test ./src/... in the terminal to run all the tests I care about but I'm having trouble replicating that in a VSCode launch configuration.
Here's my current launch configuration:
{
"name": "run tests",
"type": "go",
"request": "launch",
"mode": "test",
"args": ["./src..."],
"program": "${workspaceFolder}",
}
It seems that using ./src/... as args doesn't behave as I expect it to. Using this launch configuration I get an error that:
no Go files in /home/paymahn/gadic/backend
exit status 1
Process exiting with code: 1
Is there a way to replicate go test ./src/... as a VSCode launch configuration?
When I try to run a simple test I use this setting, reference here.
{
"name": "Tests",
"type": "go",
"request": "launch",
"mode": "test",
"remotePath": "",
"program": "${fileDirname}",
"env": {},
"args": ["^TestGenerateConfigurationFailure$"]
}
And about the specific folder, I understand that the Golang test tool inspects the "test" module package. No, the specific regex name folder for the Golang test tool.
You can check more info in official debugging VS-Code for Golang HERE

How to debug app via rake test with VSCode

Trying to debug a minitest test on a non-rails ruby application and I have the following config in the launch.json file:
{
"name": "Debug a rake task",
"type": "Ruby",
"request": "launch",
"useBundler": true,
"cwd": "${workspaceRoot}",
"showDebuggerOutput": true,
"pathToBundler": "/home/user/.rbenv/versions/2.5.5/bin/bundle",
"pathToRDebugIDE": "/home/user/.rbenv/versions/2.5.5/bin/rdebug-ide",
"program": "/home/user/.rbenv/versions/2.5.5/bin/rake",
"args": [
"test",
],
"env": {
"LOG_LEVEL": "debug",
"TEST": "test_file.rb",
"TESTOPTS": "--name=test_name",
}
},
Even though the breakpoints of the Rakefile are hit, neither the breakpoints of test_file.rb nor the breakpoints within the app are hit.
I suspect this might be because rake task are called as separate processes but I couldn't figure out how to handle this case. How can I make the debugger stop at the breakpoints of the app and the test files?
PS: I use byebug at the moment; however, it doesn't replace a IDE based debugger.

Debugging MSTest Unittests in Visual Studio Code

I am trying to use Visual Studio Code to Debug a MSTest unit test project. But the tests just run and the breakpoint is never reached.
Here is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Test (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "C:\\Program Files\\dotnet\\dotnet.exe",
"args": ["test"],
"cwd": "${workspaceRoot}",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
How can I debug a unit test (MSTest)? This same problem exists for XUnit.
If you are using the latest version of VS Code (I'm using v1.29.0), debugging unit test is in-built feature.
You need to first build the solution dotnet build for the test run & debug options to appear.
Try https://github.com/Microsoft/vstest-docs/blob/master/docs/diagnose.md#debug-test-platform-components (assumes you're using dotnet-cli tools 1.0.0)
> set VSTEST_HOST_DEBUG=1
> dotnet test
# Process will wait for attach
# Set breakpoint in vscode
# Use the NETCore attach config from vscode and pick the dotnet process
Try and reload VS Code, worked for me.
Press Ctrl+Shift+P which opens the command palette and then :
Reload Window
Before building, remember to include in your .csproj file
<GenerateProgramFile>false</GenerateProgramFile>
or else it will not know what to run...
Program.cs(160,21): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. [/Users/.../Fraction.csproj]

Resources