keathley/twirp-elixir

(KeyError) key :elixir_module_prefix not found in: %Google.Protobuf.FileOptions{}

Closed this issue · 3 comments

(KeyError) key :elixir_module_prefix not found in: %Google.Protobuf.FileOptions{}

https://github.com/keathley/twirp-elixir/blob/master/lib/twirp/protoc/generator.ex#L25 it appears protobuf-elixir/protobuf no longer has that key though it looks like it might exist as some sort of monkey-patched protobuf extension if you require the right module.

I'm sorry this isn't a patch, I'm new to Elixir and unsure if we should be testing for the key or requiring in the extension.

Hey I was able to recreate this issue.
The problem is the extra options that you can declare on the proto file. In my example, we are using the proto for Java and Elixir we have something like this.

syntax = "proto3";

option java_multiple_files = true;
option java_package = "example.apps.services.grpc";
option java_outer_classname = "ProtocolProto";

service ProtocolService {
    rpc Recover(RecoveryRequest) returns (ProtocolReply) {}
}

message RecoveryRequest {
    int64 timestamp = 1;
}
  
message ProtocolReply {
    string message = 1;
}

If we try to compile it with:

protoc --proto_path=./example example/grpc/example.proto --twirp_elixir_out=./elixir-proto-lib/example --elixir_out=./elixir-proto-lib/example

It will complain about the error stated on the issue
key :elixir_module_prefix not found in: %Google.Protobuf.FileOptions{

If we comment the options out it will compile without any problem
This was not a problem for us for like 1 year or something like that, but this past month something changed and now it does no longer work.

Hey I found a possible solution for this problem.
the module Twirp.Protoc.Generator contains the fun generate_content. Here we can that the ctx is being build.

    ctx = %{
      ctx
      | package: desc.package || "",
        syntax: syntax(desc.syntax),
        module_prefix: (desc.options && desc.options.elixir_module_prefix) || (desc.package || "")
    }

The problem here is because the protofile can have custom options, if elixir_module_prefix if not present will crash the tool. an easy fix will be something like.

module_prefix: (desc.options && Map.get(desc.options, :elixir_module_prefix)) || (desc.package || "")

I still don't get why it started to fail 🍡

It most likely started to fail because of a backwards incompatible change made to the elixir proto library. Nothing changed in the twirp library. We can defend against it in this code as @saulbensach demonstrated or by fixing the elixir proto library. Its probably easier to just defend against it here. if someone would like to submit a PR for that, that would be great.