I'm trying to add a custom build rule that will automatically add all the targets (python files) in each sub-folder to the dependencies for a py_binary in a BUILD file.
Here is the folder structure of my project:
/my/project/src/subdir1/
/my/project/src/subdir2/
/my/project/src/subdir3/
/my/project/src/subdir4/
/my/project/src/subdir5/
Each subdir/ folder has its own BUILD file with the package default_visibility set to public for the py_library targets in that folder.
The python source file for the py_binary and the respective BUILD file are in the /my/project/src/ folder. In the custom build rule under /my/project/src/, I'm trying to use glob() to add the path for each target in each sub-folder to the dependencies for the py_binary in the BUILD file. However, the targets in a sub-folder aren't visible to the custom build rule unless I remove the BUILD file in that sub-folder.
Custom Build Rule:
def compute_deps():
pattern = ["**/.*py"]
all_files = native.glob(pattern)
print(all_files)
deps = [convert_path_to_target(filepath) for filepath in all_files]
return deps
def custom_build_rule(name, main, srcs):
deps = compute_deps()
return py_binary(
name = name,
main = main,
srcs = srcs,
deps = deps
)
When I run bazel query //my/project/... in the terminal, all the targets in each sub-folder are listed, so I don't understand why these targets aren't visible to the custom build rule.
The
globfunction cannot traverse past subpackage boundaries, which explains the behavior you see.You may be able to put something working together with the
subpackagesfunction, which can list all subpackages of the current package but not any targets within them.