Skip to content

[flutter_tools] Redirect macOS sandbox container DevFS to application_darwin_temp_dir - #195328

Open
bkonyi wants to merge 7 commits into
flutter:masterfrom
bkonyi:issue-193907
Open

bkonyi wants to merge 7 commits into
flutter:masterfrom
bkonyi:issue-193907

Conversation

@bkonyi

@bkonyi bkonyi commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Sandboxed macOS apps place their DevFS directory inside ~/Library/Containers/<bundle-id>/Data/tmp/. Starting in macOS 14, macOS TCC associates sandbox containers with the app's code signature (kTCCServiceSystemPolicyAppData) and blocks external processes like flutter_tools from writing directly into the container via LocalDevFSWriter unless the user approves the TCC prompt or the terminal/IDE has Full Disk Access, causing hot reload and hot restart to exit with a ToolExit.

Redirect macOS App Sandbox container DevFS paths (/Library/Containers/<bundle-id>/Data/tmp/<temp-dir>/<fs-name>/) in DevFS._prepareDeviceUri to application_darwin_temp_dir (<DARWIN_USER_TEMP_DIR>/<bundle-id>/<temp-dir>/<fs-name>/, resolved via getconf DARWIN_USER_TEMP_DIR). The macOS App Sandbox profile (/System/Library/Sandbox/Profiles/application.sb) grants every sandboxed app full read/write access to <DARWIN_USER_TEMP_DIR>/<bundle-id>, which lies outside ~/Library/Containers/ and is not subject to TCC container protection, preserving fast APFS copy-on-write file copies via LocalDevFSWriter.

Reproduction was confirmed manually in Terminal.app on macOS with an Apple Development-signed sandboxed app (kTCCServiceSystemPolicyAppData denied for com.apple.Terminal).

Current output:

OUT: [   +4 ms] DevFS: Creating new filesystem on the device (null)
OUT: [  +17 ms] DevFS: Created new filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [        ] DevFS: Sync finished
OUT: [        ] Flutter run key commands.
OUT: [        ] r Hot reload. 🔥🔥🔥
OUT: [        ] R Hot restart.
OUT: [ +152 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [        ] Performing hot reload...
OUT: [   +1 ms] DevFS: Sync finished
OUT: [   +3 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [  +30 ms] Hot reload performed in 122ms.
OUT: [        ] Performing hot reload... (completed in 126ms)
OUT: [        ] Reloaded 1 of 1051 libraries in 132ms (compile: 25 ms, reload: 46 ms, reassemble: 34 ms).
OUT: [   +1 ms] Performing hot restart...
OUT: [  +38 ms] DevFS: Sync finished
OUT: [ +243 ms] Hot restart performed in 343ms.
OUT: [        ] Performing hot restart... (completed in 344ms)
OUT: [        ] Restarted application in 354ms.
OUT: [ +263 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [+1742 ms] DevFS: Deleting filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [  +11 ms] DevFS: Deleted filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [   +7 ms] Application finished.

Fixes #193907

Sandboxed macOS apps place their DevFS directory inside ~/Library/Containers/<bundle-id>/Data/tmp/. Starting in macOS 14, macOS TCC associates sandbox containers with the app's code signature and blocks external processes like `flutter_tools` from writing directly into the container via `LocalDevFSWriter` unless the user approves the TCC prompt or the terminal/IDE has Full Disk Access, causing hot reload and hot restart to exit with a `ToolExit`.

Override `MacOSDevice.createDevFSWriter` to return `null` (matching the transport already used by `flutter attach -d macos`) so `DevFS` syncs updated kernel and asset files over the VM service HTTP transport and the app process writes to its own container. Note that this reverts the local file copy from flutter#66678 for macOS targets, including non-sandboxed benchmarks like `hot_mode_dev_cycle_macos_target__benchmark`.

Previous output:
```
Performing hot restart...
Application finished.
Flutter failed to create file at "/Users/user/Library/Containers/com.example.issue193907Repro/Data/tmp/appExfFRB/app/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.
```

Current output:
```
Performing hot reload...
Reloaded 0 libraries in 114ms (compile: 16 ms, reload: 0 ms, reassemble: 35 ms).

Performing hot restart...
Restarted application in 623ms.
```

Fixes flutter#193907
@bkonyi bkonyi added the CICD Run CI/CD label Oct 26, 2026
@github-actions github-actions Bot added tool Affects the "flutter" command-line tool. See also t: labels. platform-macos Building on or for macOS specifically a: desktop Running on desktop team-ios Owned by iOS platform team team-macos Owned by the macOS platform team labels Oct 26, 2026
@bkonyi bkonyi changed the title [flutter_tools] Sync macOS DevFS over VM service HTTP transport [flutter_tools] Redirect macOS sandbox container DevFS to application_darwin_temp_dir Oct 27, 2026
…_darwin_temp_dir

Sandboxed macOS apps place their DevFS directory inside `~/Library/Containers/<bundle-id>/Data/tmp/`. Starting in macOS 14, macOS TCC associates sandbox containers with the app's code signature (`kTCCServiceSystemPolicyAppData`) and blocks external processes like `flutter_tools` from writing directly into the container via `LocalDevFSWriter` unless the user approves the TCC prompt or the terminal/IDE has Full Disk Access.

Falling back to `_DevFSHttpWriter` (`MacOSDevice.createDevFSWriter` returning `null`) avoids direct container writes, but regresses macOS hot restart times from ~200-400ms to ~660-880ms because `main.dart.dill` is ~70 MB and must be gzip-compressed and streamed over HTTP instead of cloned via APFS copy-on-write (`File.copySync`).

Instead, revert the `MacOSDevice` / `DesktopDevice` writer change and redirect macOS sandbox container DevFS paths (`/Library/Containers/<bundle-id>/Data/tmp/<temp-dir>/<fs-name>/`) in `DevFS._prepareDeviceUri` to `application_darwin_temp_dir` (`<DARWIN_USER_TEMP_DIR>/<bundle-id>/<temp-dir>/<fs-name>/`, resolved via `getconf DARWIN_USER_TEMP_DIR`). The macOS App Sandbox profile (`/System/Library/Sandbox/Profiles/application.sb`) grants every sandboxed app full read/write access to `<DARWIN_USER_TEMP_DIR>/<bundle-id>`, which lies outside `~/Library/Containers/` and is not subject to TCC container protection.

Reproduction was confirmed manually in `Terminal.app` on macOS with an `Apple Development`-signed sandboxed app (`kTCCServiceSystemPolicyAppData` denied for `com.apple.Terminal`).

Current output:
```
OUT: [   +4 ms] DevFS: Creating new filesystem on the device (null)
OUT: [  +17 ms] DevFS: Created new filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [        ] DevFS: Sync finished
OUT: [        ] Flutter run key commands.
OUT: [        ] r Hot reload. 🔥🔥🔥
OUT: [        ] R Hot restart.
OUT: [ +152 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [        ] Performing hot reload...
OUT: [   +1 ms] DevFS: Sync finished
OUT: [   +3 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [  +30 ms] Hot reload performed in 122ms.
OUT: [        ] Performing hot reload... (completed in 126ms)
OUT: [        ] Reloaded 1 of 1051 libraries in 132ms (compile: 25 ms, reload: 46 ms, reassemble: 34 ms).
OUT: [   +1 ms] Performing hot restart...
OUT: [  +38 ms] DevFS: Sync finished
OUT: [ +243 ms] Hot restart performed in 343ms.
OUT: [        ] Performing hot restart... (completed in 344ms)
OUT: [        ] Restarted application in 354ms.
OUT: [ +263 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [+1742 ms] DevFS: Deleting filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [  +11 ms] DevFS: Deleted filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [   +7 ms] Application finished.
```

Fixes flutter#193907
@github-actions github-actions Bot removed platform-macos Building on or for macOS specifically a: desktop Running on desktop team-ios Owned by iOS platform team team-macos Owned by the macOS platform team labels Oct 27, 2026
@bkonyi
bkonyi marked this pull request as ready for review November 5, 2026 01:20

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request redirects macOS sandbox container DevFS URIs to the DARWIN_USER_TEMP_DIR to prevent macOS TCC container protection issues, and ensures the redirected directories are cleaned up on destruction. It also adds corresponding unit tests. The review feedback suggests making the regular expression used to match the sandbox container path case-insensitive to robustly handle potential casing variations on macOS.

…tively

Pass `caseSensitive: false` to `DevFS._macOSSandboxContainerTempRegExp` so macOS App Sandbox container DevFS paths with non-canonical directory casing (for example `/users/.../library/containers/<bundle-id>/data/tmp/...` on case-insensitive APFS) still redirect to `<DARWIN_USER_TEMP_DIR>/<bundle-id>/...` while preserving captured bundle identifier casing.

Canonical macOS paths match identically to the previous `mac-runner` verification run (`mac-runner` is currently offline); case-insensitive path matching is covered in `devfs_test.dart`.

Current output:
```
OUT: [   +4 ms] DevFS: Creating new filesystem on the device (null)
OUT: [  +17 ms] DevFS: Created new filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [        ] DevFS: Sync finished
OUT: [        ] Flutter run key commands.
OUT: [        ] r Hot reload. 🔥🔥🔥
OUT: [        ] R Hot restart.
OUT: [ +152 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [        ] Performing hot reload...
OUT: [   +1 ms] DevFS: Sync finished
OUT: [   +3 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [  +30 ms] Hot reload performed in 122ms.
OUT: [        ] Performing hot reload... (completed in 126ms)
OUT: [        ] Reloaded 1 of 1051 libraries in 132ms (compile: 25 ms, reload: 46 ms, reassemble: 34 ms).
OUT: [   +1 ms] Performing hot restart...
OUT: [  +38 ms] DevFS: Sync finished
OUT: [ +243 ms] Hot restart performed in 343ms.
OUT: [        ] Performing hot restart... (completed in 344ms)
OUT: [        ] Restarted application in 354ms.
OUT: [ +263 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [+1742 ms] DevFS: Deleting filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [  +11 ms] DevFS: Deleted filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [   +7 ms] Application finished.
```
…irectory

In production, `DevFS._toolContext.fs` is wrapped in `ErrorHandlingFileSystem`, which converts mapped `FileSystemException` errnos (such as `EACCES` and `EPERM`) on `existsSync` and `deleteSync` into `ToolExit` before `on FileSystemException` in `DevFS.destroy()` can catch them.

Wrap the best-effort `_localSandboxTempDir` cleanup in `ErrorHandlingFileSystem.noExitOnFailure` (ahead of `_vmService.deleteDevFS(fsName)` so local cleanup still completes if the app window was closed and the VM service disconnected) so permission errors during temporary directory cleanup log a trace message instead of aborting shutdown with a `ToolExit`.

Canonical macOS paths match the previous `mac-runner` verification run (`mac-runner` is currently offline); `ErrorHandlingFileSystem` cleanup error handling and local cleanup when the remote `_deleteDevFS` call fails are covered in `devfs_test.dart`.

Current output:
```
OUT: [   +4 ms] DevFS: Creating new filesystem on the device (null)
OUT: [  +17 ms] DevFS: Created new filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [        ] DevFS: Sync finished
OUT: [        ] Flutter run key commands.
OUT: [        ] r Hot reload. 🔥🔥🔥
OUT: [        ] R Hot restart.
OUT: [ +152 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [        ] Performing hot reload...
OUT: [   +1 ms] DevFS: Sync finished
OUT: [   +3 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [  +30 ms] Hot reload performed in 122ms.
OUT: [        ] Performing hot reload... (completed in 126ms)
OUT: [        ] Reloaded 1 of 1051 libraries in 132ms (compile: 25 ms, reload: 46 ms, reassemble: 34 ms).
OUT: [   +1 ms] Performing hot restart...
OUT: [  +38 ms] DevFS: Sync finished
OUT: [ +243 ms] Hot restart performed in 343ms.
OUT: [        ] Performing hot restart... (completed in 344ms)
OUT: [        ] Restarted application in 354ms.
OUT: [ +263 ms] flutter: ASSET_VERIFY_OK: FontManifest.json (208 bytes)
OUT: [+1742 ms] DevFS: Deleting filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [  +11 ms] DevFS: Deleted filesystem on the device (file:///var/folders/_y/rdj_3wtj5mbbtf6syy_2cw6800d8lv/T/com.example.sandboxRepro/sandbox_reproA1G4Iv/sandbox_repro/)
OUT: [   +7 ms] Application finished.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS hot restart fails writing DevFS files into a sandboxed app container

1 participant