简体   繁体   中英

How can I find all source files included in multiple bazel targets?

In my codebase it's often a mistake to include the same source file in multiple targets ( cc_binary , cc_library , etc). I'd like to detect this.

I can do

bazel query labels(srcs, //target:name)

to get a list of sources for a single target. This is enough to figure out which sources are part of multiple targets, by doing this for every target and detecting which source files are in multiple targets. But I'd still need a query to get me a list of every target.

It would be a lot easier/cleaner if I could just query which targets a source file belongs to and see if it's a source of multiple targets. Is that possible?

Also, I have a lot of source files, optimalness of the query is a concern too.

A few clarifying questions:

  • With what frequency would you like to detect this kind of scenario? (ie Do you want this run as a test, or do you want to manually verify this?)
  • Is it considered an error if two unrelated targets A and B both depend on file foo, even if A does not depend on B, nor does B depend on A?
  • Is it insufficient to restrict src visibility to targets which are defined in the same package? For example, consider ensuring foo/bar/src.c is only visible to targets defined in foo/bar/BUILD , and then ensure that foo/bar/BUILD contains only one cc_library target.

Consider using aspects to obtain a list of all transitive source files of a given target.

If you're truly attached to using bazel query , you can get a comprehensive list of every target under your project by invoking, for example,

bazel query //...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM