Skip to content

Fix RPC name conflicts with generated code #1261

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

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 2 additions & 1 deletion tonic-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ version = "0.8.0"
prettyplease = {version = "0.1"}
proc-macro2 = "1.0"
prost-build = {version = "0.11", optional = true}
prost-reflect-build = {version = "0.11", optional = true}
quote = "1.0"
syn = "1.0"

[features]
default = ["transport", "prost"]
prost = ["prost-build"]
prost = ["prost-build", "prost-reflect-build"]
transport = []

[package.metadata.docs.rs]
Expand Down
10 changes: 5 additions & 5 deletions tonic-build/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Attributes, Method, Service};
use crate::{generate_doc_comments, naive_snake_case};
use crate::{generate_doc_comments, naive_snake_case, sanitize_name};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

Expand Down Expand Up @@ -174,7 +174,7 @@ fn generate_unary<T: Method>(
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();
let ident = format_ident!("{}", method.name());
let ident = format_ident!("{}", sanitize_name(method.name()));
let (request, response) = method.request_response_name(proto_path, compile_well_known_types);

quote! {
Expand All @@ -199,7 +199,7 @@ fn generate_server_streaming<T: Method>(
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();
let ident = format_ident!("{}", method.name());
let ident = format_ident!("{}", sanitize_name(method.name()));

let (request, response) = method.request_response_name(proto_path, compile_well_known_types);

Expand All @@ -225,7 +225,7 @@ fn generate_client_streaming<T: Method>(
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();
let ident = format_ident!("{}", method.name());
let ident = format_ident!("{}", sanitize_name(method.name()));

let (request, response) = method.request_response_name(proto_path, compile_well_known_types);

Expand All @@ -251,7 +251,7 @@ fn generate_streaming<T: Method>(
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();
let ident = format_ident!("{}", method.name());
let ident = format_ident!("{}", sanitize_name(method.name()));

let (request, response) = method.request_response_name(proto_path, compile_well_known_types);

Expand Down
11 changes: 11 additions & 0 deletions tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,17 @@ fn naive_snake_case(name: &str) -> String {
s
}

fn sanitize_name(name: &str) -> String {
let mut ident: String = name.into();
match ident.as_str() {
"connect" | "new" | "send_gzip" | "accept_gzip" => {
ident.insert(0, '_')
},
_ => (),
};
ident
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
29 changes: 27 additions & 2 deletions tonic-build/src/prost.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::{client, server, Attributes};
use proc_macro2::TokenStream;
use prost_build::{Config, Method, Service};
use prost_reflect_build::Builder as ProstBuilder;
use quote::ToTokens;
use std::{
borrow::BorrowMut,
ffi::OsString,
io,
path::{Path, PathBuf},
Expand All @@ -16,6 +18,7 @@ pub fn configure() -> Builder {
build_client: true,
build_server: true,
file_descriptor_set_path: None,
descriptor_pool: None,
out_dir: None,
extern_path: Vec::new(),
field_attributes: Vec::new(),
Expand Down Expand Up @@ -215,6 +218,7 @@ pub struct Builder {
pub(crate) build_client: bool,
pub(crate) build_server: bool,
pub(crate) file_descriptor_set_path: Option<PathBuf>,
pub(crate) descriptor_pool: Option<String>,
pub(crate) extern_path: Vec<(String, String)>,
pub(crate) field_attributes: Vec<(String, String)>,
pub(crate) type_attributes: Vec<(String, String)>,
Expand Down Expand Up @@ -258,6 +262,13 @@ impl Builder {
self
}

/// Set the variable name that will be used for the descriptor pool when generating
/// reflection builds
pub fn descriptor_pool_name(mut self, name: impl Into<String>) -> Self {
self.descriptor_pool = Some(name.into());
self
}

/// Declare externally provided Protobuf package or type.
///
/// Passed directly to `prost_build::Config.extern_path`.
Expand Down Expand Up @@ -411,9 +422,19 @@ impl Builder {
PathBuf::from(std::env::var("OUT_DIR").unwrap())
};

let mut reflect_builder = self.descriptor_pool.as_ref().map(|name| {
let mut builder = ProstBuilder::new();
builder.descriptor_pool(name);
builder
});

config.out_dir(out_dir);
if let Some(path) = self.file_descriptor_set_path.as_ref() {
config.file_descriptor_set_path(path);
if let Some(builder) = reflect_builder.borrow_mut() {
builder.file_descriptor_set_path(path);
} else {
config.file_descriptor_set_path(path);
}
}
for (proto_path, rust_path) in self.extern_path.iter() {
config.extern_path(proto_path, rust_path);
Expand Down Expand Up @@ -450,7 +471,11 @@ impl Builder {

config.service_generator(self.service_generator());

config.compile_protos(protos, includes)?;
if let Some(builder) = reflect_builder.borrow_mut() {
builder.compile_protos_with_config(config, protos, includes)?;
} else {
config.compile_protos(protos, includes)?;
}

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Attributes, Method, Service};
use crate::{generate_doc_comment, generate_doc_comments, naive_snake_case};
use crate::{generate_doc_comment, generate_doc_comments, naive_snake_case, sanitize_name};
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{Ident, Lit, LitStr};
Expand Down Expand Up @@ -194,7 +194,7 @@ fn generate_trait_methods<T: Service>(
let mut stream = TokenStream::new();

for method in service.methods() {
let name = quote::format_ident!("{}", method.name());
let name = quote::format_ident!("{}", sanitize_name(method.name()));

let (req_message, res_message) =
method.request_response_name(proto_path, compile_well_known_types);
Expand Down Expand Up @@ -290,7 +290,7 @@ fn generate_methods<T: Service>(
method.identifier()
);
let method_path = Lit::Str(LitStr::new(&path, Span::call_site()));
let ident = quote::format_ident!("{}", method.name());
let ident = quote::format_ident!("{}", sanitize_name(method.name()));
let server_trait = quote::format_ident!("{}", service.name());

let method_stream = match (method.client_streaming(), method.server_streaming()) {
Expand Down