Use a bazel query as a build shortcut? - bazel

Suppose you have this:
$ bazel query "filter('_image_publish$', attr(generator_function, go_server_v1, ...))"
//helloworld/server:zurigo_server_image_publish
//bababot:bababot_server_image_publish
Is it possible to create rules or macros that let me do a single bazel build the builds all the targets above?
I'd like to do:
$ bazel build :all-servers
Which would implicitly build the ones from the output above. Is this possible?
Another way to put it, I'm looking for a Skylark alternative to doing a loop using bash on the output of the query.

You can write a genquery() rule, which will write the query result targets into a file in bazel-bin.
The final command will look something like:
bazel build //package:my_genquery && cat bazel-bin/package/my_genquery | xargs bazel build

Related

Expand bazel command with bazelrc

given a bazel command, for example bazel build //..., is it possible to see what build command this expands to based on the bazelrc(s)?
bazelrcs are great to set default flags, like the default platform to build for setting some variables based on profiles.
They also allow for nested bazelrcs, that you can import and try-import leading to a rather complex expansion of bazel commands via bazelrcs.
To verify that the bazelrcs did indeed configure the correct things and did not override some flags accidentally, I would love to see the output of what bazel would expand a bazel command to.
Is that possible?
I tried setting --logging=0 and --logging=6 with bazel 5.3.0 but see no such output.
You can run your build using the --explain flag.
Something like
bazel build --explain explain.log //...
should produce a file explain.log where the first line contains the expanded flags.

Is it possible to run bazel build with a increased io-niceness value?

I would like to run bazel and all of its subprocesses at a higher IO-nice-ness level.
How can I achieve that?
You can use the bazel --io_nice_level=<your nice value here> option
Careful it's a startup option, so you need to pass that flag before the bazel action:
bazel --io_nice_level=8 build //...

How to pass custom flags in a `bazel query` command?

I try to query deps with a custom flags. For example,
bazel query --define=using_cuda=true "deps(#org_tensorflow//tensorflow/core:lib)"
Bazel complained:
ERROR: Unrecognized option: --define=using_cuda=true
How could I pass a custom flag to bazel query command?
bazel query doesn't support the --define flag because the command doesn't use it. Instead, use bazel cquery instead, which stands for "configurable query".

How to specify "default" target labels when running bazel test

We're considering migrating to Bazel from Make. To make the transition easier I would like to have bazel test (no flags / options) run the current directory's tests, if any.
So instead of bazel test my_tests bazel test would find the current directory's BUILD file, find any *_test rules and run those.
If you want to do exactly as you said then you can use your own script.
When you run “bazel” it actually looks to see if there is a script named “bazel” under tools directory of the current workspace. So if you have an executable under “$workspace/tools/bazel” bazel will run that instead of the bazel binary itself.
This means you can write a script that checks if the only argument is “test” and if so calls “bazel-real test :all”
It can also check the exit code to see if there were no tests (it’s a specific error code) and return 0 instead
You can use the all target pattern to match all targets in the current package: bazel test :all
You can read more about it here: https://docs.bazel.build/versions/master/user-manual.html#target-patterns
Note however that if there are no test targets in the current package, bazel will give an error: "ERROR: No test targets were found, yet testing was requested.". In this case bazel will give an exit code of 4: https://docs.bazel.build/versions/master/user-manual.html#what-exit-code-will-i-get
I recommend creating a alias called bazel-test to bazel test :all.

Running bazel build with an aspect on test targets does not yield test output jars

running bazel build //... \
--aspects some-aspect.bzl%some_aspect \
--output_groups=some_new_output,default
does not create test jars outputs.
on the other hand running bazel test does create the test jar outputs:
bazel test //... \
--aspects some-aspect.bzl%some_aspect \
--output_groups=some_new_output,default
How come?
This question was updated to reflect use of aspects:
The original question:
running bazel build //... does not add test code to output jar.
on the other hand bazel test //... builds the test code but also
runs it.
Is there a way in bazel to build the test code without running the
tests?
I had a mistake in the values I gave the --output_groups flag.
It should have been --output_groups=+some_new_output,+default
The default can even be omitted:
--output_groups=+some_new_output
This flag is not documented at all. There is an open issue on this in bazel github repo.
You may be looking for --build_tests_only.

Resources