elixir-protobuf / protobuf

A pure Elixir implementation of Google Protobuf.
https://hexdocs.pm/protobuf/readme.html
MIT License
823 stars 143 forks source link

Compiling failures with enum #74

Closed tony612 closed 4 years ago

tony612 commented 4 years ago
== Compilation error in file lib/protos/content/service.pb.ex ==
** (UndefinedFunctionError) function Content.Foo.key/1 is undefined (module Content.Foo is not available)
    Content.ThumbnailSpritesType.key(0)
    lib/protobuf/dsl.ex:32: anonymous fn/2 in Protobuf.DSL."MACRO-__before_compile__"/2
    (elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
    expanding macro: Protobuf.DSL.__before_compile__/1
    lib/protos/content/service.pb.ex:254: Content.Bar (module)`

It seems this is caused by this PR. I guess the reason is Content.Foo is defined at another file, and with this PR's change, Content.Foo is not compiled when compiling Content.Bar.

whatyouhide commented 4 years ago

@tony612 can you try a quick patch, which is calling Code.ensure_loaded(type) before calling type.key(1) in the changes made in my PR?

tony612 commented 4 years ago

@whatyouhide I tried, but only using ensure_loaded doesn't work. I think it's because we have a file like this

defmodule Foo.Msg do
  use Protobuf, syntax: :proto3
  field :type, 0, type: Foo.Enum, enum: true
end

defmodule Foo.Enum do
  use Protobuf, enum: true, syntax: :proto3
  field :UNKNOWN, 0
end

And Elixir can't compile a file like this, we have to write a file like

defmodule Foo.Enum do
  use Protobuf, enum: true, syntax: :proto3
  field :UNKNOWN, 0
end

defmodule Foo.Msg do
  use Protobuf, syntax: :proto3
  field :type, 0, type: Foo.Enum, enum: true
end

I tried adjuting the ordering of the generated code in this branch https://github.com/tony612/protobuf-elixir/compare/fix-gen-order?expand=1

This branch just put enum in the first in generated files, I'm not sure if this can cover every case, but our protos can be compiled successfully with it.

whatyouhide commented 4 years ago

@tony612 looks good to me :)

jared-mackey commented 4 years ago

I am having issues with this on 0.7.1.

I am having an issue with enums defined in the same file are being generated at the bottom of the file. My work around is to move them to the top of the file but that has to be done every time we regenerate them.

@whatyouhide @tony612

tony612 commented 4 years ago

@mackeyja92 Could you regenerate the code after upgrading protoc plugin?

mix escript.install hex protobuf 0.7.1

The escript version is different from the protobuf version in mix.exs.

jared-mackey commented 4 years ago

@tony612 Yes. That solved it for me. Thank you!