Visual Studio Code Debug: source and then launch on same shell
Asked Answered
T

4

8

My current workflow for a project is the following:

  • build the project (via catkin)
  • source a setup.sh script (generated by catkin, which I wouldn't like to modify) setting environment variables and the names needed by my executable.
  • Run "MyProgram", which is only available after sourcing the "setup.sh" script.

I would like to be able to debug my project in Visual Studio Code. To do this, I have defined a task building the executable via catkin, named "catkin build all", and I have defined a second task as:

{
  "type": "shell",
  "label": "load programs",
  "command": "source /some_folder/setup.sh",
  "group": "build",
  "dependsOn": ["catkin build all"]
}

Which is the "preLaunchTask" of my lanuch.json launch configuration.

Launching debug will correctly compile the project, but execution fails with error "launch: program myProgram does not exist". Indeed program MyProgram can not be found if setup.sh is not sourced, but is should be sourced by the "preLaunchTask".

In my launch.json i can also set "program" to "/full/path/to/myProgram" instead of "myProgram", but in this case shared libraries are not found, as setup.sh would take care of that.

I have also tried to source setup.sh on a shell and then launch visual studio code from the same shell, but this did not solve the "launch: program myProgram does not exist" problem.

Do tasks run on different shells? How can I have the preLaunchTask running in the same shell as the subsequent program code? Or any other hint on how to get my workflow working?

Trihedron answered 8/10, 2019 at 15:6 Comment(3)
What's strange is why are you hiding that you are using ROS ? The behaviour of the "setup.sh", catkin_make and launch files, ... are ROS specific. Moreover we have no concrete information about your process. How can you expect an answer with that amount of mystery ? State exactly what you did, how do you execute your tasks (via roslaunch ? something else ?), what's the content of the launch files, ... We can't guess what you did. Moreover keep in mind that sourcing devel.sh will only affect the current shell console.Wedded
I have no experience with ros and did not setup the workflow I am using. I wrote a normaly CMake library which was then integrated into a ros/catkin project. I am not describing the ROS part because i would not know how to do it properly. I was just wondering if there is a way of having visual studio code source "setup.sh" and then run myProgram on the same shell console, instead of, as i can understand, doing it in a separate shell console.Trihedron
Ah ok, I see. I would advise you to follow the ros tutorials in order to better understand what you are doing. You can do the beginner level from step 1 to 16 (you can ignore the python parts) at least. No need to go further (but you can if you want to). This tutorial is very informative, easy to read (and to reproduce) and helped me a lot in the past when I had to deal with ROS. I bet it'll be a huge help for you too :)Wedded
D
13

My solution is to use a env_file

In one terminal, source your file such as: source /opt/ros/melodic/setup.bash

Recover the changes by using: printenv | grep melodic

Create a .env file in your repo with the environment variables; (except PWD)

LD_LIBRARY_PATH=/opt/ros/melodic/lib
ROS_ETC_DIR=/opt/ros/melodic/etc/ros
CMAKE_PREFIX_PATH=/opt/ros/melodic
ROS_ROOT=/opt/ros/melodic/share/ros
PYTHONPATH=/opt/ros/melodic/lib/python2.7/dist-packages
ROS_PACKAGE_PATH=/opt/ros/melodic/share
PATH=/opt/ros/melodic/bin:/home/alexis/.nvm/versions/node/v8.16.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PKG_CONFIG_PATH=/opt/ros/melodic/lib/pkgconfig
ROS_DISTRO=melodic

Add the following line to your launch.json task: "envFile": "${workspaceFolder}/.env"

Note: this could be automated in a prerunTask using:

command: "source /opt/ros/melodic/setup.bash; printenv | grep melodic > ${workspaceFolder}/.env"
Daryl answered 14/10, 2019 at 15:9 Comment(3)
Does not actually source in the run shell, I'll accept an answer that does so or brings evidence that it can not be done. But definetly did the trick for my case and got me debugging on VS code, so +1 and bounty is yours.Trihedron
@Alexis Paques This is working fine for me exactly when I run python3 and try to import tf module. The most important part is PYTHONPATH, so I've used "source /opt/ros/melodic/setup.bash; printenv | grep PYTHONPATH > ${workspaceFolder}/.env". Thanks anyway, but why python debug console does not source .bashrc? this is a question for me . Maybe it is not opened as login terminal.Expatiate
The likely reason would be this simply doesn't use Bash to execute the command. You could try to add source /opt/ros/melodic/setup.sh to your ~/.profile which should be available for the whole session. (to test it, you have to logout and login). If it works, the bad thing about it would be the environment pollution. I am using multiple versions of ROS/ROS2 and many different workspace.Daryl
E
2

Perhaps this might help after a zoom. this Got that info from here

Expiation answered 12/10, 2019 at 7:4 Comment(3)
Thanks, unfortunately this does not solve my problem. My setup.sh among other things adds the folder of my executable "myProgram" to PATH and sets the path of shared librries. Also with the changes you proposed, if i try to run with "program"="MyProgram", VScode will still complain that myProgram is not found. If i set "program"="/full/path/to/myProgram" VScode the program will not find shared libraries as if setup.sh was sourced in a separate shell w.r.t. the one used for dunning myProgram. In a normal shell, calling source setup.sh and then MyProgram will run correctly.Trihedron
Worked for me when running a Python script depending on some ros2 foxy Python modules.Cadence
@Expiation this does not work for me. Using Linux ubuntu 18.04 and both Python2.7 and Pyhton 3.6.x is installed and when I applied this prerun to my python file(python3), I cannot import the tf module even though I sourced it. I have to source it manually everytime from the Python debug console when I pressed F5.Expatiate
A
1

I have found an ugly solution that works in vscode 1.83.0:

  1. update ~/.bashrc using preLunchTask
  2. launch debug
  3. restore ~/.bashrc using postDebugTask

Example:

  1. add source ~/.my_debug_env to ~/.bashrc so we do not actually modify ~/.bashrc, e.g.
if [[ -f ~/.my_debug_env ]]; then
  source ~/.my_debug_env
fi
  1. create two tasks for creating ~/.my_debug_env and removing it. my_env.sh is the file we want to source before debugging.
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "command": "echo",
      "args": ["source my_env.sh", ">", "~/.my_debug_env"],
      "label": "create ~/.my_debug_env",
    },
    {
      "type": "shell",
      "command": "rm",
      "args": ["~/.my_debug_env", "-rf"],
      "label": "remove ~/.my_debug_env",
    }
  ]
}
  1. update the debugging configuration
{
  "version": "0.2.0",
  "configurations": [
    ...
    "preLunchTask": "create ~/.my_debug_env",
    "postDebugTask": "remove ~/.my_debug_env",
  ]
}

Cheers !!!

Airsickness answered 4/6 at 9:1 Comment(0)
W
0

I actually solved this problem by adding source /some_folder/setup.bash to ~/.bashrc which is run whenever the shell is started. This way, when the shell starts starts it will source setup.bash script and you don't need the task that runs on different shell.

Washin answered 12/5, 2023 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.