SQLite.Interop.dll Issue

Haritha Mohan,debugging
Photo

I made this graphic a while ago to capture the trials and tribulations of a junior dev..but debugging this problem further validated this graphic. I came across this issue recently and the fix was actually quite simple. But getting down to the root of the issue was…well let’s just say I spent a nice amount of time debugging.

The context of the issue

So recently I have been working on an application that uses the System.Data.SQLite nuget package. And everything worked great, I was able to establish a connection to the database from my program, access data, etc, everything was working great.

But of course what good is a program without some tests. I started writing tests and debugging, but all of a sudden I started running into this error:

System.DllNotFoundException SQLite.Interop.dll
Unable to load shared library 'SQLite.Interop.dll' or one of its dependencies

What was really weird about this error was that the tests were running fine at one point but all of a sudden it seemed something triggered this error.

Let the debugging begin!

These were the first thoughts that were going through my head: Can a SQLite connection not be established in the program since it’s being called from a test? Is something wrong with the SQLite nuget package? Am I missing a nuget package that should have installed the SQLite.Interop.dll?

Some solutions I tried after researching the bug online and even consulting Chat GPT:

But these methods worked to no avail…

So then I thought about it and was like well the SQLite process works fine in the actual program, so now it was a matter of comparing and contrasting the projects to find what the main project had that the tests didn’t..

I started doing a deep dive into the bin directory and sure enough the SQLite.Interop.dll existed for the main project but not in the bin directory of the tests project. So this indicated to me, the IDE and system were not acting up- the error was indeed correct as the assembly did not exist in the tests project. Guided by my lovely findings online I had the genius idea to just copy the SQLite.Interop.dll from the main project into the tests directory. Like, “hey you are looking for the dll in this location, right? here you go!”. Butttt this didn’t solve anything either.

Now I was really stuck. But then I remembered I had seen some things about “the target processor architecture on both the development and customer machines”: ref (opens in a new tab)

This started to click, though my program and test were running on the same machine, essentially the development environment was my original program and my customer environment was my test.

I started digging a little more and stumbled upon the FAQ section (opens in a new tab) for the package...looking at FAQ 11 and 12 started guiding me in the right direction.

And then I found this video (opens in a new tab).

Photo

And finally! I could see the pieces coming together, I had to ensure that both the program and test were targeting the same platform target in its configurations. The error was that my original program was targeting specifically the x64 platform in the Debug configuration whereas my tests were targeting AnyCPU in the Debug configuration. So I adjusted the tests project to just target the x64 platform and the error was fixed and now my tests could run again!!!

Key takeaways of this post…

Obviously developers run into random issues all the time but I thought this issue was interesting for a few reasons.

The initial error message did not make it obvious what the true underlying issue was. Yes, the interop dll did exist but it was really due to the difference in target architectures that the program was not able to find it.

And online there were many misleading suggestions like downloading different SQLite nuget packages, modifying the .csproj file, etc.- all of which I tried of course. I thought the process of using breadcrumbs in this case to find the working solution was actually quite beautiful. A couple months ago, I would’ve initially started looking for the fix and probably given up halfway through. But now I realize it's a combination of using what you know and using what is online to piece together a fix and especially when details are super low-level it can get hella complicated. But embrace the complexity! And use the information you have as clues to guide you in the right direction. Hopefully this post either gives someone the answer they were looking for or hope that they too can find a fix…eventually.

© Haritha Mohan.RSS