ericsink / SQLitePCL.raw

A Portable Class Library (PCL) for low-level (raw) access to SQLite
Apache License 2.0
512 stars 106 forks source link

e_sqlite3 not found on net462 #526

Closed zmj closed 1 year ago

zmj commented 1 year ago

I'm trying to add a .NET Framework target to the tests for an existing project. The test project references a .NET Standard wrapper library that PackageReferences a SQLitePCL.raw bundle:

Library csproj: https://github.com/zmj/sqlite-fast/blob/master/Sqlite.Fast/Sqlite.Fast.csproj Test csproj: https://github.com/zmj/sqlite-fast/blob/master/Sqlite.Fast.Tests/Sqlite.Fast.Tests.csproj Batteries.Init call: https://github.com/zmj/sqlite-fast/blob/master/Sqlite.Fast/Connection.cs#L15

On Mono / M1 Mac, the tests fail on the Batteries.Init call. They also fail on Windows 11 x64 with a similar error. I'll add that output in a comment.

dotnet test output:

  Error Message:
   System.TypeInitializationException : The type initializer for 'Sqlite.Fast.Connection' threw an exception.
---- System.Exception : Library e_sqlite3 not found
plat: dlopen
suffix: DYLIB
possibilities (2):
    1) /private/var/folders/jp/s3w4bhcs0lx1kjp_7rgm10jr0000gn/T/6a8ac9fc-ea25-4701-9a77-c5f5cf22da04/6a8ac9fc-ea25-4701-9a77-c5f5cf22da04/assembly/shadow/9253aac8/ac26431d_3e75e9d5_00000002/runtimes/osx-x64/native/libe_sqlite3.dylib
    2) /private/var/folders/jp/s3w4bhcs0lx1kjp_7rgm10jr0000gn/T/6a8ac9fc-ea25-4701-9a77-c5f5cf22da04/6a8ac9fc-ea25-4701-9a77-c5f5cf22da04/assembly/shadow/9253aac8/ac26431d_3e75e9d5_00000002/libe_sqlite3.dylib
dlopen TryLoad: /private/var/folders/jp/s3w4bhcs0lx1kjp_7rgm10jr0000gn/T/6a8ac9fc-ea25-4701-9a77-c5f5cf22da04/6a8ac9fc-ea25-4701-9a77-c5f5cf22da04/assembly/shadow/9253aac8/ac26431d_3e75e9d5_00000002/runtimes/osx-x64/native/libe_sqlite3.dylib
dlopen gave: 0
dlopen TryLoad: /private/var/folders/jp/s3w4bhcs0lx1kjp_7rgm10jr0000gn/T/6a8ac9fc-ea25-4701-9a77-c5f5cf22da04/6a8ac9fc-ea25-4701-9a77-c5f5cf22da04/assembly/shadow/9253aac8/ac26431d_3e75e9d5_00000002/libe_sqlite3.dylib
dlopen gave: 0
NOT FOUND

  Stack Trace:
    at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_generic_class_init(intptr)
  at Sqlite.Fast.Tests.TestTable..ctor (System.String createTableSql) [0x00008] in <776f3b1ba5e2418f8366f895574427ec>:0 
  at Sqlite.Fast.Tests.ConverterTests.Scalar_FromInt () [0x00009] in <776f3b1ba5e2418f8366f895574427ec>:0 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <b27839cc2dba4804baacf2f5cce6de32>:0 
----- Inner Stack Trace -----
  at SQLitePCL.NativeLibrary.Load (System.String libraryName, System.Reflection.Assembly assy, System.Int32 flags) [0x00058] in <8a9cf65dcc6542b59d3abc28e58f9815>:0 
  at SQLitePCL.Batteries_V2.MakeDynamic (System.String name, System.Int32 flags) [0x00010] in <8a9cf65dcc6542b59d3abc28e58f9815>:0 
  at SQLitePCL.Batteries_V2.DoDynamic_cdecl (System.String name, System.Int32 flags) [0x00000] in <8a9cf65dcc6542b59d3abc28e58f9815>:0 
  at SQLitePCL.Batteries_V2.Init () [0x00000] in <8a9cf65dcc6542b59d3abc28e58f9815>:0 
  at Sqlite.Fast.Connection..cctor () [0x00000] in <b25266bf021944fbb119c28d8aecb6d8>:0 

Looking at the existing open issues, I see some workarounds supplying explicit RuntimeIdentifiers. Is that something I should be doing?

Template info:

What version of SQLitePCLRaw are you using?

2.1.2. Also tried 2.0.7, same error.

If you are using one of the SQLitePCLRaw bundle packages, which one?

Tried green and e_sqlite3.

What platform are you running on? What operating system? Which version? What CPU?

OSX Ventura, M1 Pro. WIndows 11 x64.

What target framework are you building for?

net462. Also tried net48, same error.

Are you on .NET Framework or the newer stuff (.NET Core, .NET 5+, etc)?

Framework.

Are you using the command line, or an IDE? Which IDE? Which version of that IDE?

Command line. On Windows, the error message is the same in Visual Studio 2022.

Is this problem something that just started happening? For example, were things working well for you and then you updated something and then the problem showed up? What changed?

No. I'm trying to add a net framework target to tests that were previously netcore only.

What is the exact error message you are seeing when the problem happens?

See above.

Are you using PackageReference or packages.config?

PackageReference

If you are using mobile platforms, are you on classic/legacy Xamarin or on .NET 6 and higher?

N/A

Sometimes other packages using SQLitePCLRaw cause problems when they are mixed together. What other packages are you including in your project?

Minimal. See csproj links above.

How can we reproduce the problem you are seeing? Your issue will get attention much faster if you attach a minimal reproduction sample project.

Clone the repository (links above) and run the tests.

zmj commented 1 year ago

On Windows:

  Error Message:
   System.TypeInitializationException : The type initializer for 'Sqlite.Fast.Connection' threw an exception.
---- System.Exception : Library e_sqlite3 not found
plat: win
suffix: DLL
possibilities (2):
    1) C:\Users\zmj\AppData\Local\Temp\d5e5718b-ca74-4b1d-8c7d-e3b7f63a95fa\d5e5718b-ca74-4b1d-8c7d-e3b7f63a95fa\assembly\dl3\bbda9fce\004c6898_94cbd801\runtimes\win-x86\native\e_sqlite3.dll
    2) C:\Users\zmj\AppData\Local\Temp\d5e5718b-ca74-4b1d-8c7d-e3b7f63a95fa\d5e5718b-ca74-4b1d-8c7d-e3b7f63a95fa\assembly\dl3\bbda9fce\004c6898_94cbd801\e_sqlite3.dll
win TryLoad: C:\Users\zmj\AppData\Local\Temp\d5e5718b-ca74-4b1d-8c7d-e3b7f63a95fa\d5e5718b-ca74-4b1d-8c7d-e3b7f63a95fa\assembly\dl3\bbda9fce\004c6898_94cbd801\runtimes\win-x86\native\e_sqlite3.dll
thrown: System.ComponentModel.Win32Exception (0x80004005): The specified module could not be found
   at SQLitePCL.NativeLibrary.TryLoad(String name, Loader plat, Action`1 log, IntPtr& h)
win TryLoad: C:\Users\zmj\AppData\Local\Temp\d5e5718b-ca74-4b1d-8c7d-e3b7f63a95fa\d5e5718b-ca74-4b1d-8c7d-e3b7f63a95fa\assembly\dl3\bbda9fce\004c6898_94cbd801\e_sqlite3.dll
thrown: System.ComponentModel.Win32Exception (0x80004005): The specified module could not be found
   at SQLitePCL.NativeLibrary.TryLoad(String name, Loader plat, Action`1 log, IntPtr& h)
NOT FOUND

  Stack Trace:
     at Sqlite.Fast.Connection..ctor()
   at Sqlite.Fast.Tests.TestTable..ctor(String createTableSql) in D:\sqlite-fast\Sqlite.Fast.Tests\TestTable.cs:line 13
   at Sqlite.Fast.Tests.ConverterTests.Custom_ToBlob() in D:\sqlite-fast\Sqlite.Fast.Tests\ConverterTests.cs:line 226
----- Inner Stack Trace -----
   at SQLitePCL.NativeLibrary.Load(String libraryName, Assembly assy, Int32 flags)
   at SQLitePCL.Batteries_V2.MakeDynamic(String name, Int32 flags)
   at SQLitePCL.Batteries_V2.DoDynamic_cdecl(String name, Int32 flags)
   at SQLitePCL.Batteries_V2.Init()
   at Sqlite.Fast.Connection..cctor() in D:\sqlite-fast\Sqlite.Fast\Connection.cs:line 15
ericsink commented 1 year ago

I cloned your repo and reproduced the problem.

Then I added a file called xunit.runner.json into Sqlite.Fast.Tests, containing:

{
   "shadowCopy": false
}

Then I added this snippet to Sqlite.Fast.Tests.csproj:

  <ItemGroup>
    <None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

After that dotnet test works.

This is a common problem when using xunit with .NET Framework.

zmj commented 1 year ago

Thank you! That fixed things.