mvukov / rules_ros

Build ROS (1) with Bazel
Apache License 2.0
21 stars 9 forks source link

Rules ROS seems to be not fully hermetic #3

Closed hofbi closed 4 months ago

hofbi commented 4 months ago

We identified some uncertainties in rules_ros while working on remote execution. We were also able to make them locally reproducible using the https://github.com/aspect-build/rules_py:

If I run bazel build @ros_comm//:roscpp_lib locally, this works fine. Now if I switch this load statement in repositories/gencpp.BUILD.bazel form load("@rules_python//python:defs.bzl", "py_binary", "py_library") to the more stricter sandboxed load("@aspect_rules_py//py:defs.bzl", "py_library", "py_binary"), I get

Traceback (most recent call last):
  File "bazel-out/k8-opt-exec-ST-a9bf7a3d11c4/bin/external/_main~non_module_ros_repositories~ros_gencpp/gencpp_bin.runfiles/_main~non_module_ros_repositories~ros_gencpp/scripts/gen_cpp.py", line 49, in <module>
    genmsg.template_tools.generate_from_command_line_options(
  File "/tmp/bazel-working-directory/_main/bazel-out/k8-opt-exec-ST-a9bf7a3d11c4/bin/external/_main~non_module_ros_repositories~ros_gencpp/gencpp_bin.runfiles/_main~non_module_dependencies~ros_genmsg/src/genmsg/template_tools.py", line 219, in generate_from_command_line_options
    generate_from_file(argv[1], options.package, options.outdir, options.emdir, options.includepath, msg_template_dict, srv_template_dict)
  File "/tmp/bazel-working-directory/_main/bazel-out/k8-opt-exec-ST-a9bf7a3d11c4/bin/external/_main~non_module_ros_repositories~ros_gencpp/gencpp_bin.runfiles/_main~non_module_dependencies~ros_genmsg/src/genmsg/template_tools.py", line 158, in generate_from_file
    _generate_msg_from_file(input_file, output_dir, template_dir, search_path, package_name, msg_template_dict)
  File "/tmp/bazel-working-directory/_main/bazel-out/k8-opt-exec-ST-a9bf7a3d11c4/bin/external/_main~non_module_ros_repositories~ros_gencpp/gencpp_bin.runfiles/_main~non_module_dependencies~ros_genmsg/src/genmsg/template_tools.py", line 95, in _generate_msg_from_file
    _generate_from_spec(input_file,
  File "/tmp/bazel-working-directory/_main/bazel-out/k8-opt-exec-ST-a9bf7a3d11c4/bin/external/_main~non_module_ros_repositories~ros_gencpp/gencpp_bin.runfiles/_main~non_module_dependencies~ros_genmsg/src/genmsg/template_tools.py", line 82, in _generate_from_spec
    raise RuntimeError("Template file %s not found in template dir %s" % (template_file_name, template_dir))
RuntimeError: Template file msg.h.template not found in template dir /tmp/bazel-source-roots/4/lib/python38.zip
Exception ignored in: <function Interpreter.__del__ at 0x7efadffe6e50>
Traceback (most recent call last):
  File "/tmp/bazel-working-directory/_main/bazel-out/k8-opt-exec-ST-a9bf7a3d11c4/bin/external/_main~non_module_ros_repositories~ros_gencpp/gencpp_bin.runfiles/rules_python~0.29.0~pip~rules_ros_pip_deps_38_empy/site-packages/em.py", line 2094, in __del__
  File "/tmp/bazel-working-directory/_main/bazel-out/k8-opt-exec-ST-a9bf7a3d11c4/bin/external/_main~non_module_ros_repositories~ros_gencpp/gencpp_bin.runfiles/rules_python~0.29.0~pip~rules_ros_pip_deps_38_empy/site-packages/em.py", line 2157, in shutdown
  File "/tmp/bazel-working-directory/_main/bazel-out/k8-opt-exec-ST-a9bf7a3d11c4/bin/external/_main~non_module_ros_repositories~ros_gencpp/gencpp_bin.runfiles/rules_python~0.29.0~pip~rules_ros_pip_deps_38_empy/site-packages/em.py", line 2647, in finalize
  File "/tmp/bazel-working-directory/_main/bazel-out/k8-opt-exec-ST-a9bf7a3d11c4/bin/external/_main~non_module_ros_repositories~ros_gencpp/gencpp_bin.runfiles/rules_python~0.29.0~pip~rules_ros_pip_deps_38_empy/site-packages/em.py", line 2200, in push
AttributeError: '_io.TextIOWrapper' object has no attribute 'push'

Now here a few guesses to help solving this issue

  1. Adding the templates as data dependency:
filegroup(
    name = "data",
    srcs = glob(["scripts/*.template"]),
    visibility = ["//visibility:public"],
)
  1. Explicitly setting the template dir (args.add("-e", ctx.attr.data.files.to_list()[0].dirname)) in here, since the default template dir defined in gemmsg seems to be wrong.

Any help/hint would be appreciated.

mvukov commented 4 months ago

Hi Markus, thanks for reaching out. I suspect some Python code in e.g. gensmg uses something like dirname(__file__)/path/to/some.template to get a template. This escapes the sandbox and it's not going to work on RBE :) The combination of your suggestions 1. and 2. should fix the issue. If it works, please feel free to open a PR.

TBH, I neglected this project as I am working on rules_ros2 repo (that also has quite some active users). Are you actively using this rules_ros repo or just evaluating at the moment?

mvukov commented 4 months ago

Fixed via https://github.com/mvukov/rules_ros/pull/7.