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

Copy over runfiles library from Bazel #13

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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