Skip to content

[flutter_tools] Fix Use dependency graph to determine plugin initialization order - #191591

Merged
auto-submit[bot] merged 11 commits into
masterfrom
investigation/issue-99153
Oct 1, 2026
Merged

auto-submit[bot] merged 11 commits into
masterfrom
investigation/issue-99153

Conversation

@bkonyi

@bkonyi bkonyi commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #99153

Summary

Flutter Issue Investigation: #99153

Issue: #99153 - Use dependency graph to determine plugin initialization order
Status: Resolved
Scope: packages/flutter_tools


1. Overview and Problem Statement

When Flutter generates native plugin registrants (such as GeneratedPluginRegistrant.java, GeneratedPluginRegistrant.m, generated_plugin_registrant.cc, web_plugin_registrant.dart) and plugin dependency files (.flutter-plugins-dependencies), plugins were previously sorted purely alphabetically by plugin name (left.name.compareTo(right.name)).

While alphabetical sorting ensured deterministic registrant generation, it did not take into account dependencies between plugins. If plugin_a depended on plugin_z, plugin_a would be registered and initialized before plugin_z, leading to potential initialization order bugs when a plugin depends on services or state initialized by another plugin.


2. Root Cause Analysis

  1. findPlugins & refreshPluginsList:
    • findPlugins collected plugins in arbitrary traversal order from computeTransitiveDependencies.
    • refreshPluginsList sorted plugins alphabetically (plugins.sort((Plugin left, Plugin right) => left.name.compareTo(right.name));).
  2. _resolvePluginImplementationsByPlatform:
    • Platform implementation candidate resolution re-sorted the platform-specific plugins alphabetically (pluginImplementations.sort((Plugin left, Plugin right) => left.name.compareTo(right.name));), overriding any topological order.
  3. _writeFlutterPluginsList and Registrant Generators:
    • Iterated over the sorted lists and wrote registrations directly to disk.

3. Solution & Architecture

Topological Sort (sortByDependencies)

Implemented sortByDependencies(Iterable<Plugin> plugins) in packages/flutter_tools/lib/src/flutter_plugins.dart:

  • Uses Kahn's algorithm for topological sorting:
    1. Computes in-degree for all plugins within the set of active plugins based on plugin.dependencies. Non-plugin dependencies (e.g., flutter, meta, external packages) are filtered out.
    2. Seeds available queue with plugins having 0 in-degree, sorted alphabetically.
    3. Dequeues plugins and decrements dependent plugins' in-degrees. When in-degree reaches 0, dependent plugins are added to the available queue (alphabetically sorted).
    4. If a dependency cycle occurs, the algorithm breaks cycles deterministically by selecting the unplaced plugin with the smallest in-degree, breaking ties alphabetically.
    5. For independent plugins with no dependency relationships, preserves alphabetical sorting.

Call Site Updates

  • Updated findPlugins to return sortByDependencies(plugins).
  • Updated _resolvePluginImplementationsByPlatform to sort platform implementations using sortByDependencies(pluginResolution.values).
  • Removed redundant plugins.sort in refreshPluginsList and packages/flutter_tools/lib/src/commands/build_swift_package.dart.

4. Automated Testing and Verification

Regression & Unit Tests Added

  • Refreshing the plugin list sorts plugins by dependency order in packages/flutter_tools/test/general.shard/plugins_test.dart.
  • group('sortByDependencies') in plugins_test.dart:
    • returns empty list when input is empty
    • returns single element list as-is
    • sorts independent plugins alphabetically
    • sorts direct dependency before dependent plugin
    • sorts multi-level linear dependency chain
    • sorts diamond dependency graph
    • ignores non-plugin dependencies
    • handles dependency cycle deterministically without throwing or hanging
  • Registrant Injection Tests:
    • Generated Linux plugin files sorts by dependency order
    • Generated Windows plugin files sorts by dependency order

Verification Results

  • dart format cleanly applied across all modified files.
  • dart analyze --fatal-infos packages/flutter_tools: Passed with 0 errors, 0 warnings, 0 infos.
  • dart test packages/flutter_tools/test/general.shard/plugins_test.dart: All 97 tests passing.

Verification

  • Regression test included and verified.

…on order (#99153)

## Description
Sorts plugins in dependency order using a topological sort (Kahn's algorithm)
so that dependent plugins are registered after their dependencies during plugin
registrant generation and `.flutter-plugins-dependencies` creation.

Previously, `findPlugins`, `refreshPluginsList`, `build_swift_package`, and
`_resolvePluginImplementationsByPlatform` sorted plugins alphabetically by name,
ignoring inter-plugin dependencies and causing dependent plugins to potentially
initialize before their dependencies.

## Related Issues
Fixes #99153

## Tests
- Added `Refreshing the plugin list sorts plugins by dependency order` in `packages/flutter_tools/test/general.shard/plugins_test.dart`.
- Added `group('sortByDependencies')` unit tests for topological sorting invariants and cycle handling.
- Added registrant injection dependency ordering tests for Linux and Windows in `plugins_test.dart`.
@bkonyi
bkonyi requested review from a team as code owners August 24, 2026 17:52
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 24, 2026
@github-actions github-actions Bot added tool Affects the "flutter" command-line tool. See also t: labels. team-ios Owned by iOS platform team team-macos Owned by the macOS platform team labels Aug 24, 2026
@bkonyi
bkonyi removed request for a team August 24, 2026 17:53

@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 replaces alphabetical plugin sorting with a dependency-based sorting mechanism (sortByDependencies) to ensure dependencies are initialized before dependent plugins. Comprehensive tests have been added to verify this behavior. Feedback suggests optimizing the topological sorting loop in sortByDependencies to prevent redundant additions during dependency cycles and to reduce sorting overhead.

Comment thread packages/flutter_tools/lib/src/flutter_plugins.dart
bkonyi added 3 commits August 24, 2026 18:01
Avoid redundant additions in dependency cycles and batch sort newly available dependents.
…al plugin ordering

Reflects that dependency plugins (plugin_b, plugin_c, plugin_d) are sorted before dependent plugin_a.
@github-actions github-actions Bot added the a: desktop Running on desktop label Sep 14, 2026

@vashworth vashworth 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.

LGTM

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 1, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Oct 1, 2026
Merged via the queue into master with commit 086a6c8 Oct 1, 2026
28 checks passed
@auto-submit
auto-submit Bot deleted the investigation/issue-99153 branch October 1, 2026 21:13
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 1, 2026
@cbracken

cbracken commented Oct 1, 2026

Copy link
Copy Markdown
Member

Reason for revert: broke Linux analyzer_benchmark and Linux analyze

@cbracken cbracken added the revert Autorevert PR (with "Reason for revert:" comment) label Oct 1, 2026
@flutteractionsbot

Copy link
Copy Markdown
Contributor

Successfully created revert PR: #193686

@flutteractionsbot flutteractionsbot removed the revert Autorevert PR (with "Reason for revert:" comment) label Oct 1, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Oct 2, 2026
flutter/flutter@e89fd0a...d03768e

2026-10-02 [email protected] Roll Skia from 7b7326917e77 to 9e88bf828078 (8 revisions) (flutter/flutter#193694)
2026-10-02 [email protected] [Widget Preview] Provide descriptive error when widget preview is unconstrained (flutter/flutter#193005)
2026-10-02 [email protected] Roll Dart SDK from 0e7642b85457 to ac1a97aae47d (26 revisions) (flutter/flutter#193692)
2026-10-02 [email protected] Remove unused Fuchsia sysmem header files (flutter/flutter#193244)
2026-10-02 [email protected] [Windows] Preserve composing extent in setEditingState (flutter/flutter#189968)
2026-10-02 [email protected] [docs] Make Border.symmetric docs more explicit about their arguments (flutter/flutter#193222)
2026-10-02 [email protected] [Embedder] Support render texture for vulkan (flutter/flutter#188855)
2026-10-02 [email protected] Updated Remaining Engine Defaults to SDK 37 (flutter/flutter#190429)
2026-10-02 [email protected] Document that enableSuggestions: false can disable keyboard languages on Android (flutter/flutter#192714)
2026-10-02 [email protected] [flutter_tools] Explicitly track host CPU architecture in command result analytics (flutter/flutter#191836)
2026-10-02 [email protected] [web] Preserve DOM focus on role update and honor isAccessibilityFocusBlocked (flutter/flutter#192963)
2026-10-02 [email protected] [flutter_tools] Include base href in web hot reload script paths (flutter/flutter#193678)
2026-10-02 [email protected] [Impeller] Deduplicate GLES render pass state (flutter/flutter#193427)
2026-10-02 [email protected] Roll Skia from f2d68e0b8863 to 7b7326917e77 (16 revisions) (flutter/flutter#193676)
2026-10-01 [email protected] Fix analysis failures due to missing `const` (flutter/flutter#193685)
2026-10-01 [email protected] Removes a11y_assessment app (flutter/flutter#193671)
2026-10-01 [email protected] test: configure Xvfb and openbox for windowing_test (flutter/flutter#193529)
2026-10-01 [email protected] Sync CHANGELOG.md from stable (flutter/flutter#193666)
2026-10-01 [email protected] Avoid using relative path in Process.start (flutter/flutter#193664)
2026-10-01 [email protected] [AGP 9.1.0 Migration #6] Deliver Flutter assets as a generated assets source directory on the app path (flutter/flutter#192488)
2026-10-01 [email protected] ci(bringup): android_java17_build_android_host_app_with_module_aar is green (flutter/flutter#193580)
2026-10-01 [email protected] [flutter_tools] Fix Use dependency graph to determine plugin initialization order (flutter/flutter#191591)
2026-10-01 [email protected] [flutter_tools] Migrate DaemonCommand and Daemon domains to constructor DI (flutter/flutter#193542)

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

a: desktop Running on desktop CICD Run CI/CD 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.

Use dependency graph to determine plugin initialization order

4 participants