Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a runfiles library #1310

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

cerisier
Copy link

@cerisier cerisier commented Sep 17, 2024

First shot at implementing #890

Main implementation

I followed guidance from @fmeum and based this implementation on the runfiles library of rules_python where applicable since this was pointed as the reference implementation.

The non applicable parts, especially the runtime computation of the runfiles directory has been inspired by the implementation in rules_rust.

Source repository problem

One particularity that comes with bzlmod is that the lookup function must know about the caller canonical repository name to lookup the proper repo mapping in the generated _repo_mapping.

Almost all implementations out there either use runtime stacktrace introspection mechanisms to deduce the source repository from the path of the source file of the function calling the runfiles rlocation method (Go, Python), or other compile time flavors provided by the language (Rust, Java).

Since none of these options are feasible in Swift, I chose to follow what the Runfiles C++ library does: provide a magical constant to be passed to the constructor of the Runfiles class for every target that depends on the @rules_swift//swift/runfiles.

import BazelRunfiles

let runfiles = try Runfiles.create(sourceRepository: BazelRunfilesConstants.currentRepository)

the path lookup can then be performed by prefixing the path of the runfile with the workspace under which it lives:

let runfile = runfiles.rlocation("repo_name/path/to/data/sample.txt")

The way this Constants enum is provided to each target that depends on @rules_swift//swift/runfiles has been hacked quickly for the proof of concept but will have to be discussed with the rules_swift maintainers for a correct implementation. See runfiles.bzl.

Note that currently, passing the runfiles library as private_deps doesn't work.

TODO

  • Make a proper PR description describing the API, the heuristics behind directory discovery and path lookup.
  • Discuss the proper way to solve the current repository issue and whether to keep the BazelRunfilesConstants file generation.
  • Fix Linux compat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants