Skip to content

[stable] [flutter_tools] Fix StateError in testCompilerBuildNativeAssets when package root does not match projectUri - #194959

Merged
auto-submit[bot] merged 1 commit into
flutter:flutter-3.47-candidate.0from
bkonyi:cherry-pick-192942-to-stable
Oct 26, 2026
Merged

auto-submit[bot] merged 1 commit into
flutter:flutter-3.47-candidate.0from
bkonyi:cherry-pick-192942-to-stable

Conversation

@bkonyi

@bkonyi bkonyi commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is created by automatic cherry pick workflow
Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request.

Issue Link:

#192933

Impact Description:

Fixes a StateError (Bad state: No element) crash in testCompilerBuildNativeAssets when running flutter test in directories where the package root URI does not strictly match projectUri in package_config.json (such as in pub workspaces, monorepos, symlinked project directories, or when ancestor package_config.json files are used). This crash was observed in Flutter 3.47.3 crash telemetry and blocks affected users from running flutter test.

Changelog Description:

[flutter/192933] When running flutter test in pub workspaces, monorepos, or symlinked directories, fix a StateError crash during native assets compilation by falling back to appName matching.

Workaround:

Run flutter test from the exact directory matching the package root or pass --no-native-assets when native assets are not required.

Risk:

  • Low
  • Medium
  • High

Test Coverage:

Are you confident that your fix is well-tested by automated tests?

  • Yes
  • No

Validation Steps:

Run flutter test in a project with a symlinked path or inside a pub workspace member package and verify that testCompilerBuildNativeAssets executes cleanly without throwing StateError (Bad state: No element).

Conflicts Resolved:

  • packages/flutter_tools/lib/src/isolated/native_assets/test/native_assets.dart: Adapted testCompilerBuildNativeAssets to maintain the Future<Uri?> return type and constructor signature present on flutter-3.47-candidate.0, returning null on failure and nativeAssetsFileUri on success.
  • packages/flutter_tools/test/general.shard/isolated/native_assets_test.dart: Updated regression tests to expect Uri? rather than the TestCompilerNativeAssetsBuildResult record type introduced in later master commits.

…package root does not match projectUri (flutter#192942)

Fixes a `StateError (Bad state: No element)` crash in
`testCompilerBuildNativeAssets` when running `flutter test` in a
directory whose URI does not strictly match any package root in
`package_config.json` (such as when working through symlinked project
directories, monorepos/workspaces, or ancestor `package_config.json`
files).

Replaces the strict `firstWhere((Package p) => p.root == projectUri)`
lookup with `findRunPackageName`, which resolves the target package name
using the following fallback order:
1. Direct URI match against `projectUri`.
2. Canonicalized filesystem path match (resolving symlinks and trailing
slashes).
3. Matching the project's `pubspec.yaml` `appName` in `packageConfig`.
4. Returning `null` if no package can be resolved, logging a warning and
skipping native assets hooks gracefully instead of crashing.

Fixes flutter#192933

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
@bkonyi bkonyi added the cp: review Cherry-picks in the review queue label Oct 21, 2026
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Oct 21, 2026
@bkonyi bkonyi added the CICD Run CI/CD label Oct 21, 2026
@flutter-dashboard

Copy link
Copy Markdown

This pull request was opened from and to a release candidate branch. This should only be done as part of the official Flutter release process. If you are attempting to make a regular contribution to the Flutter project, please close this PR and follow the instructions at Tree Hygiene for detailed instructions on contributing to Flutter.

Reviewers: Use caution before merging pull requests to release branches. Ensure the proper procedure has been followed.

@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 implements a fallback mechanism for resolving the target package name during native assets builds via the new findRunPackageName function, along with corresponding unit tests. The review comments point out critical compilation errors where the non-existent Uri.isScheme method is used, and provide suggestions to use Uri.scheme instead.

}

// 2. Canonicalized path match (handles symlinks, trailing slashes, etc.).
if (projectUri.isScheme(_fileScheme) && packageConfig.packages.isNotEmpty) {

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.

critical

The Uri class in Dart does not have an isScheme method. This will cause a compilation error. Use uri.scheme == _fileScheme instead.

Suggested change
if (projectUri.isScheme(_fileScheme) && packageConfig.packages.isNotEmpty) {
if (projectUri.scheme == _fileScheme && packageConfig.packages.isNotEmpty) {

if (projectUri.isScheme(_fileScheme) && packageConfig.packages.isNotEmpty) {
final String canonicalProjectDir = _canonicalizeUriPath(fileSystem, projectUri);
final Package? pathMatch = packageConfig.packages.where((Package p) {
if (!p.root.isScheme(_fileScheme)) {

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.

critical

The Uri class in Dart does not have an isScheme method. This will cause a compilation error. Use p.root.scheme == _fileScheme instead.

Suggested change
if (!p.root.isScheme(_fileScheme)) {
if (p.root.scheme != _fileScheme) {

@reidbaker reidbaker added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 26, 2026
@auto-submit
auto-submit Bot merged commit b3804a3 into flutter:flutter-3.47-candidate.0 Oct 26, 2026
12 checks passed
auto-submit Bot pushed a commit that referenced this pull request Oct 29, 2026
Adds CHANGELOG.md entries for the 3.47.9 stable hotfix (#195073, #194959, #195015). CI-only cherry-picks are omitted.

Tracker: #195427

### Edits to cherry-pick changelog descriptions
- #195073 and #195015: used verbatim from the cherry-pick PR's "Changelog Description".
- #194959 (@bkonyi): **edited** to follow [Hotfix Documentation Best Practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) (describe the state prior to the fix):
  - Original: "When running `flutter test` in pub workspaces, monorepos, or symlinked directories, fix a StateError crash during native assets compilation by falling back to appName matching."
  - Changelog: "When running `flutter test` in pub workspaces, monorepos, or symlinked directories on any platform, native assets compilation crashed with a `StateError`."
  - Changes: rewrote as the pre-fix behavior, added "on any platform", dropped the implementation detail ("falling back to appName matching").

@bkonyi FYI — please let us know if the edited wording for #194959 looks right.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD cp: review Cherry-picks in the review queue 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