Skip to content

[flutter_tools] Lazily initialize AppleContext dependencies via builder closures - #191992

Merged
auto-submit[bot] merged 7 commits into
flutter:masterfrom
bkonyi:optimize_apple_context_dependencies
Nov 4, 2026
Merged

auto-submit[bot] merged 7 commits into
flutter:masterfrom
bkonyi:optimize_apple_context_dependencies

Conversation

@bkonyi

@bkonyi bkonyi commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR updates AppleContext to initialize its 8 Apple-specific dependencies via lazy builder closures memoized in late final fields, and defers construction of XCDevice, IOSSimulatorUtils, and CocoaPodsValidator out of ToolDependencies.bootstrap and generateCommands.

Why this is needed now

Previously, AppleContext held 8 eager fields (cocoaPods, cocoapodsValidator, iosSimulatorUtils, iosWorkflow, plistParser, xcdevice, xcode, xcodeProjectInterpreter), and ToolDependencies.bootstrap and runner.run instantiated all 8 unconditionally on every host operating system (including Linux and Windows) and on commands that do not target Apple devices. In particular, XCDevice eagerly constructed IMobileDevice, IOSDeploy, IOSCoreDeviceControl, XcodeDebug, and registered a shutdown hook, and IOSSimulatorUtils eagerly constructed SimControl.

Note that Xcode, XcodeProjectInterpreter, CocoaPods, IOSWorkflow, and PlistParser are still accessed during startup (AttachCommand/CleanCommand/DaemonCommand/BuildCommand in generateCommands, EmulatorManager, and FlutterProjectFactory), though their constructors only assign fields and do not spawn processes.

Changes

  • Updated AppleContext to accept builder closures (cocoaPodsBuilder, cocoapodsValidatorBuilder, iosSimulatorUtilsBuilder, iosWorkflowBuilder, plistParserBuilder, xcdeviceBuilder, xcodeBuilder, and xcodeProjectInterpreterBuilder) backed by late final fields for lazy evaluation and memoization.
  • Updated ToolDependencies.bootstrap to use late final locals for Apple dependencies and accept optional *Builder callbacks, and updated runner.dart to pass lazy builder closures for globals fallbacks.
  • Updated FlutterDeviceManager to remove redundant iosSimulatorUtils, iosWorkflow, and xcDevice constructor parameters and initialize deviceDiscoverers lazily via late final from _appleContext.

Related Issues

Part of #47161 (flutter_tools modular dependency injection migration).

Tests

  • Updated packages/flutter_tools/test/general.shard/context/dependency_injection_test.dart to verify AppleContext lazily evaluates and memoizes all 8 dependencies and ToolDependencies.bootstrap defers XCDevice construction until first access.
  • Updated packages/flutter_tools/test/general.shard/runner/runner_test.dart to verify CocoaPodsValidator, IOSSimulatorUtils, and XCDevice are not eagerly evaluated during bootstrap or generateCommands.

…er closures

Defers construction of all 8 Apple-specific dependencies in `AppleContext`
(`cocoaPods`, `cocoapodsValidator`, `iosSimulatorUtils`, `iosWorkflow`,
`plistParser`, `xcdevice`, `xcode`, `xcodeProjectInterpreter`) out of
`ToolDependencies.bootstrap` by using lazy builder closures memoized in
`late final` properties.

Additionally, scopes `pubspec.yaml` reading and `FlutterFeatureFlags`
construction inside `iosWorkflowBuilder` so synchronous disk I/O does
not run on CLI startup for non-Apple commands or platforms.
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 28, 2026
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Aug 28, 2026

@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 refactors AppleContext and its dependencies in ToolDependencies to be evaluated lazily. Instead of eagerly constructing Apple-specific dependencies, AppleContext now accepts builder functions and initializes its fields using late final variables. The builders memoize the resolved instances to ensure they are shared across multiple reads. Unit tests have been updated and added to verify the lazy evaluation, memoization, and override behaviors. There are no review comments, and I have no feedback to provide.

@bkonyi
bkonyi requested a review from chingjun November 3, 2026 00:53
…ext_dependencies

# Conflicts:
#	packages/flutter_tools/lib/src/flutter_device_manager.dart
@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 3, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 3, 2026
Merged via the queue into flutter:master with commit 2d02619 Nov 4, 2026
22 of 23 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 4, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 4, 2026
flutter/flutter@9347202...3623d2d

2026-11-04 [email protected] Roll Skia from 49ec2655bf51 to aaa39bfcd393 (5 revisions) (flutter/flutter#195956)
2026-11-04 [email protected] Roll pub packages (flutter/flutter#195950)
2026-11-04 [email protected] Roll pub packages (flutter/flutter#195948)
2026-11-04 [email protected] local_engine_builds_common out of bringup (flutter/flutter#195862)
2026-11-04 [email protected] [web][a11y] Initialize slider semantics values when enabling (flutter/flutter#195564)
2026-11-04 [email protected] Drop Android SDK, JDK, and Gradle dependencies from framework_tests_misc (flutter/flutter#195869)
2026-11-04 [email protected] Roll Fuchsia Test Scripts from B6zarykZp6PKWRueg... to Jo-ZgQSNDuPsjlZ4s... (flutter/flutter#195937)
2026-11-04 [email protected] Balance tool_tests_commands and web_tool_tests subshards by duration (flutter/flutter#195863)
2026-11-04 [email protected] Roll Fuchsia Linux SDK from T_-6-6X1lDMghhAvX... to p0HDXLISPIElJfSz7... (flutter/flutter#195939)
2026-11-03 [email protected] Roll Skia from 3eeafac94f07 to 49ec2655bf51 (8 revisions) (flutter/flutter#195933)
2026-11-03 [email protected] Roll pub packages (flutter/flutter#195932)
2026-11-03 [email protected] [flutter_tools] Lazily initialize AppleContext dependencies via builder closures (flutter/flutter#191992)
2026-11-03 [email protected] [Android] Upgrade linux_android_emu_vulkan_stable avd_cipd_version to 36.1.9 (build_id:8670032186093418337) (flutter/flutter#195856)
2026-11-03 [email protected] [vulkan]: adds "color attachment read" dst dependency to subpass (flutter/flutter#195714)
2026-11-03 [email protected] [flutter_tools] Stream web dev server proxy responses instead of buffering them (flutter/flutter#194934)
2026-11-03 [email protected] Roll Skia from d80db8f8f856 to 3eeafac94f07 (6 revisions) (flutter/flutter#195926)
2026-11-03 [email protected] [flutter_tools] Add flutter assemble support for tool extension assemble targets (flutter/flutter#194563)
2026-11-03 [email protected] Manual roll Dart SDK from fe609902984b to e362a23e9738 (2 revisions) (flutter/flutter#195914)
2026-11-03 [email protected] feat(flutter_test): add startHover and startHoverAt helper to WidgetTester (flutter/flutter#190630)
2026-11-03 [email protected] [flutter_tools] Remove trailing dots from bug report URLs (flutter/flutter#194132)
2026-11-03 [email protected] Update skills_lint to 0.5.3 (flutter/flutter#195796)
2026-11-03 [email protected] Roll Skia from 58295d932350 to d80db8f8f856 (5 revisions) (flutter/flutter#195900)
2026-11-03 [email protected] [flutter_tools] Make DeviceLogReader.dispose asynchronous (flutter/flutter#195389)
2026-11-03 [email protected] Roll Packages from 3f34a6f to 1df8447 (10 revisions) (flutter/flutter#195895)
2026-11-03 [email protected] Roll Skia from c733ad3e37cb to 58295d932350 (1 revision) (flutter/flutter#195894)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
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.

2 participants