elixir-protobuf / protobuf

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

"mix test" doesn't work on Windows #281

Open wingyplus opened 2 years ago

wingyplus commented 2 years ago

If I run mix test on Windows, the project will raise error:

PS C:\Users\WingyMomo\src\github.com\elixir-protobuf\protobuf> mix test  
Generated protobuf app
Generated escript protoc-gen-elixir with MIX_ENV=test
Running: protoc --plugin=./protoc-gen-elixir --elixir_out="./generated" -I c:/Users/WingyMomo/src/github.com/elixir-protobuf/protobuf/deps/google_protobuf/src -I src -I test/protobuf/protoc/proto test/protobuf/protoc/proto/extension.proto
--elixir_out: protoc-gen-elixir: %1 is not a valid Win32 application.

** (Mix) Command exited with non-zero status: 1

After I investigate, I found that mix escript.build generate only unix shell script that run escript binary but not for Windows shell script. I try temporary by create protoc-gen-elixir.bat:

@echo off
@escript "%~dpn0" %*

NOTE: I copied this script from mix escript.install.

And modify mix.exs with:

diff --git a/mix.exs b/mix.exs
index 707526a..866b128 100644
--- a/mix.exs
+++ b/mix.exs
@@ -227,7 +227,7 @@ defmodule Protobuf.Mixfile do
     args =
       [
         ~s(protoc),
-        ~s(--plugin=./protoc-gen-elixir),
+        ~s(--plugin=./protoc-gen-elixir.bat),
         ~s(--elixir_out="#{elixir_out}"),
         args
       ] ++ files_to_generate

After run mix test will change to:

Generated protobuf app
Generated escript protoc-gen-elixir with MIX_ENV=test
Running: protoc --plugin=./protoc-gen-elixir.bat --elixir_out="./generated" -I c:/Users/WingyMomo/src/github.com/elixir-protobuf/protobuf/deps/google_protobuf/src -I src -I test/protobuf/protoc/proto test/protobuf/protoc/proto/extension.proto
** (Protobuf.DecodeError) cannot decode binary data, unknown wire type: 6
    (protobuf 0.10.1-dev) lib/protobuf/decoder.ex:80: Protobuf.Decoder.handle_field/5
    (protobuf 0.10.1-dev) lib/protobuf/decoder.ex:17: Protobuf.Decoder.decode/2
    (protobuf 0.10.1-dev) lib/protobuf/decoder.ex:231: Protobuf.Decoder.value_for_field/3
    (protobuf 0.10.1-dev) lib/protobuf/decoder.ex:382: Protobuf.Decoder.update_in_message/3
    (protobuf 0.10.1-dev) lib/protobuf/decoder.ex:176: Protobuf.Decoder.handle_value/6
    (protobuf 0.10.1-dev) lib/protobuf/decoder.ex:17: Protobuf.Decoder.decode/2
    (protobuf 0.10.1-dev) lib/protobuf/decoder.ex:231: Protobuf.Decoder.value_for_field/3
    (protobuf 0.10.1-dev) lib/protobuf/decoder.ex:382: Protobuf.Decoder.update_in_message/3
--elixir_out: protoc-gen-elixir: Plugin failed with status code 1.
** (Mix) Command exited with non-zero status: 1
whatyouhide commented 2 years ago

@wingyplus I don't have a chance to test this since I don't have a Windows machine. Are you willing to give figuring this out a try? 🙃

wingyplus commented 2 years ago

Just a little investigation. Inspecting rest inside decoder.ex with this diff:

diff --git a/lib/protobuf/decoder.ex b/lib/protobuf/decoder.ex
index 2fa107e..2febe12 100644
--- a/lib/protobuf/decoder.ex
+++ b/lib/protobuf/decoder.ex
@@ -42,6 +42,9 @@ defmodule Protobuf.Decoder do
     field_number = bsr(value, 3)
     wire_type = band(value, 0b00000111)

+    IO.inspect(:stderr, byte_size(rest), [])
+    IO.inspect(:stderr, rest, binaries: :as_binaries)
+
     if field_number != 0 do
       handle_field(rest, field_number, wire_type, message, props)
     else

This is the result when I'm running on Windows:

out.txt

JohnDoneth commented 1 year ago

I'm encountering the same issue and having a hard time tracking down where the issue could be during decoding.

value: 50
field_number: 6
wire_type: 2
bytes left: 62
<<46, 46, 103, 111, 111, 103, 108, 101, 46, 112, 114, 111, 116, 111, 98, 117,
  102, 46, 68, 101, 115, 99, 114, 105, 112, 116, 111, 114, 80, 114, 111, 116,
  111, 46, 82, 101, 115, 101, 114, 118, 101, 100, 82, 97, 110, 103, 101, 82, 10,
  114, 101, 115, 101, 114, 118, 101, 100, 82, 97, 110, 103, 101>>
---------------------------
value: 82
field_number: 10
wire_type: 2
bytes left: 14
<<10, 114, 101, 115, 101, 114, 118, 101, 100, 82, 97, 110, 103, 101>>
---------------------------
value: 110
field_number: 13
wire_type: 6
bytes left: 2
<<103, 101>>
** (Protobuf.DecodeError) cannot decode binary data, unknown wire type: 6
    (protobuf 0.12.0) lib/protobuf/decoder.ex:88: Protobuf.Decoder.handle_field/5
    (protobuf 0.12.0) lib/protobuf/decoder.ex:17: Protobuf.Decoder.decode/2