Gianni
Gianni

Reputation: 518

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

My current workflow for a project is the following:

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?

Upvotes: 9

Views: 7310

Answers (4)

Shuo Li
Shuo Li

Reputation: 141

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 !!!

Upvotes: 2

theateist
theateist

Reputation: 14399

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.

Upvotes: 0

Alexis Paques
Alexis Paques

Reputation: 1975

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"

Upvotes: 13

ignacio
ignacio

Reputation: 1197

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

Upvotes: 2

Related Questions