Skip to content
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

[native_assets] Add support for link hooks #148474

Merged
merged 13 commits into from
May 22, 2024
Merged

[native_assets] Add support for link hooks #148474

merged 13 commits into from
May 22, 2024

Conversation

dcharkes
Copy link
Contributor

@dcharkes dcharkes commented May 16, 2024

This PR adds support invoking link.dart hooks.

Link hooks can add new assets. Link hooks can transform assets sent to link hook from build hooks.

This PR does not yet add support for getting tree-shake information in the link hooks. This is pending on defining the resources.json format (dart-lang/sdk#55494).

Issue:

Implementation considerations

The build hooks could be run before Dart compilation and the link hooks after Dart compilation. (This is how it's done in Dart standalone.) However, due to the way the Targets are set up, this would require two targets and serializing and deserializing the BuildResult in between these. This would lead to more code but no benefits. Currently there is nothing that mandates running build hooks before Dart compilation.

Testing

  • The unit tests verify that the native_assets_builder link and linkDryRun would be invoked with help of the existing fake.
  • The native assets integration test now also invokes an FFI call of a package that adds the asset during the link hook instead of the build hook.
    • In order to keep coverage of the flutter create --template=package_ffi, flutter create is still run and the extra dependency is added and an extra ffi call is added. (Open to alternative suggestions.)

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. a: desktop Running on desktop labels May 16, 2024
@dcharkes
Copy link
Contributor Author

@christopherfujino Is there a way to allow arbitrary git dependencies on the CI here?
(If I make a PR upstream to support a PR here, having to merge and publish that upstream dependency before being able to run the CI here means its likely something will be forgotten upstream and we end up having to do more than one PR upstream.)

@christopherfujino
Copy link
Member

@christopherfujino Is there a way to allow arbitrary git dependencies on the CI here? (If I make a PR upstream to support a PR here, having to merge and publish that upstream dependency before being able to run the CI here means its likely something will be forgotten upstream and we end up having to do more than one PR upstream.)

You mean, you want to run presubmit tests against the version of a pub package on some random branch on the internet? You should be able to do dependency_overrides with a git dependency in a pubspec.yaml. You may get an analyzer error, but I would expect everything else to Just Work:tm:

@dcharkes
Copy link
Contributor Author

@christopherfujino Is there a way to allow arbitrary git dependencies on the CI here? (If I make a PR upstream to support a PR here, having to merge and publish that upstream dependency before being able to run the CI here means its likely something will be forgotten upstream and we end up having to do more than one PR upstream.)

You mean, you want to run presubmit tests against the version of a pub package on some random branch on the internet? You should be able to do dependency_overrides with a git dependency in a pubspec.yaml. You may get an analyzer error, but I would expect everything else to Just Work:tm:

👍
I also had to make some hacks in packages/flutter_tools/lib/src/commands/update_packages.dart.
And I had to use the googlesource mirrors instead of github. (We have some allowed hosts list somewhere in the infra but I couldn't find out where.)

Anyway, the CI then looked green enough to hit the publish button on the upstream changes, so the PR now only uses published deps.

@christopherfujino
Copy link
Member

@christopherfujino Is there a way to allow arbitrary git dependencies on the CI here? (If I make a PR upstream to support a PR here, having to merge and publish that upstream dependency before being able to run the CI here means its likely something will be forgotten upstream and we end up having to do more than one PR upstream.)

You mean, you want to run presubmit tests against the version of a pub package on some random branch on the internet? You should be able to do dependency_overrides with a git dependency in a pubspec.yaml. You may get an analyzer error, but I would expect everything else to Just Work:tm:

👍 I also had to make some hacks in packages/flutter_tools/lib/src/commands/update_packages.dart. And I had to use the googlesource mirrors instead of github. (We have some allowed hosts list somewhere in the infra but I couldn't find out where.)

Anyway, the CI then looked green enough to hit the publish button on the upstream changes, so the PR now only uses published deps.

🤌

@dcharkes
Copy link
Contributor Author

All green, ready for review @christopherfujino.
lmk if you want to chat about it.

@christopherfujino
Copy link
Member

cc @andrewkolos

@christopherfujino
Copy link
Member

Sorry for the delay @dcharkes, I forgot about this. I will take a look today, and if I have outstanding questions I will schedule a 1:1.

ensureNativeAssetsBuildSucceed(dryRunResult);
final List<AssetImpl> nativeAssets = dryRunResult.assets;
ensureNativeAssetsBuildDryRunSucceed(buildDryRunResult);
final LinkDryRunResult linkDryRunResult = await buildRunner.linkDryRun(
Copy link
Member

Choose a reason for hiding this comment

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

why do we need to dry run the link phase?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

@christopherfujino christopherfujino left a comment

Choose a reason for hiding this comment

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

Overall LGTM. I would appreciate a refresher on why the dry runs are needed in general, and specifically for the link phase.

@dcharkes
Copy link
Contributor Author

Overall LGTM. I would appreciate a refresher on why the dry runs are needed in general, and specifically for the link phase.

Why dry run in flutter run?

  • flutter run compiles native assets in dry run has the kernel file with a native assets mapping sitting around unused until the first hot restart. (The native assets mapping is coming from a dry run, it doesn't build the actual assets.)
    which invokes
    • xcodebuild which invokes
      • flutter assemble which compiles Dart to kernel with a native assets mapping and pushes it to the device. This kernel file with native assets mapping is used for hot reloads until the first hot restart. (This native assets mapping is coming from a normal run and will build the native assets.)

Because we want to support building a flutter app from the xcode project, the native assets mapping is needed in flutter assemble.
Because we want to support hot restart, the native assets mapping is also needed in flutter run.

The reason that flutter run does not simply do a native assets build for all target architectures of the current OS is that we would have to block until those builds are done before invoking xcodebuild. Since build hooks and link hooks cannot be invoked for the same configuration concurrently. Dry run is a different configuration then wet run, so in the current set up the dry-runs in flutter run are fired off concurrently to the xcodebuild process call. The fact that we do a kernel compilation in flutter run eagerly instead of on the first hot restart or hot reload request is to make the first hot restart or hot reload fast.

Why run link hooks in dry run?

The build hooks can produce temporary assets (for example a static library) that are linked in a link hook (for example a dynamic library). So in order to embed the correct native assets mapping, we need the output from both hooks. (In dry run both hooks are not expected to actually produce any files, just the information about which files will be produced in a non-dry-run.)

@dcharkes dcharkes added the autosubmit Merge PR when tree becomes green via auto submit App label May 22, 2024
@auto-submit auto-submit bot merged commit 1f16d91 into master May 22, 2024
142 checks passed
@auto-submit auto-submit bot deleted the link-hook-support branch May 22, 2024 16:02
@dcharkes
Copy link
Contributor Author

Thanks @christopherfujino!

hello-coder-xu added a commit to hello-coder-xu/flutter that referenced this pull request May 23, 2024
…ps://github.com/hello-coder-xu/flutter into fix/_floatingActionButtonVisibilityValue-update

* 'fix/_floatingActionButtonVisibilityValue-update' of https://github.com/hello-coder-xu/flutter:
  fix: update _floatingActionButtonVisibility only if floatingActionButton is not null
  [wiki migration] Remaining pages under docs/about/ (flutter#148782)
  Roll Flutter Engine from b6971cdf14f8 to 8b094fbb94d8 (3 revisions) (flutter#148883)
  Fix the second TextFormField to trigger onTapOutside (flutter#148206)
  Try removing robolectric from `integration_test` tests (flutter#148803)
  Prevent test folder deletion on running `flutter create --empty` on an existing app project (flutter#147160)
  [wiki migration] Tool team pages (flutter#148779)
  Roll Flutter Engine from c89defa55801 to b6971cdf14f8 (6 revisions) (flutter#148819)
  [native_assets] Add support for link hooks (flutter#148474)
  Roll Packages from ba19b247d0ba to 65254411e677 (12 revisions) (flutter#148864)
  Update tokens to 4.0.0 (flutter#148789)
  Move Linux web_long_running_tests_2_5 to bringup (flutter#148854)
  `CupertinoDialogRoute` leak fix (flutter#148774)
  Marks Windows plugin_test to be flaky (flutter#148835)
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 23, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 23, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 23, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this pull request May 23, 2024
flutter/flutter@73bf206...8d955cd

2024-05-23 nate.w5687@gmail.com Update `FocusManager` platform check to include iOS (flutter/flutter#148612)
2024-05-23 matej.knopp@gmail.com [iOS] fix hot restart with native assets (flutter/flutter#148752)
2024-05-23 engine-flutter-autoroll@skia.org Roll Flutter Engine from b8b82454e302 to 964f087f288c (8 revisions) (flutter/flutter#148943)
2024-05-23 104349824+huycozy@users.noreply.github.com Fix DecoratedSliver sample code to reflect the description (flutter/flutter#148621)
2024-05-23 82763757+NobodyForNothing@users.noreply.github.com Test raw autocomplete api examples (flutter/flutter#148234)
2024-05-23 32538273+ValentinVignal@users.noreply.github.com Add test for scaffold.0.dart and scaffold.2.dart (flutter/flutter#148166)
2024-05-23 sokolovskyi.konstantin@gmail.com Add tests for restorable_value.0.dart API example. (flutter/flutter#148676)
2024-05-23 engine-flutter-autoroll@skia.org Roll Flutter Engine from 8b094fbb94d8 to b8b82454e302 (6 revisions) (flutter/flutter#148919)
2024-05-22 31859944+LongCatIsLooong@users.noreply.github.com Allow `RenderObject.getTransformTo` to take an arbitrary RenderObject in the same tree (flutter/flutter#148897)
2024-05-22 kevinjchisholm@google.com 3.22.1 changelog updates (flutter/flutter#148895)
2024-05-22 helinx@google.com Add frame number and widget location map service extension (flutter/flutter#148702)
2024-05-22 31859944+LongCatIsLooong@users.noreply.github.com Remove an assert with false positives (flutter/flutter#148795)
2024-05-22 rmolivares@renzo-olivares.dev Revert "Fix the second TextFormField to trigger onTapOutside" (flutter/flutter#148909)
2024-05-22 katelovett@google.com [wiki migration] Remaining pages under docs/about/ (flutter/flutter#148782)
2024-05-22 engine-flutter-autoroll@skia.org Roll Flutter Engine from b6971cdf14f8 to 8b094fbb94d8 (3 revisions) (flutter/flutter#148883)
2024-05-22 42757204+wyqlxf@users.noreply.github.com Fix the second TextFormField to trigger onTapOutside (flutter/flutter#148206)
2024-05-22 34871572+gmackall@users.noreply.github.com Try removing robolectric from `integration_test` tests (flutter/flutter#148803)
2024-05-22 victoreronmosele@gmail.com Prevent test folder deletion on running `flutter create --empty` on an existing app project (flutter/flutter#147160)
2024-05-22 katelovett@google.com [wiki migration] Tool team pages (flutter/flutter#148779)
2024-05-22 engine-flutter-autoroll@skia.org Roll Flutter Engine from c89defa55801 to b6971cdf14f8 (6 revisions) (flutter/flutter#148819)
2024-05-22 dacoharkes@google.com [native_assets] Add support for link hooks (flutter/flutter#148474)
2024-05-22 engine-flutter-autoroll@skia.org Roll Packages from ba19b24 to 6525441 (12 revisions) (flutter/flutter#148864)
2024-05-22 36861262+QuncCccccc@users.noreply.github.com Update tokens to 4.0.0 (flutter/flutter#148789)
2024-05-22 zanderso@users.noreply.github.com Move Linux web_long_running_tests_2_5 to bringup (flutter/flutter#148854)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC rmistry@google.com,stuartmorgan@google.com,tarrinneal@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
victorsanni pushed a commit to victorsanni/flutter that referenced this pull request May 31, 2024
This PR adds support invoking `link.dart` hooks.

Link hooks can add new assets. Link hooks can transform assets sent to link hook from build hooks.

This PR does not yet add support for getting tree-shake information in the link hooks. This is pending on defining the `resources.json` format (dart-lang/sdk#55494).

Issue:

* flutter#146263

## Implementation considerations

The build hooks could be run before Dart compilation and the link hooks after Dart compilation. (This is how it's done in Dart standalone.) However, due to the way the `Target`s are set up, this would require two targets and serializing and deserializing the `BuildResult` in between these. This would lead to more code but no benefits. Currently there is nothing that mandates running build hooks before Dart compilation.

## Testing

* The unit tests verify that the native_assets_builder `link` and `linkDryRun` would be invoked with help of the existing fake.
* The native assets integration test now also invokes an FFI call of a package that adds the asset during the link hook instead of the build hook.
  * In order to keep coverage of the `flutter create --template=package_ffi`, `flutter create` is still run and the extra dependency is added and an extra ffi call is added. (Open to alternative suggestions.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: desktop Running on desktop autosubmit Merge PR when tree becomes green via auto submit App tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants