hostNativeLibraryCandidates function

List<String> hostNativeLibraryCandidates({
  1. required String libFileName,
  2. String? envOverride,
  3. String? cacheNamespace,
  4. List<String> relativePaths = const [],
  5. int walkUpLevels = 8,
})

Every path to try for a host-side native library, in order.

envOverride is the value of a caller-specific environment variable, or null. cacheNamespace is the bundle's subdirectory under the cache root — null for the flat layout LiteRT uses. relativePaths are package-relative, and each is tried at every level walked up from the working directory.

Returns the list rather than the first hit so callers can name every path they tried: a wrong working directory and a genuinely absent library otherwise produce the same message, which is what made the search look like a build failure.

Implementation

List<String> hostNativeLibraryCandidates({
  required String libFileName,
  String? envOverride,
  String? cacheNamespace,
  List<String> relativePaths = const [],
  int walkUpLevels = 8,
}) {
  final out = <String>[];
  if (envOverride != null && envOverride.isNotEmpty) out.add(envOverride);

  final base = hostNativeCacheBase();
  final host = hostNativeDirName();
  if (base != null && host != null) {
    final ns = cacheNamespace == null ? '' : '$cacheNamespace/';
    out.add('$base/$ns$host/$libFileName');
  }

  if (relativePaths.isNotEmpty) {
    var dir = Directory.current.absolute;
    for (var hop = 0; hop < walkUpLevels; hop++) {
      for (final rel in relativePaths) {
        out.add('${dir.path}/$rel');
      }
      final parent = dir.parent;
      if (parent.path == dir.path) break;
      dir = parent;
    }
  }
  return out;
}