Skip to content

Commit b02b4b2

Browse files
steeleLucioFranco
authored andcommitted
fix(build): Prevent duplicated client/server generated code (#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
1 parent 4d4dbdf commit b02b4b2

File tree

7 files changed

+54
-0
lines changed

7 files changed

+54
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"tonic-examples",
66
"tonic-interop",
77

8+
"tests/included_service",
89
"tests/same_name",
910
"tests/wellknown",
1011
]

tests/included_service/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "included_service"
3+
version = "0.1.0"
4+
authors = ["Lucio Franco <luciofranco14@gmail.com>"]
5+
edition = "2018"
6+
publish = false
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
tonic = { path = "../../tonic" }
12+
bytes = "0.4"
13+
prost = "0.5"
14+
15+
[build-dependencies]
16+
tonic-build = { path = "../../tonic-build" }

tests/included_service/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
tonic_build::compile_protos("proto/includer.proto").unwrap();
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto3";
2+
3+
package includee;
4+
5+
service Included {
6+
rpc SomeMethod(SomeRequest) returns (SomeResponse) {}
7+
}
8+
9+
message SomeRequest {}
10+
11+
message SomeResponse {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto3";
2+
3+
package includer;
4+
5+
message TopMessage {}
6+
7+
service TopService {
8+
rpc TopMethod(TopMessage) returns (TopMessage) {}
9+
}
10+
11+
import "includee.proto";

tests/included_service/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub mod pb {
2+
tonic::include_proto!("includer");
3+
}
4+
5+
// Ensure that an RPC service, defined before including a file that defines
6+
// another service in a different protocol buffer package, is not incorrectly
7+
// cleared from the context of its package.
8+
type _Test = dyn pb::server::TopService;

tonic-build/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
262262

263263
let code = format!("{}", client_service);
264264
buf.push_str(&code);
265+
266+
self.clients = TokenStream::default();
265267
}
266268

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

280282
let code = format!("{}", server_service);
281283
buf.push_str(&code);
284+
285+
self.servers = TokenStream::default();
282286
}
283287
}
284288
}

0 commit comments

Comments
 (0)