objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
927 stars 115 forks source link

[ERROR] Flutter desktop macOS: Storage error "Operation not permitted" (code 1) #248

Closed jochendev closed 2 years ago

jochendev commented 2 years ago

Running "flutter pub get" in untitled... 1,220ms Launching lib/main.dart on macOS in debug mode... Running pod install... 1,395ms Building macOS application...
001-10:23:16.4099 [ERROR] Storage error "Operation not permitted" (code 1) [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: ObjectBoxException: failed to create store: 10199 Could not open database environment; please check options and file system (1: Operation not permitted)

0 ObjectBoxNativeError.throwMapped (package:objectbox/src/native/bindings/helpers.dart:78:9)

1 throwLatestNativeError (package:objectbox/src/native/bindings/helpers.dart:50:48)

2 checkObxPtr (package:objectbox/src/native/bindings/helpers.dart:32:5)

3 new Store (package:objectbox/src/native/store.dart:105:7)

4 _MyHomePageState.initState. (package:untitled/main.dart:63:20)

5 _rootRunUnary (dart:async/zone.dart:1362:47)

6 _CustomZone.runUnary (dart:async/zone.dart:1265:19)

7 _FutureListener.handleValue (dart:async/future_impl.dart:152:18)

8 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:704:45)

9 Future._propagateToListeners (dart:async/future_impl.dart:733:32)

10 Future._completeWithValue (dart:async/future_impl.dart:539:5)

11 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:254:13)

12 getTemporaryDirectory (package:path_provider/path_provider.dart)

Syncing files to device macOS... 88ms Flutter run key commands. r Hot reload. 🔥🔥🔥 R Hot restart. h Repeat this help message. d Detach (terminate "flutter run" but leave application running). c Clear the screen q Quit (terminate the application on the device). An Observatory debugger and profiler on macOS is available at: http://127.0.0.1:59804/kwcL1lLkKYA=/ Flutter DevTools, a Flutter debugger and profiler, on macOS is available at: http://127.0.0.1:9106?uri=http%3A%2F%2F127.0.0.1%3A59804%2FkwcL1lLkKYA%3D%2F 💪 Running with sound null safety 💪 ``` ``` import 'package:flutter/material.dart'; import 'package:objectbox/objectbox.dart'; import 'package:path_provider/path_provider.dart'; import 'objectbox.g.dart'; @Entity() class Note { int id; String text; String? comment; DateTime date; Note(this.text, {this.id = 0, this.comment, DateTime? date}) : date = date ?? DateTime.now(); } void main() { runApp(MyHomePage()); } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override void initState() { super.initState(); getTemporaryDirectory().then((value) { var _store = Store(getObjectBoxModel(), directory: value.path + '/objectbox'); }); } @override Widget build(BuildContext context) { return Container(); } } ``` ``` flutter doctor -v [✓] Flutter (Channel stable, 2.0.6, on macOS 11.3.1 20E241 darwin-arm, locale zh-Hans) • Flutter version 2.0.6 at /Users/jochen/Documents/dev/flutter • Framework revision 1d9032c7e1 (3 weeks ago), 2021-04-29 17:37:58 -0700 • Engine revision 05e680e202 • Dart version 2.12.3 • Pub download mirror https://pub.flutter-io.cn • Flutter download mirror https://storage.flutter-io.cn [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/jochen/Library/Android/sdk • Platform android-30, build-tools 30.0.3 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.5, Build version 12E262 • CocoaPods version 1.10.1 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 4.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) [✓] VS Code (version 1.56.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension can be installed from: 🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [✓] Connected device (2 available) • macOS (desktop) • macos • darwin-arm64 • macOS 11.3.1 20E241 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.212 ```
vaind commented 2 years ago

Thanks for bumping this to a standalone issue out of #166 - I'll repeat my last comment from there:

I was able to reproduce the macOS issue and indeed it tries to write to the right directory. I've made sure the directory exists so it was only supposed to create files but the issue was still the same. I suspect this has something to do with the app being sandboxed so objectbox-core doesn't have the right permissions to create the database properly. I'll discuss internally and I've noticed there're some docs about this for objectbox-swift: https://swift.objectbox.io/advanced/macos - not sure if any of that applies.

AncientPixel commented 2 years ago

Thanks for bumping this to a standalone issue out of #166 - I'll repeat my last comment from there:

I was able to reproduce the macOS issue and indeed it tries to write to the right directory. I've made sure the directory exists so it was only supposed to create files but the issue was still the same. I suspect this has something to do with the app being sandboxed so objectbox-core doesn't have the right permissions to create the database properly. I'll discuss internally and I've noticed there're some docs about this for objectbox-swift: https://swift.objectbox.io/advanced/macos - not sure if any of that applies.

I followed the steps on the supplied link but still get the same error as original poster reported.

greenrobot commented 2 years ago

There's a macOS specific section in the Flutter Desktop docs: https://flutter.dev/desktop#macos-specific-support

Haven't tried yet, but seems like a different approach...

AncientPixel commented 2 years ago

Thanks for that link, I've updated the following lines to FALSE in my DebugProfile.entitlements and Release.entitlements file: <key>com.apple.security.app-sandbox</key> <false/> It is now working.

vaind commented 2 years ago

<key>com.apple.security.app-sandbox</key> <false/>

However, that means you won't be able to distribute the app through app store, right?

TeddyLourson commented 2 years ago

I did the same thing and turned off app-sandbox temporary so I can continue working on my app but I will have to enable it when it's done. From what I understood it's a special security thing from Apple that doesn't allow you to write/read files outside of your app's container folder without the user's consent. But no matter the path I'm giving to ObjectBox it keeps throwing me this error while just creating a simple file at that same path works.

vaind commented 2 years ago

Yeah, I'm 99.9 % sure it's not about the path but some of the other native API calls that are being executed during store setup. We'll have to take app-sandboxing as a known limitation for now until we figure out what permissions to set up so that it works. And I'm pretty sure it will, since the same native code is used for objectbox-swift...

greenrobot commented 2 years ago

The com.apple.security.cs.disable-library-validation entitlement is a candidate for causing the error:

This security-hardening feature prevents a program from loading frameworks, plug-ins, or libraries unless they’re either signed by Apple or signed with the same Team ID as the main executable.

Not exactly sure what Flutter does with the ObjectBox lib; depending on that there may be a mismatch...

Maybe someone likes to try this entitlement and report back?

TeddyLourson commented 2 years ago

I just tried it, but I still get the exception.

sunilpandit2 commented 3 weeks ago

<key>com.apple.security.app-sandbox</key> <false/>

problem solved

greenrobot-team commented 3 weeks ago

@sunilpandit2 For development this is fine. But to build a release app, you might want to set a macosApplicationGroup as described in the Store docs.

sunilpandit2 commented 3 weeks ago

Okay

Thank you for responding

On 22 Apr 2024, at 12:47 PM, Uwe - ObjectBox @.***> wrote:

@sunilpandit2 https://github.com/sunilpandit2 For development this is fine. But to build a release app, you might want to set a macosApplicationGroup as described in the Store docs https://pub.dev/documentation/objectbox/latest/objectbox/Store/Store.html.

— Reply to this email directly, view it on GitHub https://github.com/objectbox/objectbox-dart/issues/248#issuecomment-2068657229, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMNYFVWNBQLUIY4F7R3PPTDY6S2O3AVCNFSM45ELNLS2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBWHA3DKNZSGI4Q. You are receiving this because you were mentioned.