Repository navigation
[stable] [flutter_tools] Fix StateError in testCompilerBuildNativeAssets when package root does not match projectUri - #194959
Conversation
…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.
|
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. |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
The Uri class in Dart does not have an isScheme method. This will cause a compilation error. Use uri.scheme == _fileScheme instead.
| 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)) { |
b3804a3
into
flutter:flutter-3.47-candidate.0
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.
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 intestCompilerBuildNativeAssetswhen runningflutter testin directories where the package root URI does not strictly matchprojectUriinpackage_config.json(such as in pub workspaces, monorepos, symlinked project directories, or when ancestorpackage_config.jsonfiles are used). This crash was observed in Flutter 3.47.3 crash telemetry and blocks affected users from runningflutter test.Changelog Description:
[flutter/192933] When running
flutter testin pub workspaces, monorepos, or symlinked directories, fix a StateError crash during native assets compilation by falling back to appName matching.Workaround:
Run
flutter testfrom the exact directory matching the package root or pass--no-native-assetswhen native assets are not required.Risk:
Test Coverage:
Are you confident that your fix is well-tested by automated tests?
Validation Steps:
Run
flutter testin a project with a symlinked path or inside a pub workspace member package and verify thattestCompilerBuildNativeAssetsexecutes cleanly without throwingStateError (Bad state: No element).Conflicts Resolved:
packages/flutter_tools/lib/src/isolated/native_assets/test/native_assets.dart: AdaptedtestCompilerBuildNativeAssetsto maintain theFuture<Uri?>return type and constructor signature present onflutter-3.47-candidate.0, returningnullon failure andnativeAssetsFileUrion success.packages/flutter_tools/test/general.shard/isolated/native_assets_test.dart: Updated regression tests to expectUri?rather than theTestCompilerNativeAssetsBuildResultrecord type introduced in later master commits.