Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ jobs:
- run: dart pub get
- run: dart test

- name: Test pub cache fallback (AOT compiled binary)
run: |
ARCH=$(uname -m)
if [ "$ARCH" = "arm64" ]; then LIB_ARCH="arm64"; else LIB_ARCH="x86_64"; fi
LIB_NAME="libsysres-darwin-${LIB_ARCH}.dylib"

# Create a minimal Dart script that uses the library
cat > /tmp/test_pub_cache.dart << 'SCRIPT'
import 'package:system_resources_2/system_resources_2.dart';
void main() {
SystemResources.init();
final mem = SystemResources.memUsage();
assert(mem > 0, 'Expected positive memory usage');
print('SUCCESS: memUsage=$mem');
}
SCRIPT

# Set up a fake pub cache with the versioned directory structure
FAKE_CACHE="/tmp/fake-pub-cache"
PKG_DIR="$FAKE_CACHE/hosted/pub.dev/system_resources_2-2.2.1/lib/build"
mkdir -p "$PKG_DIR"
cp "lib/build/$LIB_NAME" "$PKG_DIR/"

# Compile to a native executable
dart compile exe /tmp/test_pub_cache.dart --packages=.dart_tool/package_config.json -o /tmp/test_pub_cache

# Run from /tmp with only the fake pub cache available
# HOME=/nonexistent ensures no real ~/.pub-cache is found
cd /tmp
HOME=/nonexistent PUB_CACHE="$FAKE_CACHE" ./test_pub_cache

test-container:
name: test-container (${{ matrix.arch }})
strategy:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.2

- Fix pub cache fallback path missing version suffix for macOS native library loading in AOT-compiled binaries

## 2.2.1

- Log warning to stderr when running on Windows or other unsupported platforms
Expand Down
45 changes: 43 additions & 2 deletions lib/src/macos_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,31 @@ class MacOsNative {
if (homeDir.isNotEmpty) {
final pubCachePath =
Platform.environment['PUB_CACHE'] ?? '$homeDir/.pub-cache';
locations.add(
'$pubCachePath/hosted/pub.dev/system_resources_2/lib/build/$libName');
try {
final hostedDir = Directory('$pubCachePath/hosted/pub.dev');
if (hostedDir.existsSync()) {
final matches = hostedDir
.listSync()
.whereType<Directory>()
.where((d) {
final name = d.uri.pathSegments
.lastWhere((s) => s.isNotEmpty, orElse: () => '');
return name.startsWith('system_resources_2-');
})
.toList()
..sort((a, b) => _compareVersionedDirs(
b.uri.pathSegments
.lastWhere((s) => s.isNotEmpty, orElse: () => ''),
a.uri.pathSegments
.lastWhere((s) => s.isNotEmpty, orElse: () => ''),
));
for (final dir in matches) {
locations.add('${dir.path}/lib/build/$libName');
}
}
} catch (_) {
// Ignore errors from directory listing (e.g. permission issues)
}
}

// Try each location
Expand Down Expand Up @@ -177,6 +200,24 @@ class MacOsNative {
return _getMemoryUsedBytes!();
}

/// Compare two versioned directory names for sorting.
///
/// Expects names like `system_resources_2-2.2.1`. Extracts the version
/// suffix and compares numerically by major.minor.patch.
static int _compareVersionedDirs(String a, String b) {
final aVerStr = a.split('system_resources_2-').last;
final bVerStr = b.split('system_resources_2-').last;
final aParts = aVerStr.split('.').map((s) => int.tryParse(s) ?? 0).toList();
final bParts = bVerStr.split('.').map((s) => int.tryParse(s) ?? 0).toList();
final len = aParts.length > bParts.length ? aParts.length : bParts.length;
for (var i = 0; i < len; i++) {
final av = i < aParts.length ? aParts[i] : 0;
final bv = i < bParts.length ? bParts[i] : 0;
if (av != bv) return av.compareTo(bv);
}
return 0;
}

static void _ensureInitialized() {
if (!_initialized) {
throw StateError(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: system_resources_2
description: Provides easy access to system resources (CPU load, memory usage).
version: 2.2.1
version: 2.2.2
repository: https://github.com/serverpod/system_resources_2
homepage: https://github.com/serverpod/system_resources_2#readme

Expand Down