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

[Bug]: ts_proto_library does not work with import_prefix and strip_import_prefix in proto_library #476

Open
eaplatanios opened this issue Nov 2, 2023 · 1 comment
Labels
bug Something isn't working help wanted Aspect isn't prioritizing this, but the community could

Comments

@eaplatanios
Copy link

What happened?

This is the underlying cause of what I posted here. It turns out that if you use import_prefix or strip_import_prefix in a proto_library, ts_proto_library will fail with an error that looks like this:

Could not find file in descriptor database: bazel-out/darwin_arm64-opt/bin/util/_virtual_imports/proto-library/sc/proto/util/v1/util.proto: No such file or directory

Version

Development (host) and target OS/architectures: Mac OS

Output of bazel --version: 6.4.0

Version of the Aspect rules, or other relevant rules from your
WORKSPACE or MODULE.bazel file: 2.0.0-rc1

Language(s) and/or frameworks involved: TypeScript

How to reproduce

Have a directory structure like this:


util/
  proto/
    sc/
      proto/
        util/
          v1/
            util.proto
  BUILD.bazel
WORKSPACE.bazel

with a BUILD.bazel file with these contents:

proto_library(
    name = "proto-library",
    srcs = glob(["proto/**/*.proto"]),
    strip_import_prefix = "/" + package_name() + "/proto",
    visibility = ["//visibility:public"],
    deps = ["@com_google_protobuf//:any_proto"],
)

ts_proto_library(
    name = "ts-proto-library",
    node_modules = "//:node_modules",
    proto = ":proto-library",
    visibility = ["//visibility:public"],
)

Note that py_proto_library is able to handle this scenario well such that the following builds file:

py_proto_library(
    name = "py-proto-library",
    output_mode = "NO_PREFIX",
    protos = [":proto-library"],
    visibility = ["//visibility:public"],
)


### Any other information?

_No response_
@eaplatanios eaplatanios added the bug Something isn't working label Nov 2, 2023
@github-actions github-actions bot added the untriaged Requires traige label Nov 2, 2023
@eaplatanios
Copy link
Author

I resolved this by using rules_proto_grpc and creating a TS plugin as follows:

# bazel/proto/BUILD.bazel

load("@npm//:@bufbuild/protoc-gen-connect-es/package_json.bzl", protoc_gen_connect_es_bin = "bin")
load("@npm//:@bufbuild/protoc-gen-es/package_json.bzl", protoc_gen_es_bin = "bin")
load("@rules_proto_grpc//:defs.bzl", "proto_plugin")

protoc_gen_es_bin.protoc_gen_es_binary(name = "protoc-gen-es")

proto_plugin(
    name = "ts_proto_plugin",
    env = {"BAZEL_BINDIR": "{bindir}"},
    options = [
        "target=ts",
        "import_extension=none",
    ],
    outputs = ["{protopath}_pb.ts"],
    protoc_plugin_name = "es",
    tool = "protoc-gen-es",
    use_built_in_shell_environment = False,
    visibility = ["//visibility:public"],
)

protoc_gen_connect_es_bin.protoc_gen_connect_es_binary(name = "protoc-gen-connect-es")

proto_plugin(
    name = "grpc_ts_plugin",
    env = {"BAZEL_BINDIR": "{bindir}"},
    options = [
        "target=ts",
        "import_extension=none",
        "keep_empty_files=true",
    ],
    outputs = ["{protopath}_connect.ts"],
    protoc_plugin_name = "connect-es",
    tool = "protoc-gen-connect-es",
    use_built_in_shell_environment = False,
    visibility = ["//visibility:public"],
)
# bazel/proto/defs.bzl

"""Defines a macro for constructing Python Protobuf rules."""

load(
    "@rules_proto_grpc//:defs.bzl",
    "ProtoPluginInfo",
    "proto_compile_impl",
)

ts_proto_compile = rule(
    implementation = proto_compile_impl,
    attrs = dict(
        proto_compile_attrs,
        _plugins = attr.label_list(
            providers = [ProtoPluginInfo],
            default = [
                Label("//bazel/proto:ts_proto_plugin"),
                Label("//bazel/proto:grpc_ts_plugin"),
            ],
            doc = "List of protoc plugins to apply.",
        ),
    ),
    toolchains = [str(Label("@rules_proto_grpc//protobuf:toolchain_type"))],
)

@alexeagle alexeagle added help wanted Aspect isn't prioritizing this, but the community could and removed untriaged Requires traige labels Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Aspect isn't prioritizing this, but the community could
Projects
Status: No status
Development

No branches or pull requests

2 participants