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

Allow greater customization of environment variables by plugins. #226

Merged
merged 3 commits into from
Dec 4, 2022

Conversation

gonzojive
Copy link
Contributor

def _ts_proto_compile_impl(ctx):
    """
    Implementation function for ts_proto_compile.

    Args:
        ctx: The Bazel rule execution context object.

    Returns:
        Providers:
            - ProtoCompileInfo
            - DefaultInfo

    """
    base_env = {
        # Make up for https://github.com/bazelbuild/bazel/issues/15470.
        "BAZEL_BINDIR": ctx.bin_dir.path,
    }
    return proto_compile_impl(ctx, base_env = base_env)

ts_proto_compile = rule(
    implementation = _ts_proto_compile_impl,
    attrs = dict(
        proto_compile_attrs,
        _plugins = attr.label_list(
            providers = [ProtoPluginInfo],
            default = [
                Label("//bazel/web/targets:ts_proto_compile"),
            ],
            doc = "List of protoc plugins to apply",
        ),
    ),
    toolchains = [
        str(Label("@rules_proto_grpc//protobuf:toolchain_type")),
    ],
)
load("@npm//:ts-proto/package_json.bzl", _ts_proto_bin_factories = "bin")
load("@rules_proto_grpc//:defs.bzl", "proto_plugin")

_ts_proto_bin_factories.protoc_gen_ts_proto_binary(
    name = "protoc-gen-ts-proto",
)

proto_plugin(
    name = "ts_proto_compile",
    outputs = ["{protopath}.ts"],
    protoc_plugin_name = "ts_proto",
    tool = "//bazel/web/targets:protoc-gen-ts-proto",
    use_built_in_shell_environment = False,
    visibility = ["//visibility:public"],
)

* Add an "env" property to ProtoPluginInfo that contains an environment variable
  dictionary.

* Add a corresponding "env" attribute to the the proto_plugin rule.

* For cases where environment variables must specified based on ctx, as
  described in bazelbuild/bazel#15470 and
  aspect-build/rules_js#397, modify proto_compile_impl
  and proto_compile to accept a base_env dictionary argument. Example usage is
  below:

```starlark

def _ts_proto_compile_impl(ctx):
    """
    Implementation function for ts_proto_compile.

    Args:
        ctx: The Bazel rule execution context object.

    Returns:
        Providers:
            - ProtoCompileInfo
            - DefaultInfo

    """
    base_env = {
        # Make up for bazelbuild/bazel#15470.
        "BAZEL_BINDIR": ctx.bin_dir.path,
    }
    return proto_compile_impl(ctx, base_env = base_env)

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

```starlark
load("@npm//:ts-proto/package_json.bzl", _ts_proto_bin_factories = "bin")
load("@rules_proto_grpc//:defs.bzl", "proto_plugin")

_ts_proto_bin_factories.protoc_gen_ts_proto_binary(
    name = "protoc-gen-ts-proto",
)

proto_plugin(
    name = "ts_proto_compile",
    outputs = ["{protopath}.ts"],
    protoc_plugin_name = "ts_proto",
    tool = "//bazel/web/targets:protoc-gen-ts-proto",
    use_built_in_shell_environment = False,
    visibility = ["//visibility:public"],
)
```
@aaliddell aaliddell mentioned this pull request Nov 22, 2022
@aaliddell
Copy link
Member

Seems reasonable, thanks. The doc_template_compile_impl needs fixing, but I'll merge to dev and fix it there for 4.3.0. I'd be interested to see the result of getting protoc-gen-ts-proto working with rules_js, as there's ongoing problems with getting typescript to work with rules_nodejs in this repo and I'd like to see if there's a viable alternative 👍

@aaliddell aaliddell changed the base branch from master to dev November 22, 2022 22:39
@aaliddell
Copy link
Member

Ok, thanks for finding that reference. I'll take this now for 4.3.0 but will need a little rework to take the env from the plugin env attr as presently that is not checked

@aaliddell aaliddell merged commit e9b7119 into rules-proto-grpc:dev Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants