kleisauke / net-vips

.NET binding for libvips.
https://kleisauke.github.io/net-vips/
MIT License
401 stars 32 forks source link

Trying to run on macos using blazor provides missing dll issue #240

Open justasxz opened 1 month ago

justasxz commented 1 month ago

Hi,

So I am trying to build the application (that already runs on windows) on mac, it builds successfully everything seems to work, but when the application opens up it throws netvips.dll exception. Is there any fix to this ? I have all the native-arm packages installed. It only fails when it tries to run netvips code, it functions normally besides that. I am running VS Code blazor hybrid

kleisauke commented 1 month ago

Did you see https://github.com/kleisauke/net-vips/issues/189#issuecomment-1408407451 ?

I have all the native-arm packages installed.

You could try to install the NetVips.Native NuGet package, it will automatically choose the appropriate NetVips.Native.* package based on which OS/bitness it's running on. This can be useful for cross-platform deployments.

justasxz commented 1 month ago

Did you see #189 (comment) ?

I have all the native-arm packages installed.

You could try to install the NetVips.Native NuGet package, it will automatically choose the appropriate NetVips.Native.* package based on which OS/bitness it's running on. This can be useful for cross-platform deployments.

Thanks, for quick reply. Yes I tried various combinations such as netvips and netvips.native only. netvips native and arm64 version at the same time. Only arm64 version, neither works. Here is the full error:

Exception has occurred: CLR/System.DllNotFoundException

Exception thrown: 'System.DllNotFoundException' in NetVips.dll: 'libvips.so.42' at NetVips.Internal.Vips.Init(String argv0) at NetVips.NetVips.Init() at NetVips.ModuleInitializer.Initialize() at .cctor() at PSPerformance.Domain.Editor.EditorImage.b__33_0() in /Users/diana/Documents/ProSystemos/PSPerformance/PSPerformance.Domain/Editor/EditorImage.cs:line 66 at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.<>c.<.cctor>b__281_0(Object obj) at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) at System.Threading.Tasks.Task.ExecuteEntryUnsafe(Thread threadPoolThread) at System.Threading.Tasks.Task.ExecuteFromThreadPool(Thread threadPoolThread) at System.Threading.ThreadPoolWorkQueue.DispatchWorkItem(Object workItem, Thread currentThread) at System.Threading.ThreadPoolWorkQueue.DispatchItemWithAutoreleasePool(Object workItem, Thread currentThread) at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading.PortableThreadPool.WorkerThread.WorkerDoWork(PortableThreadPool threadPoolInstance, Boolean& spinWait) at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() at System.Threading.Thread.StartHelper.RunWorker() at System.Threading.Thread.StartHelper.Run() at System.Threading.Thread.StartCallback()

If that helps I had similar issue with MagickNet until I added this to my csproj < MagickCopyNativeMacOS >true< /MagickCopyNativeMacOS >

Just to reiterate this is blazor hybrid project (so basically maui + blazor) It works fine on windows, just on macos keeps throwing that error. I have not tried, but I believe manually adding the dll might help ?

kleisauke commented 1 month ago

On macOS, it should look for libvips.42.dylib instead of libvips.so.42. Could you try updating to the release candidates of NetVips 3.0.0 and NetVips.Native 8.16.0? https://www.nuget.org/packages/NetVips/3.0.0-rc1 https://www.nuget.org/packages/NetVips.Native/8.16.0-rc1

If that works, it seems that the wrong NetVips DLL for Linux (from the runtimes/unix/lib/netstandard2.0 directory) is being used on macOS, when it should be loading the macOS-specific DLL (from the runtimes/osx/lib/netstandard2.0 directory) instead. Fortunately, after commit 14480400ce17a1bbb48c74e718f4d05f50e11e49, only a single DLL is now required, with re-mapping handled at runtime using NativeLibrary.SetDllImportResolver().

justasxz commented 1 month ago

On macOS, it should look for libvips.42.dylib instead of libvips.so.42. Could you try updating to the release candidates of NetVips 3.0.0 and NetVips.Native 8.16.0? https://www.nuget.org/packages/NetVips/3.0.0-rc1 https://www.nuget.org/packages/NetVips.Native/8.16.0-rc1

If that works, it seems that the wrong NetVips DLL for Linux (from the runtimes/unix/lib/netstandard2.0 directory) is being used on macOS, when it should be loading the macOS-specific DLL (from the runtimes/osx/lib/netstandard2.0 directory) instead. Fortunately, after commit 1448040, only a single DLL is now required, with re-mapping handled at runtime using NativeLibrary.SetDllImportResolver().

I updated it to the newest versions but I seem to be getting the same issue. To clarify the target is maccatalyst. I don't think you can target macOS directly on blazor hybrid. Unless I am missing something.

kleisauke commented 1 month ago

It appears that this issue is related to https://github.com/dotnet/runtime/issues/104160. As a workaround, you could try symlinking or renaming libvips.42.dylib to libvips.so.42, although I'm unsure if this would work on Mac Catalyst without further adjustments.

Additionally, I'm uncertain whether NetVips, or any other that relies on unmanaged/native code, would function correctly in a Blazor Hybrid environment. It's probably unsupported in Blazor components, as P/Invoke isn't natively supported there.

kleisauke commented 4 weeks ago

@justasxz Are you able to test commit 8308801dd52e84a6769b688019193b3d02fdab09? Testing can be done by using the nightly version of NetVips. Add the https://ci.appveyor.com/nuget/net-vips feed in the <packageSources> section of your NuGet.config:

<packageSources>
  <add key="netvips-nightly" value="https://ci.appveyor.com/nuget/net-vips" />
</packageSources>

And update NetVips to 3.0.0 (build number 605 - prerelease).

kleisauke commented 4 weeks ago

Hmm, looking at PortableRuntimeIdentifierGraph.json, I'm unsure if the above commit will work with the NetVips.Native* packages, since the maccatalyst RIDs inherit from ios rather than osx.

graph TD;
    unix --> unix-x64 & ios & unix-arm64;
    unix-x64 --> ios-x64;
    unix-arm64 --> ios-arm64;
    ios --> ios-x64 & maccatalyst & ios-arm64;
    ios-x64 & maccatalyst --> maccatalyst-x64;
    ios-arm64 & maccatalyst --> maccatalyst-arm64;
justasxz commented 4 weeks ago

@justasxz Are you able to test commit 8308801? Testing can be done by using the nightly version of NetVips. Add the https://ci.appveyor.com/nuget/net-vips feed in the <packageSources> section of your NuGet.config:

<packageSources>
  <add key="netvips-nightly" value="https://ci.appveyor.com/nuget/net-vips" />
</packageSources>

And update NetVips to 3.0.0 (build number 605 - prerelease).

I would for sure be able to, I greatly appreciate your effort to add support to maccatalyst. But you just responded that you doubt it would work. Do you want me to test it or wait for another commit ?

kleisauke commented 4 weeks ago

Do you want me to test it or wait for another commit ?

Feel free to test that commit; hopefully it will now look for libvips.42.dylib instead of libvips.so.42.

Support for iOS (and .NET MAUI) is being tracked in issue #154.

kleisauke commented 1 week ago

@justasxz Were you able to make any progress with this?

justasxz commented 1 week ago

Hi, sorry it was put on hold for a bit, I will get back to it when I can. I was having trouble downloading the preview version (never done it, and following tutorials, adding the link you provided into .nuget file did not allow me to pick preview version, might be me not knowing how to, might be just visual studio code, not working as intended), and then some other things came up with higher priority. Should come back with an answer in a few weeks if I get it working.