Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to build all buildable targets and test all testable targets #4257

Closed
alexeagle opened this issue Dec 8, 2017 · 23 comments
Closed

How to build all buildable targets and test all testable targets #4257

alexeagle opened this issue Dec 8, 2017 · 23 comments
Labels
category: misc > misc P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) type: feature request

Comments

@alexeagle
Copy link
Contributor

I want to "build and test all the things" on my CI.

bazel test ... is bad because it doesn't build a target unless some test depends on it. (probably surprising to new users)

bazel build ... && bazel test ... is bad because the first test doesn't start running until the slowest build target is built

At Google we give up on this, and use build_test (alluded to at https://docs.bazel.build/versions/master/be/functions.html#load) which is a special test target that always passes, but consumes the files from some given deps, forcing those deps to build, and thus making the test either fail with a build error, or pass. But I think this is bad because it's easy to forget to create a build_test for some target, and we shouldn't expect everyone to be ever-watchful for that gotcha.

Is there some other way I don't know of?

@iirina
Copy link
Contributor

iirina commented Dec 8, 2017

There's no way to do this (that I'm aware of), since building all buildable targets and testing all testable targets involve 2 different bazel commands with two different sets of targets. Unfortunately it's not how bazel was designed.

bazel build ... && bazel test ... is bad because the first test doesn't start running until the slowest build target is built

On the other hand, when bazel test starts, everything needed would have already been built.

CC: @aehlig @buchgr does BEP help in this case?

@buchgr
Copy link
Contributor

buchgr commented Dec 8, 2017

@iirina wouldn't know how.

@alexeagle why don't you do bazel test ... && bazel build ...? The test invocation builds any build targets required by test targets, and the build invocation builds any remaining build targets (as the other ones are in cache).

@laszlocsomor
Copy link
Contributor

bazel test $(bazel query //...) ?

@buchgr
Copy link
Contributor

buchgr commented Dec 8, 2017

bazel test $(bazel query //...) ?

Doesn't this fail for non-test targets?

Nevermind. Nice!

@laszlocsomor
Copy link
Contributor

laszlocsomor commented Dec 8, 2017

No, you can "bazel test" a non-test target. Bazel will warn you about it, but will build it anyway.
The caveat with my proposal though is that it'll likely exhaust the command length limit.

@alexeagle
Copy link
Contributor Author

@buchgr bazel test ... && bazel build ... might help in common cases, but you might still have a long-running non-test target like building your production server docker image. I could imagine a critical path becoming longer when it waits for all tests to finish first.

@laszlocsomor that's basically what I wish Bazel could do, without exhausting the command-line limit which seems like an easy pit to fall into.

Consider this a feature request?

@laszlocsomor
Copy link
Contributor

@alexeagle : I'm afraid we don't have free cycles to work on this feature request right now.

@laszlocsomor laszlocsomor added type: feature request category: misc > misc P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) labels Dec 11, 2017
@buchgr
Copy link
Contributor

buchgr commented Dec 13, 2017

@laszlocsomor that's basically what I wish Bazel could do, without exhausting the command-line limit which seems like an easy pit to fall into.

bazel query //... | xargs bazel test should do the trick.

@alexeagle
Copy link
Contributor Author

@buchgr not portable, but my main concern for this is CI where I control the environment. I can do this just for Linux/Mac builds to start with, and let Windows testing be slower.
Thanks!

@alexeagle
Copy link
Contributor Author

It's a bit more tricky. We use tags=["manual"] to exclude tests from the test suite since it's not matched by //....
To restore that behavior, we also need --test_tag_filters=-manual on the bazel test command

@drigz
Copy link
Contributor

drigz commented May 17, 2018

bazel test ... is bad because it doesn't build a target unless some test depends on it. (probably surprising to new users)

Can you clarify this? If I have a basic workspace with a cc_binary and a cc_test, bazel test //... builds both and then runs the test. If I pass --build_tests_only then it behaves as you described.

@alexeagle
Copy link
Contributor Author

alexeagle commented May 17, 2018 via email

@drigz
Copy link
Contributor

drigz commented May 17, 2018

Even without the cc_test, the cc_binary gets built:

> cat BUILD.bazel
cc_binary(
    name = "main",
    srcs = ["main.cc"],
)

#cc_test(
#    name = "test",
#    srcs = ["test.cc"],
#)
> ls bazel-bin
ls: cannot access 'bazel-bin': No such file or directory
> bazel test //...
INFO: Analysed target //:main (6 packages loaded).
INFO: Found 1 target and 0 test targets...
Target //:main up-to-date:
  bazel-bin/main
INFO: Elapsed time: 1.043s, Critical Path: 0.24s
INFO: 3 processes: 2 linux-sandbox, 1 local.
INFO: Build completed successfully, 6 total actions
ERROR: No test targets were found, yet testing was requested
> ls bazel-bin
main  main-2.params  main.runfiles  main.runfiles_manifest  _objs

@alexeagle
Copy link
Contributor Author

alexeagle commented May 17, 2018 via email

@drigz
Copy link
Contributor

drigz commented May 18, 2018

Yep. The bazel output also shows that it has built the binary (6 total actions) even through no test targets were found.

@oferb
Copy link

oferb commented May 25, 2018

@alexeagle hey :) does this mean we can simplify the instructions at
https://github.com/alexeagle/angular-bazel-example/wiki/Continuous-Integration#build-and-test-all-the-things
to just the bazel test --config=ci //... part?

@alexeagle
Copy link
Contributor Author

Wow, I'm surprised to learn this. We have an elaborate build_test rule internally that creates a trivial test which depends on building a target, just to work around this. I suppose it was fixed by accident in some Bazel change - @laszlocsomor any idea what changed?

@laszlocsomor
Copy link
Contributor

@alexeagle : sorry, to learn what? I don't know if anything changed in Bazel about how it handles "...".

@alexeagle
Copy link
Contributor Author

I think target expansion is not what changed - it's that you can use bazel test with some pattern that includes binary targets, and these will be re-built. Meaning the workarounds in this issue are no longer needed, and bazel test //... is exhaustive to build the buildable things and test the testable things in your project.

Confirmed this is the case under Blaze as well.

Ubehebe added a commit to Ubehebe/r5js that referenced this issue Mar 26, 2019
the behavior of bazel changed sometime in mid-2018 so that `test //...`
now builds binaries, even if they aren't depended on by tests. see
bazelbuild/bazel#4257.
@jxramos
Copy link

jxramos commented Feb 24, 2020

Having something close to bazel test --query "kind('py_test', '//...')" would be really ideal since the management would be a lot more tidy. In my case I'm in the process of upgrading python pytest's for our repo and would like to run all the bazel py_test rules.

@ishaangandhi
Copy link

bazel test //... is exhaustive to build the buildable things and test the testable things in your project.

Is this still true as of Bazel 4.2.1? It is not building all binaries for me.

@alexeagle
Copy link
Contributor Author

@ishaangandhi maybe you have the --build_tests_only flag?

@ishaangandhi
Copy link

Thanks! Adding --nobuild_tests_only made it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: misc > misc P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) type: feature request
Projects
None yet
Development

No branches or pull requests

8 participants