Skip to content
Latchkey

Bazel "ERROR: no such target" in CI

Bazel found the package but the name after the colon does not match any rule or file declared in that BUILD file. The message lists the target you asked for and confirms the package was loaded.

What this error means

A build stops with "ERROR: no such target '//foo:bar': target 'bar' not declared in package 'foo'" even though //foo loads fine.

bazel
ERROR: no such target '//foo:bar': target 'bar' not declared in package 'foo'
defined by /home/runner/work/app/app/foo/BUILD.bazel (Tip: use `query "//foo:*"`
to see all the targets in that package)

Common causes

The rule name was renamed or removed

The BUILD file no longer declares a rule with that name, so the label points at nothing. A typo in the target name gives the same error.

The file is not exported as a target

You referenced a source file that is not listed in srcs or exported via exports_files, so it is not an addressable target.

How to fix it

List the real targets and fix the label

  1. Run the query the error suggests to see every target in the package.
  2. Update the label to a target that actually exists.
  3. If a file was intended, add it to the rule srcs or exports_files.
Terminal
bazel query "//foo:*"

Export a file you need to reference

Declare the file so other packages can address it as a target.

foo/BUILD.bazel
exports_files(["data.json"])

How to prevent it

  • Keep target names stable, or update all referencing labels when you rename.
  • Use bazel query "//pkg:*" to confirm a target exists before referencing it.
  • Export files explicitly when they must be visible to other packages.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →