philss / rustler_precompiled

Use precompiled NIFs from trusted sources in your Elixir code
181 stars 25 forks source link

precompiled nif on windows unknow error #56

Closed mrdotb closed 1 year ago

mrdotb commented 1 year ago

Hello I have an issue with precompiled nif on windows. It work fine on ubuntu. I followed the https://github.com/philss/rustler_precompiled/blob/main/PRECOMPILATION_GUIDE.md . The repo https://github.com/mrdotb/resvg_nif The release nif https://github.com/mrdotb/resvg_nif/releases When I try to run then with the precompiled nif it quit without error after downloading the nif.

→ C:\Users\b\projects\resvg_nif [master ≡]› mix test
==> jason
Compiling 10 files (.ex)
Generated jason app
==> castore
Compiling 1 file (.ex)
Generated castore app
==> toml
Compiling 10 files (.ex)
Generated toml app
==> rustler
Compiling 7 files (.ex)
Generated rustler app
==> rustler_precompiled
Compiling 4 files (.ex)
Generated rustler_precompiled app
==> resvg
Compiling 3 files (.ex)

11:42:57.992 [debug] Copying NIF from cache and extracting to c:/Users/b/projects/resvg_nif/_build/test/lib/resvg/priv/native/resvg-v0.2.0-nif-2.15-x86_64-pc-windows-msvc.dll
→ C:\Users\b\projects\resvg_nif [master ≡]›

Forcing the build work on windows.

→ C:\Users\b\projects\resvg_nif [master ≡]› $env:RESVG_BUILD = "1"
→ C:\Users\b\projects\resvg_nif [master ≡]› mix test
...........
Finished in 0.2 seconds (0.00s async, 0.2s sync)
14 tests, 2 failures

Randomized with seed 296000
→ C:\Users\b\projects\resvg_nif [master ≡]›

On wsl and ubuntu running test from the precompiled nif work

$ mix test                                                                                                                     master
Compiling 3 files (.ex)

11:45:44.461 [debug] Copying NIF from cache and extracting to /home/john/Projects/resvg_nif/_build/test/lib/resvg/priv/native/libresvg-v0.2.0-nif-2.16-x86_64-unknown-linux-gnu.so
Generated resvg app
..............
Finished in 0.2 seconds (0.00s async, 0.2s sync)
14 tests, 0 failures

Randomized with seed 633233

I read the source of rustler_precompiled and add some extra logsthe build_metada return

 %{
   load?: true,
   load_data: 0,
   load_from: {:resvg,
    "priv/native/resvg-v0.2.0-nif-2.15-x86_64-pc-windows-msvc"}
 }}

So it appear the nif is properly downloaded and the checksum valid. Any ideas of what should I try next ?

philss commented 1 year ago

Hey @mrdotb :wave:

I'm not good with debugging on Windows, but I'm pretty sure this is some dependency that is missing.

Do you know if the MSVC libraries are installed? And if not, do you know if MinGW is installed? Maybe the selection of ABI is wrong, in case MinGW is installed.

mrdotb commented 1 year ago

Thanks. So I try the https://github.com/philss/rustler_precompilation_example and I got the same issue.

 mix test
Compiling 2 files (.ex)

23:40:49.386 [debug] Copying NIF from cache and extracting to c:/Users/john/projects/rustler_precompilation_example/_build/test/lib/rustler_precompilation_example/priv/native/example-v0.5.0-nif-2.15-x86_64-pc-windows-msvc.dll

I have MSVC and MinGW installed. I removed MinGW from the env, then from the computer. No success. What's the recommended windows env where the precompiled nif should work on windows ? I can try to go a bit deeper but I don't know what is the flow of things. Any clues what sources I should look ?

philss commented 1 year ago

Oh, I see. There are some ideas from this tutorial: https://www.erlang.org/doc/tutorial/debugging.html I'm going to test that on Monday. Thanks for reporting!

mrdotb commented 1 year ago

I've made significant progress after investing some time in exploration and experimentation.

Initially, I attempted to locally replicate what occurs within the action using cross and cross-toolchain. I followed the instructions for building the dockerfile, which allowed me to generate a DLL with debug features using 'cross' within WSL (Windows Subsystem for Linux).

Following that, I attempted to utilize this DLL on Windows by setting up a local server. Although there may be simpler alternatives, I was unable to find one and opted for this approach:

use RustlerPrecompiled,
    otp_app: :resvg,
    crate: "resvg",
    # base_url: "#{github_url}/releases/download/v#{version}",
    base_url: "http://localhost:8080/v#{version}",

Even though the NIF couldn't be loaded, the error message I received differed from the previous one. This strategy is likely to aid in identifying the exact issue with cross wine compilation in the docker, albeit requiring additional time and research.

I also experimented with using NIF versions 2.15 and 2.16 by upgrading the Elixir and Erlang versions on my Windows machine from OTP 23 to OTP 24 both got the same errors.

I also installed msvc 2019 on visual studio only the 2022 was installed (I saw on the docker a mention that the 2019 is used) but same errors.

I found a temporary fix which is generating the x86_64-pc-windows-msvc on my local Windows machine. Once I had done that, I edited the release and replaced the x86_64-pc-windows-msvc 2.16 built NIF with the one I had constructed locally. This allowed me to successfully use the precompiled NIF on a separate Windows computer.

philss commented 1 year ago

@mrdotb this is awesome! Thank you for sharing! I couldn't yet dive into this, but I should be able soon.

mrdotb commented 1 year ago

I try again today and now it work on my two windows computer. I can't reproduce the bug anymore. I did not do any modification my windows computers. Only think I can think of is windows update.