codegen: an RPC named `Config`, `ConfigMut` or `New` collides with the generated client's inherent methods (E0592)

Open
#345 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
tooling

Research direction

Start in connectrpc-codegen/src/codegen.rs: read check_method_collisions and inspect the generated client impl block. Compare the collision checks with the generated new, config and config_mut methods; the issue leaves the long-term fix open. Done means a colliding RPC is handled deliberately, rather than failing with E0592 in generated code.

Written by the indexing model from the issue text.

Description

The generated FooClient<T> has three inherent methods of its own, new,
config and config_mut, in the same impl block as one method per RPC. An
RPC whose snake_case name is one of those emits a second definition, and the
crate fails to compile:

error[E0592]: duplicate definitions with name `config`
   --> .../out/repro.__connect.rs:386:5
    |
378 |       pub fn config(&self) -> &::connectrpc::client::ClientConfig {
    |       ----------------------------------------------------------- other definition for `config`
...
386 | /     pub async fn config(
387 | |         &self,
388 | |         request: Req,
...   |
394 | |     > {
    | |_____^ duplicate definitions for `config`

ConfigMut gives the same error for config_mut, and New for new.

This shows up in real protos: opencensus.proto.agent.trace.v1.TraceService
has rpc Config(stream CurrentLibraryConfig) returns (stream UpdatedLibraryConfig).

check_method_collisions (from #28) catches RPCs that collide with each other,
and #280 extended the check across services that share a module. Neither
checks RPC names against the client's own methods, so the failure is reported
in generated code instead of in the proto.

Reproduction

connectrpc 0.9.1, connectrpc-build 0.9.0. The same code is on main at
bd2ab67 (connectrpc-codegen/src/codegen.rs, the client impl block).

Cargo.toml:

[package]
name = "repro"
version = "0.0.0"
edition = "2024"
publish = false

[dependencies]
buffa = { version = "0.9.2", features = ["json"] }
connectrpc = { version = "0.9.1", features = ["client"] }
serde = { version = "1", features = ["derive"] }

[build-dependencies]
connectrpc-build = "0.9.0"

proto/repro.proto:

syntax = "proto3";
package repro;

message Req {}
message Resp {}

service Trace {
  rpc Config(Req) returns (Resp);
}

build.rs:

fn main() {
    connectrpc_build::Config::new()
        .files(&["proto/repro.proto"])
        .includes(&["proto"])
        .include_file("_connectrpc.rs")
        .compile()
        .unwrap();
}

src/lib.rs:

include!(concat!(env!("OUT_DIR"), "/_connectrpc.rs"));

cargo check fails with the E0592 above.

Possible fixes

  1. Rename the client's accessors where they collide. Keep the RPC methods
    named after the RPCs, matching the server trait, and emit the accessors
    under another name (e.g. client_config / client_config_mut) only in
    clients that need it. Callers can work around the bug this way today by
    post-processing the generated code. It makes the accessor name vary by service, so it
    probably isn't the right long-term answer.
  2. Move the accessors off the inherent namespace. For example, put them on a
    trait (ClientConfigExt::config(&client)) or make them associated
    functions (FooClient::config(&client)). Either way an RPC can never
    collide with them. This is a breaking change.
  3. Rename the RPC method where it collides, for example config_rpc. This
    keeps the accessors, but the client method no longer matches the server
    trait's method name.
  4. At least reject it clearly. Treat new, config and config_mut as
    taken in check_method_collisions, so codegen fails with "RPC Config collides with the generated client's
    config()" instead of E0592 in generated code.

New can't be fixed by renaming the constructor without breaking every
client, so (4) is probably right for new whatever is chosen for
config/config_mut.

Dominant language
Rust
Stars
544
Forks
77
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from connectrpc/connect-rust

All issues in connectrpc/connect-rust

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.