bufbuild / protovalidate

Protocol Buffer Validation - Go, Java, Python, and C++ Beta Releases!
https://buf.build/bufbuild/protovalidate
Apache License 2.0
837 stars 36 forks source link

Set default visibility to public for proto files #238

Open CristianNeamtu-47684 opened 3 weeks ago

CristianNeamtu-47684 commented 3 weeks ago

Feature description: Set default package visibility to public for proto files since they can be seen as an api specification

Problem it solves or use case: A user of protovalidate may decide to provide their own protobuf definitions for others to use and to generate code based on them. With bazel this is more difficult because they are not publicly exported with the github.com/bufbuild/protovalidate-go.

This can be worked around by either copying them in the 3rd party protos or copying them via a git_repository. With either option it's easy to be out of sync with the go dependency as time passes.

This is useful when working with other individuals or teams that have not transitioned to buf

Proposed implementation or solution: Define default package visibility as public

package(default_visibility = ["//visibility:public"])

Contribution: If approved and I can raise a pull-request with the abode change added to the BUILD.bazel

Examples or references: The deprecated protoc-gen-validate BUILD.bzazel Google APIs BUILD.bazel

These references allow shipping proto dependenices together with the user's proto definitions.

Additional context: Bazel query

:> bazel query '@buf_deps//buf/validate:*'
@buf_deps//buf/validate:BUILD.bazel
@buf_deps//buf/validate:expression.proto |
@buf_deps//buf/validate:validate.proto   | <-- desired files
@buf_deps//buf/validate:validate_proto

Our usecase

:> cat proto/BUILD.bazel

load("@rules_pkg//:pkg.bzl", "pkg_zip")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")

pkg_files(
    name = "protovalidate",
    srcs = [
        "@buf_deps//buf/validate:expression.proto",
        "@buf_deps//buf/validate:validate.proto",
    ]
)

# (...)

pkg_zip(
    name = "pkg_proto",
    srcs = [
      # (...)
      "//proto/ours/api.proto",
      # (...)
      ":protovalidate",
    ],
    package_file_name = "proto.zip",
    visibility = ["//visibility:public"],
)
:> bazel build :protovalidate

ERROR: /users/foo/projects/my-project/proto/BUILD.bazel:4:10: in pkg_files rule //proto:protovalidate: target '@buf_deps//buf/validate:expression.proto' is not visible from target '//proto:protovalidate'. Check the visibility declaration of the former target if you think the dependency is legitimate. To set the visibility of that source file target, use the exports_files() function
ERROR: /users/foo/projects/my-project/proto/BUILD.bazel:4:10: in pkg_files rule //proto:protovalidate: target '@buf_deps//buf/validate:validate.proto' is not visible from target '//proto:protovalidate'. Check the visibility declaration of the former target if you think the dependency is legitimate. To set the visibility of that source file target, use the exports_files() function
ERROR: /users/foo/projects/my-project/proto/BUILD.bazel:4:10: Analysis of target '//proto:protovalidate' failed
ERROR: Analysis of target '//proto:protovalidate' failed; build aborted:
INFO: Elapsed time: 0.620s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 2 targets configured)
CristianNeamtu-47684 commented 3 weeks ago

Upon a closer inspection proto/protovalidate/buf/validate/validate.proto imports buf/validate/priv/private.proto.

Is there another way we can enable others to generate code (without buf) while also providing/managing dependencies? For example: C#, Ruby

CristianNeamtu-47684 commented 1 week ago

Hi, Could you please let me know if there are any options to export the proto files as third_party/ dependencies?

This is useful for people that use @grpc/proto-loader or protoc directly, where proto files need to be explicitly added to path.

Thank you!