VSCode debug Python module as child process - vscode-debugger

I would like to debug a Python module in VSCode. The module relies on some secrets as environment variables that are retrieved by Berglas from GSM.
berglas exec -- my_module spawns a child process with secrets populated in the child's environment and runs my_module.
Applied to VSCode my launch.json with Berglas looks like this:
{
"configurations": [
{
"name": "Foobar",
"type": "python",
"request": "launch",
"module": "berglas exec -- my_module",
"envFile": "${workspaceFolder}/.env",
"console": "integratedTerminal",
"justMyCode": true,
},
]
}
# .env
MY_GSM_SECRET=sm://myproject/my-gsm-secret
The error I receive makes sense because Berglas is not the Python module.
No module named berglas exec -- my_module
How do I debug the Python module in VSCode while still using Berglas to retrieve and inject the secrets as environment variables at runtime?

Related

How to set ROS_PACKAGE_PATH environment variable before starting debug environment in VSCode?

I would like to add /home/sanya/work/ORB_SLAM3/Examples/ROS to the ROS_PACKAGE_PATH variable before the ROS debug session starts. If it isn't added, the debugger won't start, because it cannot find the package.
I would like to export the variable before the ROS debug starts.
What I already tried:
I tried to add it as an environment variable in the launch.json in two different ways:
first way:
{
"name": "ROS: Launch + build (release)",
"type": "ros",
"request": "launch",
"target": "/home/sanya/work/ORB_SLAM3/Examples/ROS/ORB_SLAM3/launch/ORB_SLAM3_bag.launch",
"env": {"ROS_PACKAGE_PATH": "/opt/ros/melodic/share:/home/sanya/work/ORB_SLAM3/Examples/ROS"}
}
second way:
{
"name": "ROS: Launch + build (release)",
"type": "ros",
"request": "launch",
"target": "/home/sanya/work/ORB_SLAM3/Examples/ROS/ORB_SLAM3/launch/ORB_SLAM3_bag.launch",
"environment": [{"name": "ROS_PACKAGE_PATH", "value": "/opt/ros/melodic/share:/home/sanya/work/ORB_SLAM3/Examples/ROS"}]
}
I tried to add it to an .env file, and set the envFile property in launch.json:
.env file:
ROS_PACKAGE_PATH=/opt/ros/melodic/share:/home/sanya/work/ORB_SLAM3/Examples/ROS
launch.json file:
{
"name": "ROS: Launch + build (release)",
"type": "ros",
"request": "launch",
"target": "/home/sanya/work/ORB_SLAM3/Examples/ROS/ORB_SLAM3/launch/ORB_SLAM3_bag.launch",
"envFile": "${workspaceFolder}/.env"
}
I tried adding a prelaunchTask:
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:/home/sanya/work/ORB_SLAM3_multi/Examples/ROS
This option works— the export happens (I ran echo $ROS_PACKAGE_PATH after to check)— but the debug session starts in a different terminal (if I understand correctly) and the variable won't be set to the correct value in the debug session.
The only thing that has worked is adding it to the .bashrc file. This isn't really ok for me, because I have a modified version of the same library, and I would like to use them both (I will benchmark the modified version against the original), and I wouldn't like to modify the .bashrc file everytime when switching between the 2 versions.
Is there another option to export variables to the VSCode debug environment?
(I maintain the Microsoft VSCode ROS extension.)
The extension inherits most of the environment from the ROS environment it is launched from, so if you need to set environment variables (or relocate ROS), you can set them in a terminal and launch code from the terminal.
I create a feature request - https://github.com/ms-iot/vscode-ros/issues/646, and assigned it to our 0.8.0 which I'm working on now.

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

Configuring a custom C++ build and GDB debug in Visual Studio Code for Linux

I recently have been using GDB to debug a C++ program. For standard usage, I usually do:
$ cd compiledir
$ compilescript
$ gdb compiled.out
$ run inputfile
compilescript is a program that compiles the code for the particular software I am working on, and only works in compiledir. It reads an external file for compiler flags. For gdb, I include the neccisary -g flag. This works for me to debug via the text interface. However, this text interface is becoming increasingly frustrating to use compared to a IDE, and I know that Visual Studio uses gdb as a backend to debug C++ files by default.
To get started, I let visual studio generate default C++ debugging configurations and tried to change the commands, but I have no idea what it's doing, and there doesn't appear to be much documentation on making custom build/debug configurations, particularly for for the linux version of VScode.
Currently I have:
launch.json (default)
{
// 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": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "Custom Build",
"command": "compilescript" ,
"options": {
"cwd": "compiledir"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compile using the custom build script."
}
],
"version": "2.0.0"
}
Running this task, and running it indirectly through the launch.json file both fail. I get the following:
> Executing task: Custom Build<
Starting build...
and it hangs there. I tried to put echo into the build command to see if it was even running, or have it create a file. However, it's like nothing is being run at all and I can't see any printing or files being created anywhere.
What is the proper way to create a custom GDB build/debug task for Visual Studio (Linux Edition)?
Update
I've edited both tasks.json and launch.json, and am able to compile successfully and run GDB with the inputfile I want. However, the environment variables are not configuring properly. I need environment variables to be configured to properly run the inputfile. I currently have:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": compiled.out,
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [{"VAR1": "VAR1_VALUE", "VAR2": "VAR2_VALUE"}],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
However, when I run the build/debug task, it builds, but doesn't debug, and GDB complains of an error. It appears the "environment" variable is setting something, but I get the following error:
Unable to start debugging. Unexpected GDB output from command "-interpreter-exec console "set env"". Argument required (environment variable and value).
This is how you can set up debugging in C++ without using any Task extensions. Only the launch.json file will be needed.
First, compile your program with the debug flag: -g, and keep it anywhere in your folder.
For example in your active folder (or workspace folder which you have currently opened), Let's say only your source code is present
Program.cpp
# Other files...
Compile your program using the -g flag and create an executable as shown:
gcc -g Program.cpp -o a.out
If your compilation is successful, the executable a.out will be created in the folder, and your root folder will look like
a.out
Program.cpp
# Other files...
Next, you need to create your launch.json file. Since you are not using any Tasks extensions, you can choose the default configuration which will create the said file with most properties filled with their default values.
Now edit the program path in the launch.json to the path of this executable. I've also seen that there's a bug for some Linux OSes for VS Code's C++ debugger that does not work with the internal console. So you might also need to set the "external console" option as true
in this case, you'll make two edits in your launch.json file as
"configurations": [
{
#.... Other stuff
"program": "${fileDirname}/a.out",
#.... Other stuff
"externalConsole": true,
#.... Other stuff
}
]
}
That's all you need. Set breakpoints in your program, and start debugging using the button in the Debug Panel. An external console window will pop up running your program, and the breakpoints should be working.
Also if you're new to debugging in general, remember that every time you change your source code (Program.cpp in this case), you will need to compile and build your debuggable executable again. If you do not do so, your debugging would be glitchy.
Edit:
I see you want to add environment variables. I have a hunch on what you're doing wrong.
It seems to me that you want to set two variables such that
VAR1 = VAR1_VALUE
VAR2 = VAR2_VALUE
Your syntax for this in your launch.json file is incorrect. It should be as shown:
#... Other stuff
"cwd": "${fileDirname}",
"environment":
[
{
"name": "VAR1", "value": "VAR1_VALUE"
},
{
"name": "VAR2", "value": "VAR2_VALUE"
}
],
"externalConsole": false,
#... Other stuff

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

can't debug bash scripts using VSCode

I've installed VSCode with Bash debug extension.
Before that, I've installed bashdb and I've verified its version using (bashdb --version) and it's 4.4.
Now, the extension creates an empty file called launch.json.
I wrote the following to start debugging, but still, nothing happened
{
"version": "0.2.0",
"scriptPath": "${command:SelectScriptName}",
"configurations": [
],
"compounds": [
{
"type": "bashdb",
"name": "Compound",
"configurations": []
}
]
}
What should I do to enable debugging?
Regards,
With the bashdb extension installed, add the following block to the launch.json file. This configuration allows you to debug the script you have currently open on VS Code. So, in order to start debugging a script you must have it open on VS Code, and type F5 to start debugging it. Alternatively, and also with the same script open on VS Code, you can open the Run and Debug menu, on the VS Code left bar, and click Play.
{
"type": "bashdb",
"request": "launch",
"name": "Bash-Debug (simplest configuration)",
"cwd": "${workspaceFolder}",
"program": "${file}",
"showDebugOutput": true,
"terminalKind": "integrated"
}
Adding the args parameter to the above block, allows you to pass an array with arguments to the script while debugging.
I tested this code with the Bash Debug extension:
{
"version": "0.2.0",
"configurations":
[
{
"type": "bashdb",
"request": "launch",
"name": "Bash-Debug (select script from list of sh files)",
"cwd": "${workspaceFolder}",
"program": "${command:SelectScriptName}",
"args": []
}
]
}
Visual Studio Code displays a list of script from your project, and you can pick the one you want to run. You can alternatively choose a "Bash-Debug (hardcoded script name)" configuration, which allow to hardcode the script path.
If you want to debug a Bash script I recommend you execute your script with -x flag. For example:
bash -x script.sh
or into the script add:
set -x
<DEBUG_CODE_LINES>
set +x

Resources