-
Couldn't load subscription status.
- Fork 3.3k
Improve LoadExtension to work correctly with dotnet run and lib* named libs #35617
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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") | ||||||
|
|
@@ -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); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.