-
Notifications
You must be signed in to change notification settings - Fork 179
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
Add bazel rules for handling directories #494
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/bazel-* | ||
MODULE.bazel.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
licenses(["notice"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# 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. | ||
|
||
"""Skylib module containing rules to create metadata about directories.""" | ||
|
||
load(":providers.bzl", "DirectoryInfo") | ||
|
||
visibility("public") | ||
|
||
def _directory_impl(ctx): | ||
if ctx.label.workspace_root: | ||
pkg_path = ctx.label.workspace_root + "/" + ctx.label.package | ||
else: | ||
pkg_path = ctx.label.package | ||
source_dir = pkg_path.rstrip("/") | ||
source_prefix = source_dir + "/" | ||
|
||
# Declare a generated file so that we can get the path to generated files. | ||
f = ctx.actions.declare_file(ctx.label.name) | ||
ctx.actions.write(f, "") | ||
generated_dir = f.path.rsplit("/", 1)[0] | ||
generated_prefix = generated_dir + "/" | ||
|
||
root_metadata = struct( | ||
directories = {}, | ||
files = [], | ||
source_path = source_dir, | ||
generated_path = generated_dir, | ||
human_readable = "@@%s//%s" % (ctx.label.repo_name, ctx.label.package), | ||
) | ||
|
||
# Topologically order directories so we can use them for DFS. | ||
topo = [root_metadata] | ||
for src in ctx.files.srcs: | ||
prefix = source_prefix if src.is_source else generated_prefix | ||
if not src.path.startswith(prefix): | ||
fail("{path} is not contained within {prefix}".format( | ||
path = src.path, | ||
prefix = root_metadata.human_readable, | ||
)) | ||
relative = src.path[len(prefix):].split("/") | ||
current_path = root_metadata | ||
for dirname in relative[:-1]: | ||
if dirname not in current_path.directories: | ||
dir_metadata = struct( | ||
directories = {}, | ||
files = [], | ||
source_path = "%s/%s" % (current_path.source_path, dirname), | ||
generated_path = "%s/%s" % (current_path.generated_path, dirname), | ||
human_readable = "%s/%s" % (current_path.human_readable, dirname), | ||
) | ||
current_path.directories[dirname] = dir_metadata | ||
topo.append(dir_metadata) | ||
|
||
current_path = current_path.directories[dirname] | ||
current_path.files.append(src) | ||
|
||
# The output DirectoryInfos. Key them by something arbitrary but unique. | ||
# In this case, we choose source_path. | ||
out = {} | ||
for dir_metadata in reversed(topo): | ||
child_dirs = { | ||
dirname: out[subdir_metadata.source_path] | ||
for dirname, subdir_metadata in dir_metadata.directories.items() | ||
} | ||
files = sorted(dir_metadata.files, key = lambda file: file.basename) | ||
direct_files = depset(files) | ||
transitive_files = depset(transitive = [direct_files] + [ | ||
d.transitive_files | ||
for d in child_dirs.values() | ||
], order = "preorder") | ||
out[dir_metadata.source_path] = DirectoryInfo( | ||
directories = child_dirs, | ||
files = {f.basename: f for f in files}, | ||
direct_files = direct_files, | ||
transitive_files = transitive_files, | ||
source_path = dir_metadata.source_path, | ||
generated_path = dir_metadata.generated_path, | ||
human_readable = dir_metadata.human_readable, | ||
) | ||
|
||
root_directory = out[root_metadata.source_path] | ||
|
||
return [ | ||
root_directory, | ||
DefaultInfo(files = root_directory.transitive_files), | ||
] | ||
|
||
directory = rule( | ||
implementation = _directory_impl, | ||
attrs = { | ||
"srcs": attr.label_list(allow_files = True), | ||
}, | ||
provides = [DirectoryInfo], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 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. | ||
|
||
"""Skylib module containing providers for directories.""" | ||
|
||
visibility("public") | ||
|
||
DirectoryInfo = provider( | ||
doc = "Information about a directory", | ||
# @unsorted-dict-items | ||
fields = { | ||
"directories": "(Dict[str, DirectoryInfo]) The subdirectories contained directly within", | ||
"files": "(Dict[str, File]) The files contained directly within the directory, keyed by their path relative to this directory.", | ||
"direct_files": "(Depset[File])", | ||
# A transitive_directories would be useful here, but is blocked on | ||
# https://github.com/bazelbuild/starlark/issues/272 | ||
"transitive_files": "(Depset[File])", | ||
"source_path": "(string) Path to all source files contained within this directory", | ||
"generated_path": "(string) Path to all generated files contained within this directory", | ||
"human_readable": "(string) A human readable identifier for a directory. Useful for providing error messages to a user.", | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 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. | ||
|
||
"""Skylib module containing rules to create metadata about subdirectories.""" | ||
|
||
load(":providers.bzl", "DirectoryInfo") | ||
load(":utils.bzl", "get_subdirectory") | ||
|
||
visibility("public") | ||
|
||
def _subdirectory_impl(ctx): | ||
dir = get_subdirectory(ctx.attr.parent[DirectoryInfo], ctx.attr.path) | ||
return [ | ||
dir, | ||
DefaultInfo(files = dir.transitive_files), | ||
] | ||
|
||
subdirectory = rule( | ||
implementation = _subdirectory_impl, | ||
attrs = { | ||
"parent": attr.label(providers = [DirectoryInfo], mandatory = True), | ||
"path": attr.string(mandatory = True), | ||
}, | ||
provides = [DirectoryInfo], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# 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. | ||
|
||
"""Skylib module containing utility functions related to directories.""" | ||
|
||
visibility("public") | ||
|
||
_DIR_NOT_FOUND = """{directory} does not contain a directory named {dirname}. | ||
Instead, it contains the directories {children}.""" | ||
|
||
def _check_path_relative(path): | ||
if path.startswith("/"): | ||
fail("Path must be relative. Got {path}".format(path = path)) | ||
|
||
def get_direct_subdirectory(directory, dirname): | ||
"""Gets the direct subdirectory of a directory. | ||
|
||
Args: | ||
directory: (DirectoryInfo) The directory to look within. | ||
dirname: (string) The name of the directory to look for. | ||
Returns: | ||
(DirectoryInfo) The directory contained within.""" | ||
if dirname not in directory.directories: | ||
fail(_DIR_NOT_FOUND.format( | ||
directory = directory.human_readable, | ||
dirname = repr(dirname), | ||
children = repr(sorted(directory.directories)), | ||
)) | ||
return directory.directories[dirname] | ||
|
||
def get_subdirectory(directory, path): | ||
"""Gets a subdirectory contained within a tree of another directory. | ||
|
||
Args: | ||
directory: (DirectoryInfo) The directory to look within. | ||
path: (string) The path of the directory to look for within it. | ||
Returns: | ||
(DirectoryInfo) The directory contained within. | ||
""" | ||
_check_path_relative(path) | ||
|
||
for dirname in path.split("/"): | ||
directory = get_direct_subdirectory(directory, dirname) | ||
return directory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
load("@rules_testing//lib:analysis_test.bzl", "analysis_test") | ||
load( | ||
":directory_test.bzl", | ||
"directory_test", | ||
"nonexistent_subdirectory_test", | ||
"outside_testdata_test", | ||
"subdirectory_test", | ||
) | ||
|
||
analysis_test( | ||
name = "directory_test", | ||
impl = directory_test, | ||
targets = { | ||
"root": "//tests/directories/testdata:root", | ||
"f1": "//tests/directories/testdata:f1_filegroup", | ||
"f2": "//tests/directories/testdata:f2_filegroup", | ||
"f3": "//tests/directories/testdata:f3", | ||
}, | ||
) | ||
|
||
filegroup( | ||
name = "outside_testdata", | ||
srcs = ["BUILD"], | ||
visibility = ["//tests/directories/testdata:__pkg__"], | ||
) | ||
|
||
analysis_test( | ||
name = "outside_testdata_test", | ||
expect_failure = True, | ||
impl = outside_testdata_test, | ||
target = "//tests/directories/testdata:outside_testdata", | ||
) | ||
|
||
analysis_test( | ||
name = "subdirectory_test", | ||
impl = subdirectory_test, | ||
targets = { | ||
"root": "//tests/directories/testdata:root", | ||
"dir": "//tests/directories/testdata:dir", | ||
"subdir": "//tests/directories/testdata:subdir", | ||
"f2": "//tests/directories/testdata:f2_filegroup", | ||
}, | ||
) | ||
|
||
analysis_test( | ||
name = "nonexistent_subdirectory_test", | ||
expect_failure = True, | ||
impl = nonexistent_subdirectory_test, | ||
target = "//tests/directories/testdata:nonexistent_subdirectory", | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This provider looks surprisingly heavyweight. Someone reaching for this kind of rule probably just wants to do something like:
With the current provider's structure, how to achieve that goal is non-obvious to me. I understand that a lot of this information is handy (and in some cases critical to the current implementation), but it seems to get in the way of usability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, I created rules_directory to do this instead. I'll close this PR.
And I agree with your concerns, when rewriting for rules_directory I simplified it a lot. The following should now be possible:
Then in the foo_rule implementation: