We have a bazel_binary
which iterates through all the subdirectories of a fixed directory, and parses the .json
files in them, as follows. We want to use a directory iterator because this directory can have more subdirectories in future with new .json
files in them.
<code>for (const auto& entry : std::filesystem::directory_iterator("/system/artefacts")) {
if (entry.is_directory()) {
// find .json files and do stuff with them
<code>for (const auto& entry : std::filesystem::directory_iterator("/system/artefacts")) {
if (entry.is_directory()) {
// find .json files and do stuff with them
}
}
</code>
for (const auto& entry : std::filesystem::directory_iterator("/system/artefacts")) {
if (entry.is_directory()) {
// find .json files and do stuff with them
}
}
We are using bazel
to build and run this binary, the bazel
target looks like:
<code>cc_binary(
name = "dir_iterator",
srcs = [
"dir_iterator.cpp",
],
data = [
":data",
],
deps = [
#...
],
)
</code>
cc_binary(
name = "dir_iterator",
srcs = [
"dir_iterator.cpp",
],
data = [
":data",
],
deps = [
#...
],
)
"system/artefacts", # does NOT work
<code>filegroup(
name = "data",
srcs = glob([
"system/artefacts", # does NOT work
]),
)
</code>
filegroup(
name = "data",
srcs = glob([
"system/artefacts", # does NOT work
]),
)
When we run this target with bazel run
it fails with the following error:
<code>t3-build-shell-5.0$ bazel run //utils:dir_iterator
INFO: Analyzed target //utils:dir_iterator (1 packages loaded, 8 targets configured).
Target //utils:dir_iterator up-to-date:
bazel-bin/utils/dir_iterator
INFO: Elapsed time: 1.082s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/utils/dir_iterator
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: directory iterator cannot open directory: No such file or directory [system/artifacts/]
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: directory iterator cannot open directory: No such file or directory [system/artefacts/]
<code>t3-build-shell-5.0$ bazel run //utils:dir_iterator
INFO: Analyzed target //utils:dir_iterator (1 packages loaded, 8 targets configured).
INFO: Found 1 target...
Target //utils:dir_iterator up-to-date:
bazel-bin/utils/dir_iterator
INFO: Elapsed time: 1.082s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/utils/dir_iterator
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: directory iterator cannot open directory: No such file or directory [system/artifacts/]
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: directory iterator cannot open directory: No such file or directory [system/artefacts/]
</code>
t3-build-shell-5.0$ bazel run //utils:dir_iterator
INFO: Analyzed target //utils:dir_iterator (1 packages loaded, 8 targets configured).
INFO: Found 1 target...
Target //utils:dir_iterator up-to-date:
bazel-bin/utils/dir_iterator
INFO: Elapsed time: 1.082s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/utils/dir_iterator
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: directory iterator cannot open directory: No such file or directory [system/artifacts/]
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: directory iterator cannot open directory: No such file or directory [system/artefacts/]
This is because bazel
is not including the directory in question in the runtime environment.
So the question is: how can I include the directory in question with this bazel
target, to get rid of this error?
Directory structure of bazel
workspace
<code>.
├── system
│ └── artefacts
│ ├── x
│ │ └── a.json
│ ├── y
│ │ └── a.json
│ └── z
│ └── a.json
└── util
├── BUILD
└── dir_iterator.cpp
</code>
.
├── system
│ └── artefacts
│ ├── x
│ │ └── a.json
│ ├── y
│ │ └── a.json
│ └── z
│ └── a.json
└── util
├── BUILD
└── dir_iterator.cpp