Skip to content

[tool] Wire dependency injection into runner and executable with required non-nullable contexts - #190922

Merged
auto-submit[bot] merged 14 commits into
flutter:masterfrom
bkonyi:di/02-runner-bootstrap
Aug 19, 2026
Merged

auto-submit[bot] merged 14 commits into
flutter:masterfrom
bkonyi:di/02-runner-bootstrap

Conversation

@bkonyi

@bkonyi bkonyi commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 2 of the modular dependency injection migration (stacked on #190724).

Wires explicit dependency injection into the Flutter tool entrypoints:

  • Wires ToolDependencies.bootstrap into runner.dart to initialize dependencies at startup.
  • Updates FlutterCommandRunner to accept non-nullable toolContext, androidContext, appleContext, and toolDependencies.
  • Updates executable.dart to forward ToolDependencies into generateCommands.
  • Updates test_flutter_command_runner.dart and hermetic test doubles.

Part of #47161

@bkonyi
bkonyi requested review from a team as code owners August 11, 2026 14:53
@bkonyi
bkonyi requested review from mboetger and removed request for a team August 11, 2026 14:53
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 11, 2026
@github-actions github-actions Bot added tool Affects the "flutter" command-line tool. See also t: labels. team-android Owned by Android platform team team-ios Owned by iOS platform team team-macos Owned by the macOS platform team labels Aug 11, 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 dependency injection in flutter_tools by introducing AndroidContext, AppleContext, ToolContext, and ToolDependencies to manage and bootstrap tool dependencies instead of relying on global variables. Feedback on these changes highlights opportunities to improve testability and efficiency, such as using the shared finalShutdownHooks for the file system, utilizing finalSystemClock.now() for the terminal, preferring injected analytics over global analytics, avoiding a potential null pointer crash with DevtoolsLauncher.instance, and optimizing path traversal in _gatherProjectPaths by using fs.path.basename.

Comment thread packages/flutter_tools/lib/src/context/tool_dependencies.dart
Comment thread packages/flutter_tools/lib/src/context/tool_dependencies.dart
Comment thread packages/flutter_tools/lib/src/runner/flutter_command_runner.dart
Comment thread packages/flutter_tools/lib/src/runner/flutter_command_runner.dart Outdated
Comment thread packages/flutter_tools/lib/src/runner/flutter_command_runner.dart
Comment thread packages/flutter_tools/lib/src/runner/flutter_command_runner.dart Outdated
@bkonyi
bkonyi force-pushed the di/02-runner-bootstrap branch from cf41bc2 to cb1f566 Compare August 11, 2026 15:31
@bkonyi
bkonyi force-pushed the di/02-runner-bootstrap branch from cb1f566 to 5533bb8 Compare August 11, 2026 17:58
victorsanni pushed a commit to victorsanni/flutter that referenced this pull request Aug 12, 2026
flutter#190724)

## Summary

Part 1 of the modular dependency injection migration.

Defines the foundational modular dependency injection containers and
bootstrapper for `flutter_tools`:
1. **`ToolContext`**: Layer 1 OS wrappers (`HostEnvironment`) and Layer
2 SDK state (`ToolConfiguration`).
2. **Nullable Sub-contexts**: `AndroidContext` and `AppleContext` for
clean platform isolation.
3. **`ToolDependencies`**: Topologically instantiates and manages the
dependency graph at startup with explicit overrides and lazy closures.
4. **Hermetic Test Doubles**: Includes `dependency_injection_test.dart`
and fake context doubles.

Followed by Part 2 (flutter#190922) for runner wiring.

Part of flutter#47161
# Conflicts:
#	packages/flutter_tools/lib/src/context/tool_dependencies.dart
#	packages/flutter_tools/test/general.shard/context/dependency_injection_test.dart
@github-actions github-actions Bot removed the team-android Owned by Android platform team label Aug 12, 2026
@github-actions github-actions Bot added the platform-ios iOS applications specifically label Aug 13, 2026
@cbracken
cbracken requested a review from vashworth August 13, 2026 21:58
@bkonyi
bkonyi requested a review from chingjun August 18, 2026 20:10
chingjun
chingjun previously approved these changes Aug 18, 2026
@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 18, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Aug 18, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Aug 19, 2026
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Aug 19, 2026
# Conflicts:
#	packages/flutter_tools/lib/executable.dart
@bkonyi
bkonyi requested a review from chingjun August 19, 2026 01:14
@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 19, 2026
@jason-simmons

Copy link
Copy Markdown
Member

hey @bkonyi, this seemed to have regressed this benchmark about 1s.

This PR is constructing ToolDependencies using various objects from globals. These globals objects have lazy initializers, so before the introduction of ToolDependencies many commands avoided the initialization cost because they did not use a particular object.

In particular, AndroidStudio is part of ToolDependencies and is expensive to construct. Its constructor spawns various external tools to gather configuration data.

Tasks like macOS app compilation are seeing increased latency because they now create an AndroidStudio object even though it is never used.

@bkonyi

bkonyi commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

hey @bkonyi, this seemed to have regressed this benchmark about 1s.

This PR is constructing ToolDependencies using various objects from globals. These globals objects have lazy initializers, so before the introduction of ToolDependencies many commands avoided the initialization cost because they did not use a particular object.

In particular, AndroidStudio is part of ToolDependencies and is expensive to construct. Its constructor spawns various external tools to gather configuration data.

Tasks like macOS app compilation are seeing increased latency because they now create an AndroidStudio object even though it is never used.

Yikes, I'm surprised we're doing blocking IO in a constructor like that.

I'll send a couple of PRs out to lazily run the initialization when we actually go to access the state instead of when we create the object.

@bkonyi
bkonyi deleted the di/02-runner-bootstrap branch August 28, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD platform-ios iOS applications specifically team-ios Owned by iOS platform team team-macos Owned by the macOS platform team tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants