From e43fb9fc5cd47d48f503907d95a2c1becb27257a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Wed, 31 Jul 2024 14:35:21 +0200 Subject: [PATCH 1/2] Don't assume package_config.json --- .../lib/src/handles/native/init.dart | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/realm_dart/lib/src/handles/native/init.dart b/packages/realm_dart/lib/src/handles/native/init.dart index f2fe88d0e..4dcdf2816 100644 --- a/packages/realm_dart/lib/src/handles/native/init.dart +++ b/packages/realm_dart/lib/src/handles/native/init.dart @@ -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 @@ -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'); @@ -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) { From 75893ea5de122d0fce10b3c0a1e767a5ba79a286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Wed, 31 Jul 2024 14:35:34 +0200 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5f839c75..c5abbe963 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.