Skip to content

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

Merged
auto-submit[bot] merged 12 commits into
flutter:masterfrom
bkonyi:issue-192933
Oct 20, 2026
Merged

auto-submit[bot] merged 12 commits into
flutter:masterfrom
bkonyi:issue-192933

Conversation

@bkonyi

@bkonyi bkonyi commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

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 #192933

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

…package root does not match projectUri

Avoid throwing an unhandled StateError in testCompilerBuildNativeAssets when no package in buildInfo.packageConfig.packages has a root URI directly matching FlutterProject.current().directory.uri (such as in pub workspaces or symlinked directories).

Instead, attempt canonicalized path matching, then fall back to the target package name from the project manifest or packageConfig, and return null gracefully if no package can be resolved.

Fixes flutter#192933
@bkonyi bkonyi added the CICD Run CI/CD label Sep 17, 2026
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Sep 17, 2026
…sets

Add an optional FileSystem parameter to testCompilerBuildNativeAssets and TestCompilerNativeAssetsBuilderImpl to avoid repeated ambient globals.fs lookups. Decouple _findRunPackageName from FlutterProject by passing projectUri and manifestAppName directly, use fileSystem.path.fromUri instead of allocating a Directory, and add documentation comments.
…estCompilerBuildNativeAssets test and document fallback tests
@bkonyi
bkonyi requested a review from dcharkes October 16, 2026 15:39
@bkonyi
bkonyi marked this pull request as ready for review October 16, 2026 15:39

@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 introduces a robust fallback mechanism (findRunPackageName) for resolving the target package name during native assets builds for tests, handling direct URI matches, canonicalized paths (including symlinks), and project manifest app name fallbacks. It also adds comprehensive unit tests covering these resolution paths and error handling. Feedback suggests optimizing the path resolution logic by checking the manifestAppName first to avoid potentially slow synchronous I/O operations in a loop over all packages.

Comment on lines +174 to +182
final Package? pathMatch = packageConfig.packages.where((Package p) {
if (!p.root.isScheme(_fileScheme)) {
return false;
}
return _canonicalizeUriPath(fileSystem, p.root) == canonicalProjectDir;
}).firstOrNull;
if (pathMatch != null) {
return pathMatch.name;
}

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.

medium

To avoid performing synchronous I/O operations (existsSync and resolveSymbolicLinksSync) in a loop over all packages in packageConfig.packages (which can be slow in large projects), we can add a fast path that checks the package matching manifestAppName first. Since the root package name in package_config.json always matches the name in pubspec.yaml (manifestAppName), this will resolve the package directly in almost all cases without iterating through other packages.

    if (manifestAppName.isNotEmpty) {
      final Package? candidate = packageConfig[manifestAppName];
      if (candidate != null &&
          candidate.root.isScheme(_fileScheme) &&
          _canonicalizeUriPath(fileSystem, candidate.root) == canonicalProjectDir) {
        return candidate.name;
      }
    }
    final Package? pathMatch = packageConfig.packages.where((Package p) {
      if (!p.root.isScheme(_fileScheme)) {
        return false;
      }
      return _canonicalizeUriPath(fileSystem, p.root) == canonicalProjectDir;
    }).firstOrNull;
    if (pathMatch != null) {
      return pathMatch.name;
    }
References
  1. Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)

dcharkes
dcharkes previously approved these changes Oct 19, 2026

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

IIRC, there are more and older github issues that are related to tool crashes because there are entry points that are not part of the resolved package graph?

LGTM to a sane fallback behavior.

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 19, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 19, 2026
@auto-submit

auto-submit Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/192942, because - The status or check suite Dashboard Checks has failed. Please fix the issues identified (or deflake) before re-applying this label.

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 19, 2026
@bkonyi
bkonyi requested a review from dcharkes October 19, 2026 18:52
@bkonyi bkonyi added the t: cp-candidate A PR that should be considered for a cherry-pick upon landing. label Oct 20, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Oct 20, 2026
Merged via the queue into flutter:master with commit 6238260 Oct 20, 2026
28 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 20, 2026
@bkonyi bkonyi added cp: beta cherry pick this pull request to beta release candidate branch cp: stable cherry pick this pull request to stable release candidate branch labels Oct 21, 2026
@flutteractionsbot

Copy link
Copy Markdown
Contributor

Failed to create CP due to merge conflicts.
You will need to create the PR manually. See the cherrypick wiki for more info.

LongCatIsLooong pushed a commit to LongCatIsLooong/flutter that referenced this pull request Oct 22, 2026
…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

## Pre-launch Checklist

- [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.
suryaKommana2662 pushed a commit to suryaKommana2662/flutter that referenced this pull request Oct 29, 2026
…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

## Pre-launch Checklist

- [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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD cp: beta cherry pick this pull request to beta release candidate branch cp: stable cherry pick this pull request to stable release candidate branch t: cp-candidate A PR that should be considered for a cherry-pick upon landing. tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[tool_crash] StateError (Bad state: No element) in testCompilerBuildNativeAssets

3 participants