Skip to content

Commit 25deb78

Browse files
author
Kasper Overgård Nielsen
authored
Fix _openRealmLib (#1766)
* Don't assume package_config.json * Update CHANGELOG
1 parent 28e0e5a commit 25deb78

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
### Fixed
77
* 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))
8+
* 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))
89

910
### Compatibility
1011
* Realm Studio: 15.0.0 or later.

packages/realm_dart/lib/src/handles/native/init.dart

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ String _getLibPathDart(Package realmDartPackage) {
5252
_platformNotSupported();
5353
}
5454

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

72-
File _getPackageConfigJson(Directory d) {
71+
File? _getPackageConfigJson(Directory d) {
7372
final root = _getNearestProjectRoot(d.path);
7473
if (root != null) {
7574
final file = File(p.join(root, '.dart_tool', 'package_config.json'));
7675
if (file.existsSync()) return file;
7776
}
78-
throw StateError('Could not find package_config.json');
77+
return null;
7978
}
8079

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

108107
final isFlutterTest = Platform.environment.containsKey('FLUTTER_TEST');
109108
if (isFlutterPlatform && !isFlutterTest) {
110-
return open(_getLibPathFlutter());
109+
return open(_getLibPathFlutter()); // flutter app
111110
}
112111

113112
// NOTE: This needs to be sync, so we cannot use findPackageConfig
114113
final packageConfigFile = _getPackageConfigJson(Directory.current);
115-
final packageConfig = PackageConfig.parseBytes(packageConfigFile.readAsBytesSync(), packageConfigFile.uri);
116-
117-
if (isFlutterTest) {
118-
final realmPackage = packageConfig['realm']!;
119-
return open(_getLibPathFlutterTest(realmPackage));
114+
if (packageConfigFile != null) {
115+
// inside a project
116+
final packageConfig = PackageConfig.parseBytes(packageConfigFile.readAsBytesSync(), packageConfigFile.uri);
117+
if (isFlutterTest) {
118+
// running flutter test (not flutter test integration_test or flutter drive)
119+
final realmPackage = packageConfig['realm']!;
120+
return open(_getLibPathFlutterTest(realmPackage));
121+
}
122+
// plain dart
123+
final realmDartPackage = packageConfig['realm_dart']!;
124+
return open(_getLibPathDart(realmDartPackage));
120125
}
121126

122-
final realmDartPackage = packageConfig['realm_dart']!;
123-
124-
// else plain dart
127+
// plain dart (compiled or interpreted)
125128
final candidatePaths = [
126129
nativeLibraryName, // just ask OS..
127130
p.join(_exeDirName, nativeLibraryName), // try finding it next to the executable
128-
_getLibPathDart(realmDartPackage), // try finding it in the package
129131
];
130132
DynamicLibrary? lib;
131133
for (final path in candidatePaths) {

0 commit comments

Comments
 (0)