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

RDART-1082: Fix _openRealmLib #1766

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Fixed
* Fixed an issue where creating a flexible sync configuration with an embedded object not referenced by any top-level object would throw a "No such table" exception with no meaningful information about the issue. Now a `RealmException` will be thrown that includes the offending object name, as well as more precise text for what the root cause of the error is. (PR [#1748](https://github.com/realm/realm-dart/pull/1748))
* Pure dart apps, when compiled to an exe and run from outside the project directory would fail to load the native shared library. (Issue [#1765](https://github.com/realm/realm-dart/issues/1765))

### Compatibility
* Realm Studio: 15.0.0 or later.
Expand Down
28 changes: 15 additions & 13 deletions packages/realm_dart/lib/src/handles/native/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ String _getLibPathDart(Package realmDartPackage) {
_platformNotSupported();
}


String _getLibName(String stem) => switch (targetOsType) {
TargetOsType.android => 'lib$stem.so',
TargetOsType.ios => stem, // xcframeworks are a directory
Expand All @@ -69,13 +68,13 @@ String? _getNearestProjectRoot(String dir) {
return null;
}

File _getPackageConfigJson(Directory d) {
File? _getPackageConfigJson(Directory d) {
final root = _getNearestProjectRoot(d.path);
if (root != null) {
final file = File(p.join(root, '.dart_tool', 'package_config.json'));
if (file.existsSync()) return file;
}
throw StateError('Could not find package_config.json');
return null;
}

Never _platformNotSupported() => throw UnsupportedError('Platform ${Platform.operatingSystem} is not supported');
Expand Down Expand Up @@ -107,25 +106,28 @@ DynamicLibrary _openRealmLib() {

final isFlutterTest = Platform.environment.containsKey('FLUTTER_TEST');
if (isFlutterPlatform && !isFlutterTest) {
return open(_getLibPathFlutter());
return open(_getLibPathFlutter()); // flutter app
}

// NOTE: This needs to be sync, so we cannot use findPackageConfig
final packageConfigFile = _getPackageConfigJson(Directory.current);
final packageConfig = PackageConfig.parseBytes(packageConfigFile.readAsBytesSync(), packageConfigFile.uri);

if (isFlutterTest) {
final realmPackage = packageConfig['realm']!;
return open(_getLibPathFlutterTest(realmPackage));
if (packageConfigFile != null) {
// inside a project
final packageConfig = PackageConfig.parseBytes(packageConfigFile.readAsBytesSync(), packageConfigFile.uri);
if (isFlutterTest) {
// running flutter test (not flutter test integration_test or flutter drive)
final realmPackage = packageConfig['realm']!;
return open(_getLibPathFlutterTest(realmPackage));
}
// plain dart
final realmDartPackage = packageConfig['realm_dart']!;
return open(_getLibPathDart(realmDartPackage));
}

final realmDartPackage = packageConfig['realm_dart']!;

// else plain dart
// plain dart (compiled or interpreted)
final candidatePaths = [
nativeLibraryName, // just ask OS..
p.join(_exeDirName, nativeLibraryName), // try finding it next to the executable
_getLibPathDart(realmDartPackage), // try finding it in the package
];
DynamicLibrary? lib;
for (final path in candidatePaths) {
Expand Down
Loading