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

Runfiles for go_proto_compiler plugins #2895

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions proto/compiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ def go_proto_compile(go, compiler, protos, imports, importpath):
direct = [
compiler.internal.go_protoc,
compiler.internal.protoc,
compiler.internal.plugin,
],
] + compiler.internal.data,
transitive = [transitive_descriptor_sets],
),
outputs = go_srcs,
progress_message = "Generating into %s" % go_srcs[0].dirname,
mnemonic = "GoProtocGen",
executable = compiler.internal.go_protoc,
tools = [compiler.internal.plugin],
arguments = [args],
env = go.env,
# We may need the shell environment (potentially augmented with --action_env)
Expand Down Expand Up @@ -166,13 +166,19 @@ def _go_proto_compiler_impl(ctx):
go = go_context(ctx)
library = go.new_library(go)
source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())

expanded_options = []
for opt in ctx.attr.options:
expanded_options.append(ctx.expand_location(opt, ctx.attr.data))

return [
GoProtoCompiler(
deps = ctx.attr.deps,
compile = go_proto_compile,
valid_archive = ctx.attr.valid_archive,
internal = struct(
options = ctx.attr.options,
data = ctx.files.data,
options = expanded_options,
suffix = ctx.attr.suffix,
protoc = ctx.executable._protoc,
go_protoc = ctx.executable._go_protoc,
Expand All @@ -187,6 +193,7 @@ def _go_proto_compiler_impl(ctx):
_go_proto_compiler = rule(
implementation = _go_proto_compiler_impl,
attrs = {
"data": attr.label_list(allow_files = True),
"deps": attr.label_list(providers = [GoLibrary]),
"options": attr.string_list(),
"suffix": attr.string(default = ".pb.go"),
Expand Down
7 changes: 6 additions & 1 deletion proto/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Go Protocol buffers
.. _GoArchive: /go/providers.rst#goarchive
.. _Gazelle: https://github.com/bazelbuild/bazel-gazelle
.. _Make variable substitution: https://docs.bazel.build/versions/master/be/make-variables.html#make-var-substitution
.. _Location expansion: https://docs.bazel.build/versions/main/be/make-variables.html#predefined_label_variables
.. _Bourne shell tokenization: https://docs.bazel.build/versions/master/be/common-definitions.html#sh-tokenization
.. _gogoprotobuf: https://github.com/gogo/protobuf
.. _compiler.bzl: compiler.bzl
Expand Down Expand Up @@ -396,7 +397,11 @@ Attributes
| :param:`options` | :type:`string_list` | :value:`[]` |
+-----------------------------+----------------------+-----------------------------------------------------+
| List of command line options to be passed to the compiler. Each option will |
| be preceded by ``--option``. |
| be preceded by ``--option``. Subject to `Make variable substitution`_. |
+-----------------------------+----------------------+-----------------------------------------------------+
| :param:`data` | :type:`label_list` | :value:`[]` |
+-----------------------------+----------------------+-----------------------------------------------------+
| List of files that will be available to the compiler at runtime. |
+-----------------------------+----------------------+-----------------------------------------------------+
| :param:`suffix` | :type:`string` | :value:`.pb.go` |
+-----------------------------+----------------------+-----------------------------------------------------+
Expand Down
91 changes: 91 additions & 0 deletions tests/core/go_proto_compiler/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//proto:compiler.bzl", "go_proto_compiler")
load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
)

go_library(
name = "executable_runfiles_lib",
srcs = ["executable_runfiles_plugin.go"],
importpath = "github.com/bazelbuild/rules_go/tests/core/go_proto_compiler/executable_runfiles",
deps = [
"//go/runfiles",
"@org_golang_google_protobuf//compiler/protogen",
],
)

go_binary(
name = "protoc-gen-executable_runfiles",
data = ["executable_runfiles.conf"],
embed = [":executable_runfiles_lib"],
)

go_proto_compiler(
name = "executable_runfiles",
plugin = ":protoc-gen-executable_runfiles",
suffix = ".executable_runfiles.pb.go",
)

go_proto_library(
name = "foo_go_proto",
compilers = [
"@io_bazel_rules_go//proto:go_proto",
":executable_runfiles",
],
importpath = "github.com/bazelbuild/rules_go/tests/core/go_proto_compiler/foo",
proto = ":foo_proto",
)

go_test(
name = "executable_runfiles_test",
srcs = ["executable_runfiles_test.go"],
deps = [
":foo_go_proto",
],
)

go_library(
name = "data_lib",
srcs = ["data_plugin.go"],
importpath = "github.com/bazelbuild/rules_go/tests/core/go_proto_compiler/data",
deps = [
"//go/runfiles",
"@org_golang_google_protobuf//compiler/protogen",
],
)

go_binary(
name = "protoc-gen-data",
data = ["data.conf"],
embed = [":data_lib"],
)

go_proto_compiler(
name = "data",
data = ["data.conf"],
options = ["config=$(rootpath :data.conf)"],
plugin = ":protoc-gen-data",
suffix = ".data.pb.go",
)

go_proto_library(
name = "foo_go_proto_with_data",
compilers = [
"@io_bazel_rules_go//proto:go_proto",
":data",
],
importpath = "github.com/bazelbuild/rules_go/tests/core/go_proto_compiler/foo",
proto = ":foo_proto",
)

go_test(
name = "data_test",
srcs = ["data_test.go"],
deps = [
":foo_go_proto_with_data",
],
)
15 changes: 15 additions & 0 deletions tests/core/go_proto_compiler/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Basic go_proto_compiler functionality
=====================================

.. _go_proto_compiler: /proto/core.rst#_go_proto_compiler
.. _#2704: https://github.com/bazelbuild/rules_go/issues/2704

Tests to ensure the basic features of `go_proto_compiler`_ are working.

.. contents::

executable_runfiles_test
------------------------

Checks that the `plugin` executable of `go_proto_compiler` has access to its `data` runfiles when the compile action is run.
Verifies `#2704`_.
1 change: 1 addition & 0 deletions tests/core/go_proto_compiler/data.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hallo Welt!
49 changes: 49 additions & 0 deletions tests/core/go_proto_compiler/data_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"flag"
"strings"

"google.golang.org/protobuf/compiler/protogen"

"github.com/bazelbuild/rules_go/go/runfiles"
)

var config []byte

func main() {
var flags flag.FlagSet
configPath := flags.String("config", "", "path to config")

protogen.Options{ParamFunc: flags.Set}.Run(func(gen *protogen.Plugin) error {
if *configPath == "" {
panic("provide config path")
}
runfiles, err := runfiles.New()
if err != nil {
panic(err)
}
config, err = runfiles.ReadFile(*configPath)
if err != nil {
panic(err)
}

for _, f := range gen.Files {
if f.Generate {
filename := f.GeneratedFilenamePrefix + ".data.pb.go"
g := gen.NewGeneratedFile(filename, f.GoImportPath)
generate(g, f)
}
}
return nil
})
}

func generate(g *protogen.GeneratedFile, f *protogen.File) {
g.P("package ", f.GoPackageName)
g.P()
for _, msg := range f.Messages {
greeting := strings.TrimSpace(string(config))
g.P("const ", msg.GoIdent.GoName, "_greeting2 = `", greeting, "`")
}
}
16 changes: 16 additions & 0 deletions tests/core/go_proto_compiler/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package data_test

import (
"github.com/bazelbuild/rules_go/tests/core/go_proto_compiler/foo"
"testing"
)

func TestData(t *testing.T) {
// Foo_greeting2 is a string generated by the data plugin
// using the content of the data.conf file as value.
if foo.Foo_greeting2 != "Hallo Welt!" {
t.Logf(`Got : "%s"`, foo.Foo_greeting2)
t.Logf(`Want: "Hallo Welt!"`)
t.FailNow()
}
}
1 change: 1 addition & 0 deletions tests/core/go_proto_compiler/executable_runfiles.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
45 changes: 45 additions & 0 deletions tests/core/go_proto_compiler/executable_runfiles_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"strings"

"google.golang.org/protobuf/compiler/protogen"

"github.com/bazelbuild/rules_go/go/runfiles"
)

var (
configPath = "tests/core/go_proto_compiler/executable_runfiles.conf"
config []byte
)

func main() {
runfiles, err := runfiles.New()
if err != nil {
panic(err)
}
config, err = runfiles.ReadFile(configPath)
if err != nil {
panic(err)
}

protogen.Options{}.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if f.Generate {
filename := f.GeneratedFilenamePrefix + ".executable_runfiles.pb.go"
g := gen.NewGeneratedFile(filename, f.GoImportPath)
generate(g, f)
}
}
return nil
})
}

func generate(g *protogen.GeneratedFile, f *protogen.File) {
g.P("package ", f.GoPackageName)
g.P()
for _, msg := range f.Messages {
greeting := strings.TrimSpace(string(config))
g.P("const ", msg.GoIdent.GoName, `_greeting = "`, greeting, `"`)
}
}
14 changes: 14 additions & 0 deletions tests/core/go_proto_compiler/executable_runfiles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package executable_runfiles_test

import (
"github.com/bazelbuild/rules_go/tests/core/go_proto_compiler/foo"
"testing"
)

func TestExecutableRunfiles(t *testing.T) {
// Foo_greeting is a string generated by the executable_runfiles plugin
// using the content of the executable_runfiles.conf file as value.
if foo.Foo_greeting != "Hello World!" {
t.FailNow()
}
}
8 changes: 8 additions & 0 deletions tests/core/go_proto_compiler/foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package tests.core.go_proto_library.foo;
option go_package = "github.com/bazelbuild/rules_go/tests/core/go_proto_library/foo";

message Foo {
int64 value = 1;
}