Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add experimental data assets from hooks #164094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 0 commits into from

Conversation

mosuem
Copy link
Contributor

@mosuem mosuem commented Feb 25, 2025

Rebase of #159675

This PR adds bundling support for the experimental dart data asset feature: Dart packages with hooks can now emit data assets which the flutter tool will bundle.

It relies on flutter's existing asset bundling mechanism (e.g. entries in AssetManifest.json, DevFS syncing in reload/restart, ...).

The support is added under an experimental flag (similar to the existing native assets experimental flag).

Also, kNativeAssets is removed to also bundle data assets on flutter build bundle.

The chrome sandbox is disabled as per #165664.

cc @bkonyi

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added tool Affects the "flutter" command-line tool. See also t: labels. team-ios Owned by iOS platform team labels Feb 25, 2025
@mosuem mosuem force-pushed the rebaseMartinsDataAssets branch from 5e36ce4 to 9a2f7e6 Compare February 25, 2025 17:19
@mosuem mosuem marked this pull request as ready for review February 27, 2025 14:40
@mosuem mosuem changed the title Add experimental data assets Add experimental data assets from hooks Feb 27, 2025
@mosuem
Copy link
Contributor Author

mosuem commented Feb 28, 2025

There seems to be a test failure related to the ordering of stdouts at https://github.com/mosuem/flutter/blob/rebaseMartinsDataAssets/packages/flutter_tools/test/integration.shard/overall_experience_test.dart#L123 - not sure if this is intentional or not.

@mosuem mosuem force-pushed the rebaseMartinsDataAssets branch 2 times, most recently from 840b811 to f71c5e9 Compare March 4, 2025 08:39
@github-actions github-actions bot added the a: desktop Running on desktop label Mar 4, 2025
@mkustermann
Copy link
Member

mkustermann commented Mar 5, 2025

@mosuem You should be able to revert the changes to packages/flutter_tools/test/integration.shard/transition_test_utils.dart: I've pulled the refactoring of the test framework into a different CL that already landed on the main branch (the change landed as a44f745)

@mkustermann
Copy link
Member

There seems to be a test failure related to the ordering of stdouts at https://github.com/mosuem/flutter/blob/rebaseMartinsDataAssets/packages/flutter_tools/test/integration.shard/overall_experience_test.dart#L123 - not sure if this is intentional or not.

The ordering of the patterns in

          Multiple(
            <Pattern>[
              RegExp(
                r'^Reloaded 0 libraries in [0-9]+ms \(compile: \d+ ms, reload: \d+ ms, reassemble: \d+ ms\)\.$',
              ),
              'called reassemble',
              'called paint',
            ],
            handler: (String line) {
              processManager.killPid(pid, ProcessSignal.sigusr2);
              return null;
            },
          ),

is not relevant. You can see how Multiple is implemented. It just waits for all 3 patterns to occur (in any order) and only procceedes once all patterns have been found. So if it hangs in this step, it's a sign that one of the patterns wasn't found.

@mkustermann
Copy link
Member

There seems to be a test failure related to the ordering of stdouts at https://github.com/mosuem/flutter/blob/rebaseMartinsDataAssets/packages/flutter_tools/test/integration.shard/overall_experience_test.dart#L123 - not sure if this is intentional or not.

The root cause I believe is the modified code (which should be reverted - as mentioned earlier):

diff --git a/packages/flutter_tools/test/integration.shard/transition_test_utils.dart b/packages/flutter_tools/test/integration.shard/transition_test_utils.dart
index 73f219ac83..39210532a4 100644
--- a/packages/flutter_tools/test/integration.shard/transition_test_utils.dart
+++ b/packages/flutter_tools/test/integration.shard/transition_test_utils.dart
   @protected
-  bool lineMatchesPattern(String line, Pattern pattern, bool contains) {
-    if (pattern is RegExp) {
-      // Ideally this would also distinguish between "contains" and "equals"
-      // operation.
-      return line.contains(pattern);
+  bool lineMatchesPattern(String line, Pattern pattern) {
+    if (pattern is String) {
+      return contains ? line.contains(pattern) : line == pattern;
     }
     return contains ? line.contains(pattern) : line == pattern; // XXX
   }

This XXX is incorrect. The line == pattern will never work because line is a String and pattern is a RegExp.

@mosuem mosuem force-pushed the rebaseMartinsDataAssets branch from 7721ce5 to 5d27153 Compare March 18, 2025 17:52
@dcharkes dcharkes self-requested a review March 19, 2025 14:15
@mosuem mosuem force-pushed the rebaseMartinsDataAssets branch 4 times, most recently from 20e240f to 741ef51 Compare March 28, 2025 14:03
@mosuem mosuem requested a review from bkonyi March 28, 2025 14:35
@mosuem mosuem requested a review from a team as a code owner April 1, 2025 12:25
@mosuem mosuem force-pushed the rebaseMartinsDataAssets branch 3 times, most recently from 8e0d321 to 8e028e2 Compare April 2, 2025 12:10
@mosuem
Copy link
Contributor Author

mosuem commented Apr 2, 2025

@bkonyi I seem to be stuck debugging this. On windows,

flutter test test\integration.shard\isolated\dart_data_asset_test.dart --name "dart data assets flutter run on flutter-tester --debug"

fails - debugging prints show Could not update asset directory. Where do I go from there? This is Windows and flutter-tester specific, so it probably isn't the Dart code, right?

The other error also puzzles me - on macos,

flutter test test\integration.shard\isolated\dart_data_asset_test.dart --name "dart data assets flutter run on chrome --debug"

fails - but only on CI, not reproducible locally on my mac. It seems to be some sort of Chrome crash, maybe a Chrome version thing?

Also cc @mkustermann if you have any ideas..

github-merge-queue bot pushed a commit that referenced this pull request Apr 3, 2025
Updating the dart-lang/native dependencies to the ones published today.

No functional changes, but `CodeAsset` does not expose an `architecture`
and `os ` anymore (dart-lang/native#2127).
Instead these should be taken from what is passed in for the
`CodeConfig`. This PR refactors the `DartBuildResult` to carry around
the `Target` with `CodeAsset`s as `FlutterCodeAsset`s.

(This PR avoid refactoring relevant code due to
#164094 already refactoring this
code.)
@bkonyi
Copy link
Contributor

bkonyi commented Apr 4, 2025

@bkonyi I seem to be stuck debugging this. On windows,

flutter test test\integration.shard\isolated\dart_data_asset_test.dart --name "dart data assets flutter run on flutter-tester --debug"

fails - debugging prints show Could not update asset directory. Where do I go from there? This is Windows and flutter-tester specific, so it probably isn't the Dart code, right?

It looks like the VmService instance is throwing an RPCError here but we don't actually log the error. Can you try adding some logging of the RPCError there and verify what kind of exception we're seeing, along with the associated stack trace? If I had to guess, the VM service is shutting down for some reason in the middle of the request.

The other error also puzzles me - on macos,

flutter test test\integration.shard\isolated\dart_data_asset_test.dart --name "dart data assets flutter run on chrome --debug"

fails - but only on CI, not reproducible locally on my mac. It seems to be some sort of Chrome crash, maybe a Chrome version thing?

Yeah, I have no idea what's up for this one but it's definitely related to the Chrome crash. If could be an infra thing? cc @matanlurey @chingjun

@mosuem
Copy link
Contributor Author

mosuem commented Apr 4, 2025

Can you try adding some logging of the RPCError there and verify what kind of exception we're seeing, along with the associated stack trace?

That's what I did earlier, where the Could not update asset directory comes from. The stack trace is

#0      HotRunner._hotReloadHelper (package:flutter_tools/src/run_hot.dart:967:81)
<asynchronous suspension>
#1      HotRunner.restart (package:flutter_tools/src/run_hot.dart:825:36)
<asynchronous suspension>
#2      TerminalHandler._commonTerminalInputHandler (package:flutter_tools/src/resident_runner.dart:1717:40)
<asynchronous suspension>
#3      TerminalHandler.processTerminalInput (package:flutter_tools/src/resident_runner.dart:1772:7)
<asynchronous suspension>

@bkonyi
Copy link
Contributor

bkonyi commented Apr 4, 2025

Can you try adding some logging of the RPCError there and verify what kind of exception we're seeing, along with the associated stack trace?

That's what I did earlier, where the Could not update asset directory comes from. The stack trace is

#0      HotRunner._hotReloadHelper (package:flutter_tools/src/run_hot.dart:967:81)
<asynchronous suspension>
#1      HotRunner.restart (package:flutter_tools/src/run_hot.dart:825:36)
<asynchronous suspension>
#2      TerminalHandler._commonTerminalInputHandler (package:flutter_tools/src/resident_runner.dart:1717:40)
<asynchronous suspension>
#3      TerminalHandler.processTerminalInput (package:flutter_tools/src/resident_runner.dart:1772:7)
<asynchronous suspension>

Sorry, must have missed that.

That error is coming from the engine and can be reported when:

  • The asset directory path is invalid or
  • The new AssetManager instance is identical to the old one or is null

I'm guessing it's likely the first case. Since we provide the same path to runInView after a hot restart, I took a closer look at the logic for both runInView and setAssetBundlePath in the engine. I noticed that we're doing something slightly different with the asset bundle paths in each situation:

  // Shell::OnServiceProtocolRunInView
  std::string asset_directory_path =
      fml::paths::FromURI(params.at("assetDirectory").data());

  // ...

  configuration.AddAssetResolver(std::make_unique<DirectoryAssetBundle>(
      fml::OpenDirectory(asset_directory_path.c_str(), false,
                         fml::FilePermission::kRead),
      false));

vs

  // Shell::OnServiceProtocolSetAssetBundlePath
  if (!asset_manager->PushFront(std::make_unique<DirectoryAssetBundle>(
          fml::OpenDirectory(params.at("assetDirectory").data(), false,
                             fml::FilePermission::kRead),
          false))) {
    // The new asset directory path was invalid.
    FML_DLOG(ERROR) << "Could not update asset directory.";
    ServiceProtocolFailureError(response, "Could not update asset directory.");
    return false;
  }

In Shell::OnServiceProtocolRunInView, we're processing the assetDirectory parameter with FromURI(...), which involves changing the path separators to \ on Windows. We're not doing this for the SetAssetBundlePath, so I'm assuming that we're failing to open the directory due to a path separator issue.

Can you try patching flutter/engine/src/flutter/shell/common/shell.cc with the following and see if it fixes the issue?

  // Shell::OnServiceProtocolSetAssetBundlePath
  std::string asset_directory_path =
      fml::paths::FromURI(params.at("assetDirectory").data());
  if (!asset_manager->PushFront(std::make_unique<DirectoryAssetBundle>(
          fml::OpenDirectory(asset_directory_path.c_str(), false,
                             fml::FilePermission::kRead),
          false))) {
    // The new asset directory path was invalid.
    FML_DLOG(ERROR) << "Could not update asset directory.";
    ServiceProtocolFailureError(response, "Could not update asset directory.");
    return false;
  }

@github-actions github-actions bot added the engine flutter/engine repository. See also e: labels. label Apr 4, 2025
@mosuem
Copy link
Contributor Author

mosuem commented Apr 7, 2025

Can you try patching flutter/engine/src/flutter/shell/common/shell.cc with the following and see if it fixes the issue?

It seems it did not - the error message also stays the same.

@bkonyi
Copy link
Contributor

bkonyi commented Apr 7, 2025

Can you try patching flutter/engine/src/flutter/shell/common/shell.cc with the following and see if it fixes the issue?

It seems it did not - the error message also stays the same.

Hm, that's interesting... I think we'll need to add some logging, because this is likely something related to Windows paths. Can you add logging for the values of params.at("assetDirectory").data() and asset_directory_path.c_str() in the engine, and deviceAssetsDirectoryUri in evictDirtyAssets() in HotRunner?

@mosuem
Copy link
Contributor Author

mosuem commented Apr 11, 2025

@bkonyi added the debug statements - feel free to add more if it helps!

@mosuem mosuem force-pushed the rebaseMartinsDataAssets branch from 752eb87 to d28cf11 Compare April 16, 2025 16:56
@github-actions github-actions bot removed the engine flutter/engine repository. See also e: labels. label Apr 16, 2025
@mosuem
Copy link
Contributor Author

mosuem commented Apr 16, 2025

@matanlurey @jtmcdole any idea why the chrome tests on mac are failing?

@mosuem mosuem force-pushed the rebaseMartinsDataAssets branch from d28cf11 to 3d6ab44 Compare April 17, 2025 11:30
@mosuem
Copy link
Contributor Author

mosuem commented Apr 22, 2025

It seems the Chrome failure as reached linux as well, also not reproducible locally.

@mosuem mosuem force-pushed the rebaseMartinsDataAssets branch from abd0f6e to f51073b Compare April 25, 2025 14:46
throw MissingDefineException(kAndroidArchs, 'native_assets');
}
return androidArchsEnvironment.split(' ').map(getAndroidArchForName).toList();
case TargetPlatform.darwin:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove non-Android targets and use default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flutter-dashboard
Copy link

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@mosuem mosuem closed this May 22, 2025
@mosuem mosuem force-pushed the rebaseMartinsDataAssets branch from 2d20ea9 to 61fe9b6 Compare May 22, 2025 14:05
@mosuem mosuem mentioned this pull request May 22, 2025
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: desktop Running on desktop team-ios Owned by iOS 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.

4 participants