|
| 1 | +# Agent Guide for the Flutter Packages Repository |
| 2 | + |
| 3 | +This document provides guidance for AI agents to effectively contribute to the `flutter/packages` repository. |
| 4 | + |
| 5 | +## Guiding Principles for Contributions |
| 6 | + |
| 7 | +- **Format All Code**: Every code change must be formatted using the repository's tools. |
| 8 | +- **Pass All Tests**: All changes must pass linting, analysis, and relevant tests. |
| 9 | +- **Update CHANGELOGs**: Any user-facing change or bug fix in a package requires an update to its `CHANGELOG.md` and `pubspec.yaml` version. |
| 10 | +- **Follow Conventions**: Adhere to the repository's specific conventions, such as federated plugin structure and code generation steps. |
| 11 | + |
| 12 | +## Agent Environment Setup |
| 13 | + |
| 14 | +To ensure a consistent and functional environment, configure your VM with the following setup. This provides the necessary Flutter SDK and dependencies for building and testing packages. |
| 15 | + |
| 16 | +```bash |
| 17 | +curl -L https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_3.32.8-stable.tar.xz | tar -xJ -C $HOME |
| 18 | +export FLUTTER_HOME=$HOME/flutter |
| 19 | +export PATH=$FLUTTER_HOME/bin:$PATH |
| 20 | +flutter --disable-analytics |
| 21 | +flutter precache --force |
| 22 | +# Sanity check the configuration. |
| 23 | +flutter doctor --verbose |
| 24 | +``` |
| 25 | + |
| 26 | +## Repository Overview |
| 27 | + |
| 28 | +This is a monorepo containing many Flutter packages. |
| 29 | +- First-party packages developed entirely by the Flutter team are in `packages/`. |
| 30 | +- Packages that were originally developed by a third party, but are now maintained by the Flutter team are in `third_party/packages/`. |
| 31 | +- The repository tooling is in `script/tool/`. |
| 32 | + |
| 33 | +Many packages are part of **federated plugins**. A federated plugin has a main package (e.g., `path_provider`) that defines the API used by plugin clients, a platform interface package (e.g., `path_provider_platform_interface`) that defines the interface that each platform implementation must implement, and one or more platform implementation packages (e.g., `path_provider_android`, `path_provider_ios`) that implement that platform interface. When working on a federated plugin, you may need to modify multiple packages. |
| 34 | + |
| 35 | +For more details, see the main `README.md` and `CONTRIBUTING.md`. |
| 36 | + |
| 37 | +## Core Tooling and Workflows |
| 38 | + |
| 39 | +The primary tool for this repository is `flutter_plugin_tools.dart`. |
| 40 | + |
| 41 | +### Initial Setup |
| 42 | + |
| 43 | +First, initialize the tooling: |
| 44 | +```bash |
| 45 | +cd $REPO_ROOT/script/tool # $REPO_ROOT is the repository root |
| 46 | +dart pub get |
| 47 | +``` |
| 48 | + |
| 49 | +### Identifying Target Packages |
| 50 | + |
| 51 | +Most tool commands take a `--packages` argument. You must correctly identify all packages affected by your changes. You can derive this from git diff. |
| 52 | + |
| 53 | +For example, to find changed files against the main branch of the upstream remote (assuming the upstream remote is named `origin`): |
| 54 | + |
| 55 | +```bash |
| 56 | +git diff --name-only origin/main...HEAD |
| 57 | +``` |
| 58 | + |
| 59 | +Then, for each file path, find its enclosing package. A package is a directory containing a `pubspec.yaml` file. The directory name is usually the package name. Ignore `pubspec.yaml` files within `example/` directories when determining the package for a file. |
| 60 | + |
| 61 | +#### Targeting All Packages |
| 62 | + |
| 63 | +Running a tool command without a `--packages` argument will run the command on all packages. For example, a dependency can be updated for all packages in the repository: |
| 64 | + |
| 65 | +```bash |
| 66 | +dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart update-dependency --pub-package <dependency_name> |
| 67 | +``` |
| 68 | + |
| 69 | +### Common Commands |
| 70 | + |
| 71 | +- **Formatting**: Always format your changes. |
| 72 | + |
| 73 | + ```bash |
| 74 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart format --packages <changed_packages> |
| 75 | + ``` |
| 76 | +- **Testing**: All changes must pass analysis and tests: |
| 77 | + |
| 78 | + ```bash |
| 79 | + # Run static analysis |
| 80 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart analyze --packages <changed_packages> |
| 81 | + # Run Dart unit tests |
| 82 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart dart-test --packages <changed_packages> |
| 83 | + ``` |
| 84 | + |
| 85 | + The tool can also run native and integration tests, but these may require a more complete environment than is available. |
| 86 | +- **Validation**: Run these checks to ensure that changes follow team guidelines: |
| 87 | + ```bash |
| 88 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart publish-check --packages <changed_packages> |
| 89 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart readme-check --packages <changed_packages> |
| 90 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart version-check --packages <changed_packages> |
| 91 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart license-check |
| 92 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart repo-package-info-check |
| 93 | + ``` |
| 94 | + |
| 95 | +### Specialized Workflows |
| 96 | + |
| 97 | +- **Federated Plugin Development**: If you change multiple packages in a federated plugin that depend on each other, use `make-deps-path-based` to make their pubspec.yaml files use `path:` dependencies. This allows you to test them together locally. |
| 98 | + ```bash |
| 99 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart make-deps-path-based --target-dependencies=<changed_plugin_packages> |
| 100 | + ``` |
| 101 | + |
| 102 | + The CI system will run tests with path-based dependencies automatically, so this is not required for PRs, but can be useful for local testing. |
| 103 | +- **Updating Dependencies**: To update a dependency across multiple packages: |
| 104 | + ```bash |
| 105 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart update-dependency --pub-package <dependency_name> --packages <packages_to_update> |
| 106 | + ``` |
| 107 | +- **Updating README Code Samples**: If you change example code that is included in a README.md: |
| 108 | + ```bash |
| 109 | + dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart update-excerpts --packages <changed_packages> |
| 110 | + ``` |
| 111 | + |
| 112 | +## Code Generators |
| 113 | + |
| 114 | +Some packages use code generators, and changes to those packages require running the relevant code generators. |
| 115 | + |
| 116 | +- **Pigeon**: If you change a file in a `pigeons/` directory, you must run the Pigeon generator: |
| 117 | + ```bash |
| 118 | + # Run from the package's directory |
| 119 | + dart run pigeon --input pigeons/<changed_file>.dart |
| 120 | + ``` |
| 121 | +- **Mockito**: If you change code in a package that uses `mockito` for tests (check `dev_dependencies` in `pubspec.yaml`), you must run its mock generator: |
| 122 | + ```bash |
| 123 | + # Run from the package's directory |
| 124 | + dart run build_runner build -d |
| 125 | + ``` |
| 126 | + |
| 127 | +## Code Style |
| 128 | + |
| 129 | +All code must adhere to the repository's style guides. The `format` command handles most of this, but be aware of the specific style guides for each language, as detailed in [CONTRIBUTING.md](./CONTRIBUTING.md#style): |
| 130 | +- **Dart**: Flutter style, formatted with `dart format`. |
| 131 | +- **C++**: Google style, formatted with `clang-format`. |
| 132 | +- **Java**: Google style, formatted with `google-java-format`. |
| 133 | +- **Kotlin**: Android Kotlin style, formatted with `ktfmt`. |
| 134 | +- **Objective-C**: Google style, formatted with `clang-format`. |
| 135 | +- **Swift**: Google style, formatted with `swift-format`. |
| 136 | + |
| 137 | +## Version and CHANGELOG updates |
| 138 | + |
| 139 | +Any PR that changes non-test code in a package should update its version in pubspec.yaml and add a corresponding entry in CHANGELOG.md. |
| 140 | + |
| 141 | +**This process can be automated**. The `update-release-info` command is the preferred way to handle this. It determines changed packages, bumps versions, and updates changelogs automatically. |
| 142 | +```bash |
| 143 | +dart run $REPO_ROOT/script/tool/bin/flutter_plugin_tools.dart update-release-info \ |
| 144 | + --version=minimal \ |
| 145 | + --base-branch=origin/main \ |
| 146 | + --changelog="A description of the changes." |
| 147 | +``` |
| 148 | + |
| 149 | +- `--version=minimal`: Bumps patch for bug fixes, and skips unchanged packages. This is usually the best option unless a new feature is being added. |
| 150 | + - When making public API changes, use `--version=minor` instead. |
| 151 | +- `--base-branch=origin/main`: Diffs against the `main` branch to find changed packages. |
| 152 | + |
| 153 | +If you update manually, follow semantic versioning and the repository's CHANGELOG format. |
0 commit comments