Bazel "BUILD file not found" - Fix Missing Packages in CI
Bazel was told to treat a directory as a package but found no BUILD or BUILD.bazel file there. This is the concrete form of "no such package": the marker file that defines a package is absent.
What this error means
A target pattern (e.g. //tools/gen:all) fails with "BUILD file not found in any of the following directories", listing the directory it checked. The directory exists but has no BUILD file, so it is not a package.
ERROR: BUILD file not found in any of the following directories. Add a BUILD
file to a directory to mark it as a package.
- tools/gen
ERROR: Skipping '//tools/gen:all': no such package 'tools/gen'Common causes
Directory has no BUILD file
The directory contains sources but no BUILD/BUILD.bazel, so Bazel does not consider it a package and cannot resolve targets in it.
BUILD file not checked in or gitignored
A BUILD file present locally but excluded by .gitignore (or not committed) is absent on the CI checkout, so CI fails where local passes.
How to fix it
Create and commit the BUILD file
Add the BUILD file with the targets that directory should expose, and make sure it is tracked by git.
touch tools/gen/BUILD.bazel
git add -f tools/gen/BUILD.bazel # if a broad .gitignore was hiding itCheck the file is present in CI
- Confirm the BUILD file is committed (
git ls-files tools/gen). - Check
.gitignoredoes not excludeBUILD/BUILD.bazel. - Run
bazel query //tools/gen:allto confirm the package now resolves.
How to prevent it
- Keep a BUILD/BUILD.bazel file in every package directory and commit it.
- Add a
!BUILD.bazelun-ignore rule if a broad .gitignore could hide it. - Validate target patterns with
bazel queryin CI before building.