Referring to a dependency module from rlocation in shell scripts

32 views
Skip to first unread message

Filip Filmar

unread,
Oct 4, 2025, 5:52:11 PMOct 4
to bazel-discuss

Hi folks.

How does one robustly refer to another repository when using rlocation in bash scripts?

Consider this example:

readonly _run_this="$(rlocation runfiles~/binary)"

this shell snippet refers to the location of the target called binary in a dep repo called @runfiles.

How does one do this in a robust way? To wit the “apparent” name runfiles~ is an implementation detail of bazel 7.x. If I upgrade to bazel 8.x, this will become an error as the "correct" name is now `runfiles+`.

Where this becomes a problem, is when we don’t know what bazel version will be used to compile this code. Ostensibly, both 7.x and 8.x should work. But in this case, the script becomes brittle, as one would expect given that it's relying on an implementation detail to resolve rlocation. I usually solve this in my code by pinning the bazel version. But this might not be an option when offering a module to an unknown user.

What’s the canonical solution to this? Resolving the current repository is handled using the runfiles_current_repository function. What about “non-current” repository?

F

Fabian Meumertzheim

unread,
Oct 5, 2025, 12:50:30 AMOct 5
to Filip Filmar, bazel-discuss
The mangled name with + or ~ characters is the canonical repository name, not an apparent name. It may differ across Bazel versions and should not be hardcoded.

Runfiles libraries such as the one in rules_shell (formerly bundled with Bazel's bazel_tools) allow you to look up files using apparent repository name.

In your example, you should thus be able to use the file from the "runfiles" module via $(rlocation runfiles/binary.sh) in the "integration" module unless you set a custom apparent repo name via the "repo_name" argument of the "bazel_dep" call.

The same path would work from within the "runfiles" module itself unless it chooses a custom apparent repo name for itself via the "repo_name" argument of the "module" function.

rundiles_current_repository is only needed in special situations such as when you are writing a wrapper around rlocation that looks up user-specified files.

Fabian 

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/bazel-discuss/CAKaOXigbjKENnu2NQtPzs54a70XXrfYqctLYuF97vnakdWMewQ%40mail.gmail.com.

Filip Filmar

unread,
Oct 5, 2025, 6:20:36 PMOct 5
to Fabian Meumertzheim, bazel-discuss
Thank you for the clarification and corrections.

I applied your advice to the example repo, and it seems to work as you described. I'm still having issues deploying this correctly in a different repository, but that's probably a side quest and not caused by anything described here.

Thanks,
F

Reply all
Reply to author
Forward
0 new messages