14

I am trying to query the list of Bazel targets having public visibility. Some of our Bazel targets have visibility specified explicitly, e.g.:

cc_library(
    name = "xxx_util",
    visibility = ["//visibility:public",],
    ...
)

while most targets are implicitly public, as their BUILD files have default public visibility specified, e.g.

package(default_visibility = ["//visibility:public"])

I need a list of all such targets, so that I can copy their output to a certain location automatically after my Bazel workspace is built.

I am new to Bazel and can't figure out the query...

Curious
  • 2,783
  • 3
  • 29
  • 45

2 Answers2

17

I think the previous answer does cover the attribute query for visibility attribute restricted results. For posterity, I would add that if you simply want to find all targets from current directory, you could do:

bazel query ...
Rubin Simons
  • 1,556
  • 1
  • 13
  • 20
  • 4
    I think you meant: `bazel query //...` at least this is what worked for me. – Sergey Dryganets Dec 01 '20 at 22:05
  • 1
    No, I meant just `...`; this worked (executed from the root of the project, not sure if that matters). – Rubin Simons Dec 03 '20 at 00:56
  • `...` means "everything under the current directory", so if your current directory is the root of the project then `...` and `//...` are equivalent. But if you `cd` into a subdirectory, `...` will start showing only those targets under the subdirectory, while `//...` will continue showing everything. – Jack O'Connor Jun 11 '22 at 19:42
11

Look at this example: https://docs.bazel.build/versions/master/query-how-to.html#which-of-those-are-small-medium-large

You need something like:

bazel query 'attr(visibility, "//visibility:public", //path/to/package:*)'

Update(2017-01-04): @Curious was asking about querying for the effective visibility of a target, i.e. taking default_visibility into account. AFAIK Bazel doesn't support that; see issue #4388.

László
  • 3,973
  • 1
  • 13
  • 26
  • 2
    or simply: bazel query 'attr(visibility, "//visibility:public", ...)' – Picaud Vincent Jan 03 '18 at 16:33
  • **This works, but only partially**, only for those targets which have the visibility attribute specified explicitly, e.g. `visibility = ["//visibility:public"],` . My problem is that many of of our `BUILD` files have package level visibility specified in them e.g. `package(default_visibility = ["//visibility:public"])` & I expect all targets in such `BUILD` files appear in my query... Looks like the package level visibility is not being propagated to the target, for querying purpose... – Curious Jan 04 '18 at 06:07
  • 2
    Ah, you need the effective visibility! I don't think bazel query can tell you that. I filed a feature request: https://github.com/bazelbuild/bazel/issues/4388 . Unfortunately we don't have anyone dedicated to working on bazel query right now, so this will probably have to wait. – László Jan 04 '18 at 08:46
  • For files that have default_visibility, use this to find the targets that don't change visibility: `bazel query 'attr(visibility, "", //path/to/package:*)'` With a bit of work, you should be able to combine the results and get what you need. I don't think we have a better solution at the moment. – Laurent Jan 05 '18 at 13:55