Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve LoadExtension to work correctly with dotnet run and lib* named libs #35617

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public partial class SqliteConnection : DbConnection
private static readonly StateChangeEventArgs _fromClosedToOpenEventArgs = new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open);
private static readonly StateChangeEventArgs _fromOpenToClosedEventArgs = new StateChangeEventArgs(ConnectionState.Open, ConnectionState.Closed);

private static string[]? NativeDllSearchDirectories;

static SqliteConnection()
{
Type.GetType("SQLitePCL.Batteries_V2, SQLitePCLRaw.batteries_v2")
Expand Down Expand Up @@ -624,11 +626,71 @@ public virtual void LoadExtension(string file, string? proc = null)

private void LoadExtensionCore(string file, string? proc)
{
var rc = sqlite3_load_extension(Handle, utf8z.FromString(file), utf8z.FromString(proc), out var errmsg);
if (rc != SQLITE_OK)
SqliteException? firstException = null;
foreach (var path in GetLoadExtensionPaths(file))
{
var rc = sqlite3_load_extension(Handle, utf8z.FromString(path), utf8z.FromString(proc), out var errmsg);
if (rc == SQLITE_OK)
{
return;
}

if (firstException == null)
{
// We store the first exception so that error message looks more obvious if file appears in there
firstException = new SqliteException(Resources.SqliteNativeError(rc, errmsg.utf8_to_string()), rc, rc);
}
}

if (firstException != null)
{
throw firstException;
}
}

private static IEnumerable<string> GetLoadExtensionPaths(string file)
{
// we always try original input first
yield return file;

string? dirName = Path.GetDirectoryName(file);

// we don't try to guess directories for user, if they pass a path either absolute or relative - they're on their own
if (!string.IsNullOrEmpty(dirName))
{
yield break;
}

bool shouldTryAddingLibPrefix = !file.StartsWith("lib", StringComparison.Ordinal) && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

if (shouldTryAddingLibPrefix)
{
yield return $"lib{file}";
}

NativeDllSearchDirectories ??= GetNativeDllSearchDirectories();

foreach (string dir in NativeDllSearchDirectories)
{
yield return Path.Combine(dir, file);

if (shouldTryAddingLibPrefix)
{
yield return Path.Combine(dir, $"lib{file}");
}
}
}

private static string[] GetNativeDllSearchDirectories()
{
string? searchDirs = AppContext.GetData("NATIVE_DLL_SEARCH_DIRECTORIES") as string;

if (string.IsNullOrEmpty(searchDirs))
{
throw new SqliteException(Resources.SqliteNativeError(rc, errmsg.utf8_to_string()), rc, rc);
return [];
}

return searchDirs!.Split([ Path.PathSeparator ], StringSplitOptions.RemoveEmptyEntries);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return searchDirs!.Split([ Path.PathSeparator ], StringSplitOptions.RemoveEmptyEntries);
return searchDirs.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot change this because we're building against netstandard 2.0 where I'm getting:

  • (for lack of !): error CS8602: Dereference of a possibly null reference.
  • (using (char, StringSplitOptions) overload): error CS1503: Argument 2: cannot convert from 'System.StringSplitOptions' to 'char'

}

/// <summary>
Expand Down