-
Notifications
You must be signed in to change notification settings - 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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.