Skip to content

fix: make GRPC.Stub generation opt-out for server-only protoc runs#553

Merged
sleipnir merged 1 commit into
elixir-grpc:masterfrom
mvanhorn:fix/549-server-only-stub-generation
Jul 9, 2026
Merged

fix: make GRPC.Stub generation opt-out for server-only protoc runs#553
sleipnir merged 1 commit into
elixir-grpc:masterfrom
mvanhorn:fix/549-server-only-stub-generation

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Summary

protoc-gen-elixir (the gRPC generator) now supports opting out of GRPC.Stub client generation for server-only runs. A new gen_stubs=false option skips emitting the client stub module while still generating the service module, so projects that only implement servers no longer carry unused client stubs.

Why this matters

Issue #549 asks for a way to generate server code without the client Stub. Today the generator always emits both the service behaviour and a GRPC.Stub client module; a server-only project ends up with a client stub it never uses, which is dead surface area and can clash with a separately generated or hand-written client. There was no switch to turn it off.

This adds a gen_stubs plugin option, defaulting to true so existing behavior is unchanged. Passing --elixir_out=gen_stubs=false:... (parsed in Grpc.Protoc.CLI) threads the flag into the service generator, and the service.ex.eex template conditionally omits the stub module. Default output is byte-identical to before.

Testing

mix test test/grpc/protoc/generator/service_test.exs — 4 tests pass, covering default generation (stub present) and gen_stubs: false (stub omitted, service retained). mix format --check-formatted clean on the changed source files.

Fixes #549

@sleipnir

sleipnir commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

I will take a look but how this is compatible with protobuf_generate hex package? We support both protoc-gen-elixir and probuf_generate.

@mvanhorn

mvanhorn commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Good question. The change is entirely inside GRPC.Protoc.CLI (the protoc-gen-elixir plugin), and it's fully backward compatible: gen_stubs defaults to true, so the .Stub module is only omitted when someone explicitly passes gen_stubs=false. Nothing changes for existing users of either path.

gen_stubs is a standard protoc plugin parameter, so it flows through anywhere protoc-gen-elixir is invoked with plugin params - direct protoc, Buf, etc. For protobuf_generate specifically: it depends on whether protobuf_generate forwards arbitrary plugin options through to the elixir plugin. I wasn't able to confirm its opt-forwarding from the docs - if it doesn't pass extra plugin params today, then gen_stubs=false wouldn't be reachable via protobuf_generate until that's added, but the default (stubs generated) is unaffected either way. You know that package better than I do - if you can point me at how it forwards plugin opts, I'm happy to add a protobuf_generate example/integration test so both paths are covered.

@sleipnir

sleipnir commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Okay, I believe it will work; we just need to test it. In any case, it doesn't seem like an obstacle to us moving forward.

@sleipnir sleipnir merged commit 93736e4 into elixir-grpc:master Jul 9, 2026
7 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Appreciate the merge, @sleipnir - server-only proto builds can opt out of stub generation now.

@sleipnir

Copy link
Copy Markdown
Collaborator

Appreciate the merge, @sleipnir - server-only proto builds can opt out of stub generation now.

Thank you for your contribution.

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.

GRPC.Stub generated in gprc_server only project

2 participants