Skip to content
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

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

Closed
shanna opened this issue Nov 16, 2021 · 3 comments · Fixed by #146
Closed

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

shanna opened this issue Nov 16, 2021 · 3 comments · Fixed by #146

Comments

@shanna
Copy link

shanna commented Nov 16, 2021

(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.

@saulbensach
Copy link

saulbensach commented Nov 17, 2021

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.

@saulbensach
Copy link

saulbensach commented Nov 17, 2021

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 🍡

@keathley
Copy link
Owner

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.

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 a pull request may close this issue.

3 participants