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

Issues with unit testing using Realm: Failed to Load Dynamic Library Error #1619

Open
Taco55 opened this issue Apr 4, 2024 · 11 comments · May be fixed by #1623
Open

Issues with unit testing using Realm: Failed to Load Dynamic Library Error #1619

Taco55 opened this issue Apr 4, 2024 · 11 comments · May be fixed by #1623

Comments

@Taco55
Copy link

Taco55 commented Apr 4, 2024

What happened?

I'm encountering persistent errors when attempting to run unit tests. Following an upgrade to Realm 2.0.0, the tests fail to execute, consistently yielding the following error:

Realm error : Invalid argument(s): Failed to load dynamic library 'librealm_dart.dylib': dlopen(librealm_dart.dylib, 0x0001): tried: 'librealm_dart.dylib' (no such file), 
...
LateInitializationError: Local 'realm' has not been initialized.
dart:_internal           LateError._throwLocalNotInitialized

This issue seems to recur with every Realm upgrade, but previously, I managed to resolve it. Could you please provide guidance on how to address this error? Additionally, I would greatly appreciate any advice on best practices for upgrading Realm in a manner that does not affect unit tests (increasing the version in pubspec.yaml usually breaks unit tests).

Repro steps

flutter test

Version

3.3.3/3.19.5

What Atlas Services are you using?

Local Database only

What type of application is this?

Flutter Application

Client OS and version

macOS 14.2.1

Code snippets

void main() {
late Realm realm;

test("Open a local realm", () {
realm = Realm(Configuration.local([Car.schema]));
expect(realm.isClosed, isFalse);
});

tearDown(() {
realm.close();
});
}

Stacktrace of the exception/crash you're getting

No response

Relevant log output

Realm error : Invalid argument(s): Failed to load dynamic library 'librealm_dart.dylib': dlopen(librealm_dart.dylib, 0x0001): tried: 'librealm_dart.dylib' (no such file), 
...
LateInitializationError: Local 'realm' has not been initialized.
dart:_internal           LateError._throwLocalNotInitialized
Copy link

sync-by-unito bot commented Apr 4, 2024

➤ PM Bot commented:

Jira ticket: RDART-999

@nielsenko
Copy link
Contributor

@Taco55 Flutter test are (unlike driver or integration tests) run on your local host. Please run:

dart run realm install

in the root of your project, similar to what you would do with a non-flutter project. This should create a binary folder in your project root, in which you will find the mentioned lib:

❯ ls binary/macos/librealm_dart.dylib 
binary/macos/librealm_dart.dylib

In your tests you might want to use Configuration.inMemory instead of Configuration.local. Something like:

import 'package:flutter_test/flutter_test.dart';
import 'package:realm/realm.dart';
import 'package:realm_unittest/main.dart';

void main() {
  test('Test with Realm', () {
    final realm = Realm(Configuration.inMemory([Counter.schema]));
    realm.write(() {
      realm.add(Counter(0));
    });
    expect(realm.all<Counter>().first.value, 0);
  });
}

Now it should work:

❯ flutter test
00:05 +1: All tests passed!

@nielsenko
Copy link
Contributor

nielsenko commented Apr 4, 2024

@Taco55 .. so the above is how it should work, but it seems we have an issue in 2.0.0.

WORKAROUND

Before running dart run realm install you will need to do a temporary modification to your pubspec.yaml.

dependencies:
# disable flutter dep
#  flutter:
#    sdk: flutter
  realm: ^2.0.0
  realm_dart: ^2.0.0 # add explicit dep on realm_dart

run:

dart pub get
dart run realm install

Revert the changes to pubspec.yaml

@nielsenko nielsenko self-assigned this Apr 4, 2024
@Taco55
Copy link
Author

Taco55 commented Apr 7, 2024

Yes, that did the trick! Thanks for your help.

@Taco55 Taco55 closed this as completed Apr 7, 2024
@mingchen3563
Copy link

mingchen3563 commented Apr 9, 2024

@Taco55 Flutter test are (unlike driver or integration tests) run on your local host. Please run:

dart run realm install

in the root of your project, similar to what you would do with a non-flutter project. This should create a binary folder in your project root, in which you will find the mentioned lib:

❯ ls binary/macos/librealm_dart.dylib 
binary/macos/librealm_dart.dylib

When I do

dart run realm install

It want me to specified target OS, which it didn't before 2.0.0

Building package executable... 
Built realm:realm.
Target OS not specified
-t, --target-os-type    The target OS to install binaries for.
                        [android, ios, linux, macos, windows]

So I specified the target OS like below

dart run realm install -t macos

But it install to the unexpected path

Building package executable... 
Built realm:realm.
Directory: '/Users/ming.chen/.pub-cache/hosted/pub.dev/realm-2.0.0/macos'
Realm binaries for 2.0.0 already downloaded
Realm install command finished.

After I copied librealm_dart.dylib and realm_version.txt to the path you mentioned above in my project root, it works fine.
Maybe it's the part dart run realm install -t macos which install the sdk to the wrong folder makes it goes wrong?

@nirinchev
Copy link
Member

@nielsenko should we reopen this until we have a proper fix?

@nirinchev nirinchev reopened this Apr 9, 2024
@nielsenko
Copy link
Contributor

@nirinchev 👍

@triplehman3563 The reason (and the reason why the work-around works) is that in 2.0.0 we deduce the project type (flutter vs. dart) from the pubspec.yaml. Combined with how(/where) realm tries to open the shared library we get in trouble for flutter test.

Copying the lib in place like you did will also work.

@nielsenko nielsenko linked a pull request Apr 10, 2024 that will close this issue
@derrickgw
Copy link

I copied the .so manually in my github-action, but it still isn't working. Would this be part of the same issue?

 + dart pub run realm install -t linux
Directory: '/opt/hostedtoolcache/flutter/stable-3.19.5-x64/.pub-cache/hosted/pub.dev/realm-2.0.0/linux/binary/linux'
Downloading Realm binaries for 2.0.0 to /tmp/realm-binary-ZPACCB/linux.tar.gz
Extracting Realm binaries to /opt/hostedtoolcache/flutter/stable-3.19.5-x64/.pub-cache/hosted/pub.dev/realm-2.0.0/linux/binary/linux
extracting librealm_dart.so

Archive /tmp/realm-binary-ZPACCB/linux.tar.gz extracted to /opt/hostedtoolcache/flutter/stable-3.19.5-x64/.pub-cache/hosted/pub.dev/realm-2.0.0/linux/binary/linux
Realm install command finished.
+ mv /opt/hostedtoolcache/flutter/stable-3.19.5-x64/.pub-cache/hosted/pub.dev/realm-2.0.0/linux/binary .
+ flutter test --branch-coverage

...

  Realm error : Could not open librealm_dart.so. Tried:
  - "/opt/hostedtoolcache/flutter/stable-3.19.5-x64/bin/cache/artifacts/engine/linux-x64/lib/librealm_dart.so"
  Hint: Did you forget to add a dependency on the realm package?
  package:realm_dart/src/init.dart 71:5                _openRealmLib.throwError
  package:realm_dart/src/init.dart 84:29               _openRealmLib
  package:realm_dart/src/init.dart 120:24              initRealm

Compared to the error before I copied the *.so, which matches the OPs error:

dart pub run realm install -t linux
Directory: '/opt/hostedtoolcache/flutter/stable-3.19.5-x64/.pub-cache/hosted/pub.dev/realm-2.0.0/linux/binary/linux'
Downloading Realm binaries for 2.0.0 to /tmp/realm-binary-XBVODF/linux.tar.gz
Extracting Realm binaries to /opt/hostedtoolcache/flutter/stable-3.19.5-x64/.pub-cache/hosted/pub.dev/realm-2.0.0/linux/binary/linux
extracting librealm_dart.so

Archive /tmp/realm-binary-XBVODF/linux.tar.gz extracted to /opt/hostedtoolcache/flutter/stable-3.19.5-x64/.pub-cache/hosted/pub.dev/realm-2.0.0/linux/binary/linux
Realm install command finished.
+ flutter test --branch-coverage

...

  Realm error : Invalid argument(s): Failed to load dynamic library 'librealm_dart.so': librealm_dart.so: cannot open shared object file: No such file or directory
  Invalid argument(s): Failed to load dynamic library '/opt/hostedtoolcache/flutter/stable-3.19.5-x64/bin/cache/artifacts/engine/linux-x64/librealm_dart.so': /opt/hostedtoolcache/flutter/stable-3.19.5-x64/bin/cache/artifacts/engine/linux-x64/librealm_dart.so: cannot open shared object file: No such file or directory
  Invalid argument(s): Failed to load dynamic library '/home/runner/work/health_tracker/health_tracker/binary/linux/librealm_dart.so': /home/runner/work/health_tracker/health_tracker/binary/linux/librealm_dart.so: cannot open shared object file: No such file or directory
  Invalid argument(s): Failed to load dynamic library '/opt/hostedtoolcache/flutter/stable-3.19.5-x64/bin/cache/artifacts/engine/linux-x64/lib/librealm_dart.so': /opt/hostedtoolcache/flutter/stable-3.19.5-x64/bin/cache/artifacts/engine/linux-x64/lib/librealm_dart.so: cannot open shared object file: No such file or directory
  package:realm_dart/src/init.dart 84:3                _openRealmLib
  package:realm_dart/src/init.dart 108:24              initRealm

@richard457
Copy link

richard457 commented Apr 30, 2024

I have done

dart run realm install -t macos
Building package executable... (1.4s)
Built realm:realm.
Directory: '/Users/richard/.pub-cache/hosted/pub.dev/realm-2.1.0/macos'
Realm binaries for 2.1.0 already downloaded
Realm install command finished.
Screenshot 2024-04-30 at 11 57 48 PM

@richard457
Copy link

any fix to this @nirinchev @nielsenko would be appreciated

@nielsenko
Copy link
Contributor

Sorry, the fix did not make it into 2.1.0, 2.2.0, or 2.2.1. The work-around still stand, and I will try to get the fix merged before next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants