larry
larry

Reputation: 211

How do I get output files for a given Bazel target?

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.

Upvotes: 8

Views: 10203

Answers (4)

Eugene Yokota
Eugene Yokota

Reputation: 95604

Recent versions of Bazel now comes with this feature built-in as --output files.

bazel cquery //a/b:bundle --output files 2>/dev/null

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

Upvotes: 3

Ming Zhao
Ming Zhao

Reputation: 11

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

Upvotes: 1

Eugene Yokota
Eugene Yokota

Reputation: 95604

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.

Upvotes: 6

Christopher Parsons
Christopher Parsons

Reputation: 337

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

Upvotes: 1

Related Questions