Skip to content

feat(tonic-build): add ability to configure tonic crate path#2676

Open
lukasweber wants to merge 1 commit into
grpc:masterfrom
lukasweber:feature/tonic-path
Open

feat(tonic-build): add ability to configure tonic crate path#2676
lukasweber wants to merge 1 commit into
grpc:masterfrom
lukasweber:feature/tonic-path

Conversation

@lukasweber
Copy link
Copy Markdown

@lukasweber lukasweber commented Jun 5, 2026

Hi guys,

thanks for the great work :)

Motivation

Following the issue #699, I would like to add the capability to configure the path to the tonic crate in generated code in tonic-build. This allows other library crates building on top of tonic to re-export tonic, so users of the library (using tonic) don't have to to include a dependency to tonic.

My use case:
I'm currently building a library for the SiLA protocol on top of tonic here and it would be really nice to ditch the tonic dependency from crates using that library.

Solution

Inspired by how prost is doing it with prost_path, I added a new method tonic_path that allows to set the path to tonic in the module code_gen (crate tonic_build). The same I did for the public manual module where the path is then passed further to the CodeGenBuilder. If the builder-method is not called ::tonic is used by default. I also added the method to tonic-prost-build.

I then made sure this path is passed down to client and server code generation, so there is no "hardcoded" tonic-path anymore.

Additional Notes

  • I set the default path to ::tonic since I saw prost is doing it the same way. Although probably not a too big deal, I feel like it makes more sense to make it absolute for generated code to have the path crystal clear. That's also why I had to change many of the code in generated. Let me know if you see a problem in that. I can easily revert it. It pollutes the changeset quite a bit.

  • In the test, I also re-exported tonic_prostand configured the path using codec_path(...) which appears to be enough to also ditch the tonic_prost dependency from my_application. Let me know if you feel like I should only focus on re-exporting tonic.

  • For some methods, I had to add an additional #[allow(clippy::too_many_arguments)] where I thought it's probably ok given that it is also used in many similar cases in that module already.

  • In tonic-prost-build, I removed some unnecessary clones for parameters being passed that were right next to the tonic_path parameter.

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Jun 5, 2026

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: lukasweber / name: Lukas Weber (4d95c95)

@lukasweber lukasweber force-pushed the feature/tonic-path branch from 4d95c95 to 10d422d Compare June 5, 2026 08:57
@lukasweber lukasweber force-pushed the feature/tonic-path branch from 8607471 to 7c12e24 Compare June 5, 2026 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant