ms-iot / vscode-ros

Visual Studio Code extension for Robot Operating System (ROS) development
https://marketplace.visualstudio.com/items?itemName=ms-iot.vscode-ros
MIT License
391 stars 93 forks source link

[bug] ROS distro not sourced for tasks.json on humble #1272

Open Ryanf55 opened 6 months ago

Ryanf55 commented 6 months ago

(Please add appropriate labels)

Plugin v0.9.6 pre-release

image

what is the bug

When I add a task to run colcon build, it does not source the ROS environment.

Repro steps

  1. Add "ros.distro": "humble", to .vscode/settings.json on the remote computer
  2. Add this to tasks.json
           {
            "label": "colconBuildDebug",
            "detail": "Build ROS 2 workspace with debug symbols",
            "type": "shell",
            "command": "colcon build --symlink-install --mixin debug",
            "group": "build",
            "problemMatcher": "$gcc"
        },
  3. Ensure the ROS extension is started and running in pre-release on the development server
  4. Type F1: Tasks: Run task and select colconBuildDebug
  5. Observe the output showing that it never sourced the ROS environment
    
    *  Executing task: colcon build --symlink-install --mixin debug 

Starting >>> grid_map_geo --- stderr: grid_map_geo
Traceback (most recent call last): File "/opt/ros/humble/share/ament_cmake_core/cmake/package_templates/templates_2_cmake.py", line 21, in from ament_package.templates import get_environment_hook_template_path ModuleNotFoundError: No module named 'ament_package' CMake Error at /opt/ros/humble/share/ament_cmake_core/cmake/ament_cmake_package_templates-extras.cmake:41 (message): execute_process(/usr/bin/python3.10 /opt/ros/humble/share/ament_cmake_core/cmake/package_templates/templates_2_cmake.py /home/ryan/Dev/ros2_ws/src/grid_map_geo/build/grid_map_geo/ament_cmake_package_templates/templates.cmake) returned error code 1 Call Stack (most recent call first): /opt/ros/humble/share/ament_cmake_core/cmake/ament_cmake_coreConfig.cmake:41 (include) /opt/ros/humble/share/ament_cmake/cmake/ament_cmake_export_dependencies-extras.cmake:15 (find_package) /opt/ros/humble/share/ament_cmake/cmake/ament_cmakeConfig.cmake:41 (include) CMakeLists.txt:18 (find_package)


Failed <<< grid_map_geo [0.13s, exited with code 1]

Summary: 0 packages finished [0.50s] 1 package failed: grid_map_geo 1 package had stderr output: grid_map_geo

expected behavior

Colcon build will be successful and first source /opt/ros/humble/setup.bash before running.

additional context

Even if I add source /opt/ros/humble/setup.bash to my bashrc, it doesn't source.

As a workaround, I could do modify the task command like so, but that is not documented: "command": "source /opt/ros/humble/setup.bash && colcon build --symlink-install --mixin debug",

Then, the launch still fails because it's not sourcing the local ROS workspace before launch:

image

ooeygui commented 6 months ago

Thank you for the report.

I don't know if it is possible to support this scenario. VSCode extension model has hooks for various events, which allows code to extension code to execute. Tasks.json is handled by VSCode - but it doesn't (last I looked) provide a general pre-execution hook to allow an extension to modify the execution environment.

Sourcing ROS and the overlay is one of the main reasons that the VSCode ROS extension has its own task execution for build and debug.

For this scenario, you may be able to manually prefix the command with the environment script, or build your own script which sources the environments then runs colcon.

Ryanf55 commented 6 months ago

How is launch.json supposed to work if it cannot source the local environment?

ooeygui commented 6 months ago

The problem here is using Task.json; I don't believe there is a hook for the ROS extension in that codepath of VSCode to influence the task environment.

We register a VSCode launch.json handler which is ROS aware - and is able to source the environment. The Task..json file is not used when you use the ROS mechanism, so the extension can control the environment.

Ryanf55 commented 6 months ago

Two environments need sourcing causing two separate issues.

The Humble environment isn't sourced in tasks

The first is /opt/ros/humble/setup.bash which needs to be sourced if you want to add a colcon build task as shown at this timestamp in the video.

A workaround as I already shared is to prepend all commands with source /opt/ros/humble/setup.bash &&

The local workspace is not sourced for ROS launch in debuggerlaunch

Regardless of whether you build through a task, or build using your terminal, the local workspace has the build binaries and environment setup located in the default install folder. Before calling ros2 launch <myRos2Pkg> <myLaunchFile.xml>, you need to type this in Ubuntu: source install/setup.bash

Without this, ros2 will give an error as I shared earlier: package <myRos2Pkg> not found

And, you are unable to debug your code in VSCode with the build in GDB GUI with a launch.json.

These two problems are independent issues. Would you like me to create an issue for the 2nd one? The title of this ticket only applies to the 1st one.

I'm happy to set up a debug build of vscode, or whatever else you need to help you solve these issues on Ubuntu.

ooeygui commented 6 months ago

The VSCode ROS extension Provides a "ROS Create Terminal" Command which creates a new terminal with ROS sourced. VSCode does not give extensions a hook for providing environment through the normal UI.

Ryanf55 commented 5 months ago

Hello,

I'm not sure if you need more info, but it appears this bug is only present when SSH'ing from Windows. If I have direct access to the Linux computer, the launch task works fine, and colcon build tasks works fine with the workaround I posted.

It would be desirable for SSH not to adversely impact this extension's functionality.

ooeygui commented 5 months ago

@Ryanf55 That interesting - thanks for the note. It certainly narrows down where this could be going wrong.