Skip to content

Commit

Permalink
Fix proto_path to handle generated sources and external workspaces (#205
Browse files Browse the repository at this point in the history
)

The execdir is the effective proto sources root for a rule:
 - for normal sources, execdir is the normal Bazel EXECDIR
 - for generated sources, execdir is Bazel's GENDIR
 - for external sources, execdir is EXECDIR/external/<workspace name>
 - for external generated sources, execdir is GENDIR/external/<workspace>

This changes look through all of the transitive units for a rule and
adds the execdir from each unit to the proto path.
  • Loading branch information
bocon13 authored and pcj committed Apr 12, 2018
1 parent f013ad4 commit 1c0c6e2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
11 changes: 11 additions & 0 deletions protobuf/internal/proto_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,15 @@ def _check_if_protos_are_generated(ctx):
"""
)

def _add_imports_for_transitive_units(ctx, data, builder):
proto_paths = [ data.execdir ]
for unit in data.transitive_units:
if len(unit.data.protos) == 0:
continue
if unit.data.execdir not in proto_paths:
builder["imports"].append(_get_offset_path(data.execdir, unit.data.execdir))
proto_paths.append(unit.data.execdir)


def _proto_compile_impl(ctx):

Expand Down Expand Up @@ -632,6 +641,8 @@ def _proto_compile_impl(ctx):
"commands": [], # optional miscellaneous pre-protoc commands
}

_add_imports_for_transitive_units(ctx, data, builder)

# Build a list of structs that will be processed in this compiler
# run.
runs = []
Expand Down
55 changes: 55 additions & 0 deletions tests/generated_proto_file/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,59 @@ cpp_proto_library(
protos = [":subdir_copy_proto"],
with_grpc = True,
verbose = 0,
)

genrule(
name = "complicated_copy_proto",
srcs = ["complicated/complicated.proto"],
outs = ["complicated/comp_copy.proto"],
cmd = "cat $< > $@",
message = "copy",
)

genrule(
name = "complicated_copy_proto_copy",
srcs = ["complicated/complicated_copy.proto"],
outs = ["complicated/comp_copy2.proto"],
cmd = "cat $< > $@",
message = "copy",
)

cpp_proto_library(
name = "complicated_default_library",
protos = ["complicated/complicated.proto"],
verbose = 0,
proto_deps = [
":subdir_default_library",
],
inputs = [
"@com_google_protobuf//:well_known_protos",
],
)

cpp_proto_library(
name = "complicated_default_library_on_gen",
protos = ["complicated/complicated_copy.proto"],
verbose = 0,
proto_deps = [
":subdir_copy_library",
],
)

cpp_proto_library(
name = "complicated_copy_library",
protos = [":complicated_copy_proto"],
verbose = 0,
proto_deps = [
":subdir_default_library",
],
)

cpp_proto_library(
name = "complicated_copy_library_on_gen",
protos = [":complicated_copy_proto_copy"],
verbose = 0,
proto_deps = [
":subdir_copy_library",
],
)
10 changes: 10 additions & 0 deletions tests/generated_proto_file/complicated/complicated.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

import "tests/generated_proto_file/subdir/simple.proto";

package complicated;

message Test {
subdir.Test test = 1;
}

9 changes: 9 additions & 0 deletions tests/generated_proto_file/complicated/complicated_copy.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

import "tests/generated_proto_file/subdir/copy.proto";

package complicated;

message Test {
subdir.Test test = 1;
}
2 changes: 1 addition & 1 deletion tests/generated_proto_file/subdir/simple.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
syntax = "proto3";

package simple;
package subdir;

message Test {
string value = 1;
Expand Down

0 comments on commit 1c0c6e2

Please sign in to comment.