ROS2 integration build fails

I am running Kubuntu 22.04.5 LTS and a python 3.10.12 venv with beamngpy 1.31 installed but I cannot get the BeamNG ROS2 integration to compile;

When executing rosdep install -i --from-path src --rosdistro humble -y there are no packages missing and I get a message saying #All required rosdeps installed successfully.

However on trying to compile with colcon build I get this:

Starting >>> beamng_msgs
--- stderr: beamng_msgs                         
CMake Error at /opt/ros/humble/share/rosidl_adapter/cmake/rosidl_adapt_interfaces.cmake:59 (message):
  execute_process(/home/filippo/python_venvs/beamng_ros/bin/python3 -m
  rosidl_adapter --package-name beamng_msgs --arguments-file
  /home/filippo/beamng_ros2_ws/build/beamng_msgs/rosidl_adapter__arguments__beamng_msgs.json
  --output-dir
  /home/filippo/beamng_ros2_ws/build/beamng_msgs/rosidl_adapter/beamng_msgs
  --output-file
  /home/filippo/beamng_ros2_ws/build/beamng_msgs/rosidl_adapter/beamng_msgs.idls)
  returned error code 1:

  AttributeError processing template 'srv.idl.em'

  Traceback (most recent call last):

    File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/resource/__init__.py", line 51, in evaluate_template
      em.BUFFERED_OPT: True,

  AttributeError: module 'em' has no attribute 'BUFFERED_OPT'



  During handling of the above exception, another exception occurred:



  Traceback (most recent call last):

    File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
      exec(code, run_globals)
    File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/__main__.py", line 19, in <module>
      sys.exit(main())
    File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/main.py", line 53, in main
      abs_idl_file = convert_to_idl(
    File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/__init__.py", line 24, in convert_to_idl
      return convert_srv_to_idl(
    File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/srv/__init__.py", line 39, in convert_srv_to_idl
      expand_template('srv.idl.em', data, output_file, encoding='iso-8859-1')
    File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/resource/__init__.py", line 23, in expand_template
      content = evaluate_template(template_name, data)
    File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/resource/__init__.py", line 69, in evaluate_template
      _interpreter.shutdown()

  AttributeError: 'NoneType' object has no attribute 'shutdown'

Call Stack (most recent call first):
  /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:130 (rosidl_adapt_interfaces)
  CMakeLists.txt:19 (rosidl_generate_interfaces)


---
Failed   <<< beamng_msgs [0.34s, exited with code 1]

Summary: 0 packages finished [0.42s]
  1 package failed: beamng_msgs
  1 package had stderr output: beamng_msgs
  4 packages not processed

The error changes when running sudo colcon build:

Starting >>> beamng_msgs
--- stderr: beamng_msgs                         
Traceback (most recent call last):
  File "/opt/ros/humble/share/ament_cmake_core/cmake/package_templates/templates_2_cmake.py", line 21, in <module>
    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(/home/filippo/python_venvs/beamng_ros/bin/python3
  /opt/ros/humble/share/ament_cmake_core/cmake/package_templates/templates_2_cmake.py
  /home/filippo/beamng_ros2_ws/build/beamng_msgs/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:14 (find_package)


---
Failed   <<< beamng_msgs [0.11s, exited with code 1]

Summary: 0 packages finished [0.25s]
  1 package failed: beamng_msgs
  1 package had stderr output: beamng_msgs
  4 packages not processed

I think there is something wrong with ament_cmake but every possible solution I stumbled upon on internet didn’t work.

Is there something I am missing?

Hello FiliLecci,
Your issue appears to be a system-wide ROS 2 problem rather than a package-specific one. Follow these steps to isolate and resolve it:

  1. Remove all packages from your workspace and clear build artifacts.
  2. Attempt to build an empty workspace to check if ROS 2 itself is functional.
  3. Reintroduce packages one by one, rebuilding after each to identify any problematic package.
  4. Ensure ROS 2 is properly sourced before each build.
  5. Verify Python dependencies and reinstall any missing packages.

Thank you for the reply, I tried the steps you suggested and got the following results:

I figured this was a Python empy error, so I fixed it with pip instal empy==3.3.4. Trying to build after this got me to other errors and warnings which I discovered were caused by the lark package missing.
I installed it with pip install lark and it finally worked.

So to recap: there were 2 packages missing, empy and lark, they can be installed with pip install empy==3.3.4 lark and now the project build is successful

This was a common issue with the previous beamng_ros2_integration also, so I would suggest you specify that these packages have to be installed.

Glad you got it working! Just to clarify, empy and lark are not specific to the BeamNG ROS2 integration but are general ROS2 dependencies. It seems like your environment was missing them, which is why the issue occurred.