Skip to content

proto_common.compile() should not require plugin_output parameter #18263

@haberman

Description

@haberman

Description of the bug:

proto_common.compile() has a plugin_output parameter that a user must pass to specify where the output must go. But specifying it properly requires some tricky logic to handle all edge cases. Users have to write something like this:

load("//tools/build_defs/proto:proto_common.bzl", "proto_common")

def output_dir(ctx, proto_info):
    """Returns the output directory where generated proto files will be placed.

    Args:
      ctx: Rule context.
      proto_info: ProtoInfo provider.

    Returns:
      A string specifying the output directory
    """
    proto_root = proto_info.proto_source_root
    if proto_root.startswith(ctx.bin_dir.path):
        path = proto_root
    else:
        path = ctx.bin_dir.path + "/" + proto_root

    if proto_root == ".":
        path = ctx.bin_dir.path
    return path

def proto_common_compile(ctx, proto_info, proto_lang_toolchain_info, generated_files):
    """A wrapper around proto_common.compile that automatically calculates the output dir.

    Args:
      ctx: Rule context.
      proto_info: ProtoInfo provider.
      proto_lang_toolchain_info: ProtoLangToolchainInfo provider.
      generated_files: The files we expect to be generated from this protoc invocation.
    """
    proto_common.compile(
        actions = ctx.actions,
        proto_info = proto_info,
        proto_lang_toolchain_info = proto_lang_toolchain_info,
        generated_files = generated_files,
        plugin_output = output_dir(ctx, proto_info),
    )

proto_common.compile() should handle this internally, rather than making users copy and paste this code.

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Use proto_common.compile() without the special code above and watch it break with errors like:

ERROR: /usr/local/google/home/haberman/.cache/bazel/_bazel_haberman/17449526a2508fe9f4def2619c761b7e/external/com_google_googleapis/BUILD.bazel:31:14: output 'external/com_google_googleapis/google/rpc/status.upb.h' was not created

Which operating system are you running Bazel on?

Linux

What is the output of bazel info release?

release 6.1.2

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

Metadata

Metadata

Assignees

Labels

P2We'll consider working on this in future. (Assignee optional)team-Rules-APIAPI for writing rules/aspects: providers, runfiles, actions, artifactstype: feature request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions