Skip to content

Flutter hot reload and hot restart stop working on macOS #148648

Closed

Description

@aprosail

Steps to reproduce

  1. Flutter version 3.19.6, stable channel.
  2. macOS Sonama 14.4.1 on Apple M2 Chip.
  3. Once create a new flutter app and run, it can hot restart or hot reload normally.
  4. Once the version code in pub spec.yaml changed, it cannot hot reload or hot restart.
  5. Even if the version code changed back to the initial version code (1.0.0+1) again, it still not cannot work.
  6. I've tried many different app names, domains and even local positions, but all them failed again and again.

Expected results

In the previous versions, I can easily hot reload or hot restart whatever version code I have.
But in this version, once version code changed, it cannot hot reload or hot restart.

Actual results

Followings are the exception output:

Flutter failed to create file at
"/Users/aprosail/Library/Containers/com.example.hello/Data/tmp/helloCrWZCY/hello/main.dart.incremental.dill".
Please ensure that the SDK and/or project is installed in a location that has read/write permissions for the current user.

I think that might be a conflict with Apple's security strategies, that macOS will block Flutter from command line
to modify such application when something changed.

Code sample

It's just the same as the template.

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // TRY THIS: Try running your application with "flutter run". You'll see
        // the application has a purple toolbar. Then, without quitting the app,
        // try changing the seedColor in the colorScheme below to Colors.green
        // and then invoke "hot reload" (save your changes or press the "hot
        // reload" button in a Flutter-supported IDE, or press "r" if you used
        // the command line to start the app).
        //
        // Notice that the counter didn't reset back to zero; the application
        // state is not lost during the reload. To reset the state, use hot
        // restart instead.
        //
        // This works for code too, not just values: Most code changes can be
        // tested with just a hot reload.
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // TRY THIS: Try changing the color here to a specific color (to
        // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
        // change color while the other colors stay the same.
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          //
          // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
          // action in the IDE, or press "p" in the console), to see the
          // wireframe for each widget.
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Screenshots or Video

No response

Logs

Launching lib/main.dart on macOS in debug mode...
--- xcodebuild: WARNING: Using the first of multiple matching destinations:
{ platform:macOS, arch:arm64, id:00001234-0001234567890ABE, name:My Mac }
{ platform:macOS, arch:x86_64, id:00001234-0001234567890ABE, name:My Mac }
Building macOS application...                                           
Syncing files to device macOS...                                    45ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on macOS is available at: http://127.0.0.1:50005/CS__JsIRqV0=/
The Flutter DevTools debugger and profiler on macOS is available at: http://127.0.0.1:9107?uri=http://127.0.0.1:50005/CS__JsIRqV0=/
2024-05-20 00:24:13.907 hello[18429:6170078] WARNING: Secure coding is automatically enabled for restorable state! However, not on all supported macOS versions of this application. Opt-in to secure coding explicitly by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState:.

Performing hot reload...                                                
Application finished.
Flutter failed to create file at
"/Users/aprosail/Library/Containers/com.example.hello/Data/tmp/helloCrWZCY/hello/main.dart.incremental.dill".
Please ensure that the SDK and/or project is installed in a location that has read/write permissions for the current user.

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.6, on macOS 14.4.1 23E224 darwin-arm64, locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] VS Code (version 1.89.1)
[✓] Connected device (2 available)
[✓] Network resources

! Doctor found issues in 1 category.

Activity

  1. aprosail commented on May 19, 2024

    @aprosail
    Author

    This issue is similar to #139875 (which happened on VSCode but this happened even in macOS command line). That issue was closed, but I'm afraid that this problem is not finished yet.

  2. aprosail commented on May 19, 2024

    @aprosail
    Author

    I've update Flutter version to 3.22.0 but this problem still remains.

  3. added
    in triage
    on May 20, 2024
  4. darshankawar commented on May 20, 2024

    @darshankawar
    Member

    Thanks for the report @aprosail
    Although I don't have M2 and Sonoma OS, I tried on Intel on which I wasn't able to replicate the error.
    Can you check this with underlying comments and see if they help in your case or not ?

  5. added
    waiting for response
    on May 20, 2024
  6. mko15 commented on May 20, 2024

    @mko15

    Same here,
    Apple M1 + Sonoma 14.2
    Hot restart is not working on a clean app for the macOS platform. iOS and Android do not have this issue.

    Flutter failed to create file at "/Users/XXX/Library/Containers/com.example.XXX/Data/tmp/XXX70BulR/op_stock_assistant/main.dart.dill".
    Please ensure that the SDK and/or project is installed in a location that has read/write permissions for the current user.
    

    I've add permissions, tried Flutter 3.19.1 and 3.22.0, but nothing changed.

  7. aprosail commented on May 20, 2024

    @aprosail
    Author

    Thanks for your attention @darshankawar, and I've read the issue #70216 ,
    but such solutions cannot be applied on macOS environment.

    I guess that's not a problem about the chip, but the OS.
    There might be some security strategy to prevent modifying executables or libraries (the .dill file)
    when such application is running.
    The Flutter dev tool might be recognized as something like a virus,
    and if so, there must be some way to allow such operations.

    Please attention that I met such problem after the version code changed.
    When running for the first few times without changing the initial version code, it still runs normally.
    You can try it after changing the version code of the flutter app, or update to macOS Sonoma if possible.

    I'm now running on Chrome as an alternative.
    But there's still some platform specified code that I need to preview on macOS.
    Rebuilding once and once again really takes much longer time than hot reload or hot restart.

  8. removed
    waiting for response
    on May 20, 2024
  9. aprosail commented on May 20, 2024

    @aprosail
    Author

    @mko15 how do you "add permission"? I'm interested and I wanna to try it.

  10. aprosail commented on May 20, 2024

    @aprosail
    Author

    Well, I'm not a really skilled programmer that I cannot dive deep into Flutter's source code to fix such bug.
    But there might be two solutions in my opinion, just for reference:

    1. Modify Flutter source code to prevent triggering platform security protections.
    2. Find out how to configure macOS settings to allow such operations from Flutter dev tools. (If so, it's strongly recommended to mention it inside official docs).
  11. KevinBrendel commented on May 20, 2024

    @KevinBrendel

    For me, macOS had a popup asking me to give VS Code the ability to modify other apps when I tried to hot reload (and it worked afterwards). So this is something which can be allowed by the user, but not sure how to grant this permission without the popup.

  12. aprosail commented on May 20, 2024

    @aprosail
    Author

    Thanks a lot @KevinBrendel , I found a solution inspired by your words:

    On macOS Sonoma, open System Settings > Privacy & Security > Full Disk Access, and allow for your code editor (such as VSCode or Android Studio) or the terminal, and then it can hot reload and hot restart. @mko15 , maybe you can try it.
    However, there will be potential risk about such permission, especially when enabling for the terminal.

    Sorry for taking your time @darshankawar , but I still recommend to add some mentions into official docs to let people know there might be such problem and how to solve it.

  13. mko15 commented on May 20, 2024

    @mko15

    Yep, it works :) Thank you @aprosail

  14. added
    r: solved
    and removed
    in triage
    on May 21, 2024
  15. github-actions commented on Jun 4, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  16. locked as resolved and limited conversation to collaborators on Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: solved

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions