6

Ideally, I'd like a list of output files for a target without building. I imagine this should be possible using cquery which runs post-analysis, but can't figure out how.

larry
  • 191
  • 1
  • 6

3 Answers3

4

Here's my output.cquery

def format(target):
  outputs = target.files.to_list()
  return outputs[0].path if len(outputs) > 0 else "(missing)"

You can run this as follows:

bazel cquery //a/b:bundle --output starlark \
  --starlark:file=output.cquery 2>/dev/null

bazel-out/darwin-fastbuild/bin/a/b/something-bundle.zip

For more information on cquery.

Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
1

What exactly do you mean by "output files" here? Do you mean that you'd like to know the files generated if you build the target on the command line?

At what point would you like to have this information? Do you really want to invoke a bazel query command to acquire this information, or would you like it during analysis? I don't think there's a way, using bazel query, to get the exact expected absolute path of output files (or even the workspace-relative path, for example, bazel-out/foo/bar/baz.txt)

It may be a bit more involved than you want, but Requesting Output Files has some information about specifying output files in Starlark, with a brief bit about acquiring information about your dependencies' output files (See DefaultInfo

  • "Do you mean that you'd like to know the files generated if you build the target on the command line?" Yes, exactly. "At what point would you like to have this information?" Ideally, I'd like to be able to run `bazel cquery 'kind("output files", //foo)'` in the command line and have it return a list of output files. Now that I think about it though, for the problem that I'm trying to solve, I could just create a bazel rule that dumps the output files of its dependencies into a text file. Of course, that would still require a build, so that doesn't answer my original question. – larry Oct 12 '18 at 18:40
  • "Do you mean that you'd like to know the files generated if you build the target on the command line?" To clarify, I want to know the files generated when you build the target excluding those from its dependencies. – larry Oct 12 '18 at 18:45
0

I made a slight improvement to Engene's answer, since a target's output might be multiple:

bazel cquery  --output=starlark  \
--starlark:expr="'\n'.join([f.path for f in target.files.to_list()])"  \
//foo:bar