Skip to content

Commit

Permalink
Copy over runfiles library from Bazel (#13)
Browse files Browse the repository at this point in the history
This uses `root_symlinks` to preserve the fixed runfiles path at which
the library must be available and the private `skip_conflict_checking`
attribute to preserve backwards compatibility by not enabling strict
runfiles path conflict checking.
  • Loading branch information
fmeum authored Nov 5, 2024
1 parent 02bc019 commit a110e84
Show file tree
Hide file tree
Showing 10 changed files with 1,019 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ matrix:
- windows
bazel:
- 6.5.0
- 7.3.2
- 7.4.0

tasks:
test_module_bzlmod:
Expand Down
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
workspace(name = "rules_shell")

load("//shell:repositories.bzl", "rules_shell_dependencies", "rules_shell_toolchains")

rules_shell_dependencies()

rules_shell_toolchains()
54 changes: 54 additions & 0 deletions shell/private/root_symlinks.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2024 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Helper rule to preserve the legacy runfiles path for the runfiles lib."""

# Requires the private skip_conflict_checking parameter on ctx.runfiles, which
# is only available as of Bazel 7.4.0. We only use it when the native shell
# rules are not available.
ROOT_SYMLINKS_SUPPORTED = not hasattr(native, "sh_binary")

def _single_file_or_fail(target):
files = target[DefaultInfo].files.to_list()
if len(files) != 1:
fail("Expected exactly one file in {}, got {}".format(target.label, files))
return files[0]

def _root_symlinks_impl(ctx):
runfiles = ctx.runfiles(
root_symlinks = {
path: _single_file_or_fail(target)
for target, path in ctx.attr.root_symlinks.items()
},
# Adding root symlinks from Starlark usually enables conflict checking,
# but that would break backwards compatibility as it affects all
# runfiles, not just the symlinks.
skip_conflict_checking = True,
)
return [
DefaultInfo(
files = depset(),
runfiles = runfiles,
),
]

root_symlinks = rule(
implementation = _root_symlinks_impl,
attrs = {
"root_symlinks": attr.label_keyed_string_dict(
allow_files = True,
mandatory = True,
),
},
)
22 changes: 22 additions & 0 deletions shell/runfiles/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("//shell:sh_library.bzl", "sh_library")
load("//shell/private:root_symlinks.bzl", "ROOT_SYMLINKS_SUPPORTED", "root_symlinks")

alias(
name = "runfiles",
actual = ":runfiles_impl" if ROOT_SYMLINKS_SUPPORTED else "@bazel_tools//tools/bash/runfiles",
visibility = ["//visibility:public"],
)

sh_library(
name = "runfiles_impl",
data = [":runfiles_at_legacy_location"],
tags = ["manual"],
)

root_symlinks(
name = "runfiles_at_legacy_location",
root_symlinks = {
"runfiles.bash": "bazel_tools/tools/bash/runfiles/runfiles.bash",
},
tags = ["manual"],
)
Loading

0 comments on commit a110e84

Please sign in to comment.