dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
114 stars 40 forks source link

Add support / documentation for native assets in pub executables #1575

Open jackd opened 3 days ago

jackd commented 3 days ago

I'm looking for a way to use native assets in a pub-configured executable.

I apologise in advance if this is already implemented / documented somewhere, but having tried to minimally change the native_add_app example I haven't had much luck.

Steps to reproduce:

  1. run dart pub get in native_add_app and native_add_library
  2. add the following to native_add_app/pubspec.yaml:
    executables:
    native_add_app:
  3. activate the package globally
    dart pub global activate --source path ./native_add_app
  4. Run
    native_add_app

Result:

$ native_add_app
Building package executable...
Built native_add_app:native_add_app.
Invoking a native function to calculate 1 + 2.
Unhandled exception:
Invalid argument(s): Couldn't resolve native function 'add' in 'package:native_add_library/native_add_library.dart' : No asset with id 'package:native_add_library/native_add_library.dart' found. No available native assets. Attempted to fallback to process lookup. /home/jackd/programs/flutter/bin/cache/dart-sdk/bin/dart: undefined symbol: add.

#0      Native._ffi_resolver.#ffiClosure0 (dart:ffi-patch/ffi_patch.dart)
#1      Native._ffi_resolver_function (dart:ffi-patch/ffi_patch.dart:1523:20)
#2      add (package:native_add_library/native_add_library.dart)
#3      main (file:///home/jackd/Development/dart/native/pkgs/native_assets_cli/example/build/native_add_app/bin/native_add_app.dart:9:18)
#4      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#5      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
jackd commented 6 hours ago

Managed to get things working with

dart pub global run --enable-experiment=native-assets native_add_app:native_add_app

Not quite as convenient as native_add_app but I can't see any way to make the script pub makes at ~/.pub-cache/bin/native_add_app play nicely.

Having said that, it would be nice if the error message was a little more informative. If I dart run bin/native_add_app I get the much more helpful message:

Package(s) native_add_library require the native assets feature to be enabled. Enable native assets with `--enable-experiment=native-assets`.