Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

Commit

Permalink
fix(build): Prevent duplicated client/server generated code (hyperium…
Browse files Browse the repository at this point in the history
…#121)

* fix(build): Prevent duplicated client/server generated code

The tonic-build process collects up RPC services as they are provided by
prost, before writing them out as part of the finalization step for a
given protocol buffer package.

In the case of imported protocol buffer packages, there may be RPC
services included by import in addition to those in the top-level
package. Therefore it is necessary to make sure each set of
client/server services gathered by tonic-build is cleared after the
finalization process for a given protocol buffer package, otherwise they
will be incorrectly aggregated as the generation process proceeds
through the subsequent packages.

* Test case for duplicated client/server generated code

A simple test case that will fail to build without a fix to prevent
RPC services being duplicated into inappropriate modules (that related
to particular protocol buffer packages).

* Additional test case for included_service

Introduces an additional case that captures making sure services defined
before including a package with additional services doesn't
incidentially clear such precursor services from the including package.

* Fix unnecessary newline to keep `cargo fmt` happy
  • Loading branch information
steele authored and rabbitinspace committed Jan 1, 2020
1 parent b1dedc9 commit e339ef9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"tonic-examples",
"tonic-interop",

"tests/included_service",
"tests/same_name",
"tests/wellknown",
]
16 changes: 16 additions & 0 deletions tests/included_service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "included_service"
version = "0.1.0"
authors = ["Lucio Franco <luciofranco14@gmail.com>"]
edition = "2018"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tonic = { path = "../../tonic" }
bytes = "0.4"
prost = "0.5"

[build-dependencies]
tonic-build = { path = "../../tonic-build" }
3 changes: 3 additions & 0 deletions tests/included_service/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/includer.proto").unwrap();
}
11 changes: 11 additions & 0 deletions tests/included_service/proto/includee.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package includee;

service Included {
rpc SomeMethod(SomeRequest) returns (SomeResponse) {}
}

message SomeRequest {}

message SomeResponse {}
11 changes: 11 additions & 0 deletions tests/included_service/proto/includer.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package includer;

message TopMessage {}

service TopService {
rpc TopMethod(TopMessage) returns (TopMessage) {}
}

import "includee.proto";
8 changes: 8 additions & 0 deletions tests/included_service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod pb {
tonic::include_proto!("includer");
}

// Ensure that an RPC service, defined before including a file that defines
// another service in a different protocol buffer package, is not incorrectly
// cleared from the context of its package.
type _Test = dyn pb::server::TopService;
4 changes: 4 additions & 0 deletions tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator {

let code = format!("{}", client_service);
buf.push_str(&code);

self.clients = TokenStream::default();
}

if self.builder.build_server && !self.servers.is_empty() {
Expand All @@ -279,6 +281,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator {

let code = format!("{}", server_service);
buf.push_str(&code);

self.servers = TokenStream::default();
}
}
}
Expand Down

0 comments on commit e339ef9

Please sign in to comment.