Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
aprosail opened this issue May 19, 2024 · 11 comments
Closed

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

aprosail opened this issue May 19, 2024 · 11 comments
Labels
r: solved Issue is closed as solved

Comments

@aprosail
Copy link

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.
@aprosail
Copy link
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.

@aprosail
Copy link
Author

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

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label May 20, 2024
@darshankawar
Copy link
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 ?

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 20, 2024
@mko15
Copy link

mko15 commented May 20, 2024

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.

@aprosail
Copy link
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.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 20, 2024
@aprosail
Copy link
Author

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

@aprosail
Copy link
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).

@KevinBrendel
Copy link

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.

@aprosail
Copy link
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.

@mko15
Copy link

mko15 commented May 20, 2024

Yep, it works :) Thank you @aprosail

@darshankawar darshankawar added r: solved Issue is closed as solved and removed in triage Presently being triaged by the triage team labels May 21, 2024
Copy link

github-actions bot commented Jun 4, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
r: solved Issue is closed as solved
Projects
None yet
Development

No branches or pull requests

4 participants