Using pytorch with bazel

I have tried to install torch in a bazel project using gazelle. I have run into some serious build issues. First, I needed to patch torch because of circular dependencies, however I am now stuck with linking issues.

I have created a repo with a minimal reproduction: GitHub - OliverFM/pytorch_with_gazelle

To install torch and dependencies, run: make generate_build_files and then run bazel run //test_project:test_project_bin. The issue that I am getting is a linking error:

❯ bazel run //test_project:test_project_bin
INFO: Analyzed target //test_project:test_project_bin (1 packages loaded, 4 targets configured).
INFO: Found 1 target...
Target //test_project:test_project_bin up-to-date:
INFO: Elapsed time: 0.757s, Critical Path: 0.13s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/test_project/test_project_bin
Traceback (most recent call last):
  File "/home/oliver/.cache/bazel/_bazel_oliver/a5974369252d7c7f580df32b3fcee8ce/execroot/build_file_generation_example/bazel-out/k8-fastbuild/bin/test_project/test_project_bin.runfiles/build_file_generation_example/test_project/", line 3, in <module>
    import triton
  File "/home/oliver/.cache/bazel/_bazel_oliver/a5974369252d7c7f580df32b3fcee8ce/execroot/build_file_generation_example/bazel-out/k8-fastbuild/bin/test_project/test_project_bin.runfiles/pip_triton/site-packages/triton/", line 9, in <module>
    import torch  # noqa: F401
  File "/home/oliver/.cache/bazel/_bazel_oliver/a5974369252d7c7f580df32b3fcee8ce/execroot/build_file_generation_example/bazel-out/k8-fastbuild/bin/test_project/test_project_bin.runfiles/pip_torch/site-packages/torch/", line 229, in <module>
    from torch._C import *  # noqa: F403
ImportError: cannot open shared object file: No such file or directory

Any suggestions on debugging this?