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

Viewed 2065

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?

2 Answers

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"

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

Related