You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[native_assets] Stop doing dry run in flutter run #154425
We introduced dry-runs to build hooks due to flutter run running concurrently with the inner flutter assemble.
Now we have made the native_assets_builder wait on concurrent invocations, we should be able to do full builds in flutter run which will then be picked up by flutter assemble also invoking the native_assets_builder.
This will simplify the user experience, as there is no longer any reasoning about dry runs or not.
We can't easily get rid of dry-run in flutter run.
Some information need to run hooks is only defined in the native builds (XCodeBuild, Grade, CMake), and only available to flutter assemble:
Android Minimum SDK version
iOS/MacOS code sign identity. We might be able to sign with the empty identity. But that would lead to two different binaries within flutter run (empty identity) and flutter assemble (the identity chosen in the xcodeproject)
Not available in flutter run, but we could work around it:
iOS SDK (emulator vs device). We could take the info from the devices instead of the SDK root environment variable.
Target architectures. We could take the info from the devices instead of the SDK root environment variable.
Some information is available in flutter run:
Build mode (release vs debug, the flag is explicit in flutter run)
iOS/MacOS minimum SDK version (hardcoded in Flutter tools). If Flutter ever decides to get this value from the XCodeBuild it would have the same issue as Android Minimum SDK.
We could start defining the minimum Android SDK version in Flutter apps in the pubspec. This has the issue that now we have information in two places, in the native build files of the Android app, and in the pubspec. And users can let these go out of sync. An alternative that doesn't work is to invoke the build with Flutter's lowest supported Android minimum SDK version. But that would make flutter run fail if a build hook and app can both do a newer version, but not the lowest version.
I think a somewhat deeper design question is where Flutter wants to have configuration such as which hardware architectures are supported and what the minimum OS SDK is. Currently, these are defined in the native builds. This means they cannot be surfaced on pub for example (dart-lang/pub-dev#8010). cc @andrewkolos@christopherfujino@stuartmorgan
I'd prefer having a way to specify such constraints that is not flutter specific, but that would work in Dart standalone packages as well. Of course that would lead to duplicate specification, both in the generated flutter native build system files for a Flutter app, and the pubspec. But maybe it would be possible to read them from the pubspec in the native build files?
We could start defining the minimum Android SDK version in Flutter apps in the pubspec. This has the issue that now we have information in two places, in the native build files of the Android app, and in the pubspec. And users can let these go out of sync.
I'm not clear on what the advantage would be that would offset these downsides. If we need this information, why not just read it out of the source-of-truth files?
I think a somewhat deeper design question is where Flutter wants to have configuration such as which hardware architectures are supported and what the minimum OS SDK is. Currently, these are defined in the native builds.
Why wouldn't they be? The only other use case I've ever heard for this information beyond the native builds themselves is to use as a constraint in pub resolution, and there has been significant resistance to adding those dimensions to the constraint matrix.
Why couldn't pub.dev read the actual value for these from the source of truth, rather than a copy that has no enforcement and could, as you pointed out, easily become out of sync?
I'd prefer having a way to specify such constraints that is not flutter specific, but that would work in Dart standalone packages as well.
The prior art here would be the top-level platforms: tag, which is used when present, and when it isn't pub.dev uses the plugin source of truth (flutter:plugin:platforms:).
The last few days I've been looking into the code bases of the native asset system in flutter and outside of flutter and I encounter complexity everywhere - and I really wonder whether this complexity is actually needed or justified.
We can't easily get rid of dry-run in flutter run.
Some information need to run hooks is only defined in the native builds (XCodeBuild, Grade, CMake), and only available to flutter assemble:
Android Minimum SDK version
...
The flutter assemble command will trigger the build of native code in all transitive packages. That command should know everything it needs to know about the target we compile for and can pass that information to all the build scripts of packages that have native code (i.e. flutter assemble will invoke hook/build.dart of packages and provides it with a build config that includes android SDK versions, etc).
Not available in flutter run, but we could work around it:
Target architectures. We could take the info from the devices instead of the SDK root environment variable.
...
I think the complexity arises because the responsibility of dealing with code assets isn't in one place but split between
flutter assemble: It does the actual build of native code
flutter run: It does a dry run invocation - to see what native assets will be there (once the build will be done) - it then makes a native_assets.yaml, compiles that to kernel, includes that into the application kernel file.
How about we do this differently
flutter run will not do any dry run, it will simply invoke the native build system, which will then invoke flutter assemble.
flutter assemble will build the native code for all packages as it already does now and puts them into build/native_assets/**/*.so
=> We'll make it now also emit a small native_libs.txt file describing all the native assets it has built (it actually already has a build/native_assets/**/native_assets.yaml - but that is used for a different purpose and has Dart SDK specified format -- we don't need that here)
the native build (e.g. cmake, gradle) copies (as it already does currently) the build/native_assets/* into the actual bundle (e.g. build/linux/x64/debug/bundle/lib/*) - both all the native libraries but also the native_libs.txt
we make the flutter runtime use a custom library & symbol resolution via Dart_InitializeNativeAssetsResolver instead of using the standalone embedders resolution mechanism
=> The implementation here will then use native_libs.txt to load the corresponding libraries
This would allow us to remove all the complexity introduced into flutter tools that deals with native_assets.yaml, it's kernel compilation, kernel concatenation.
=> We make flutter decide how to bundle & load code assets (instead of trying to force us to do it the same way as standalone dart embedder does it).
If that works, then we can also get remove this dry-run complexity in the Dart hook build/link protocol.
Some information is only ... available to flutter assemble:
...
Not available in flutter run
flutter run (indirectly, via the native build system) calls flutter assemble. Therefore flutter assemble could just return this information to flutter run (e.g. by writing it out somewhere), can't it?
It may be just the way things got currently wired up with the native_asset.yaml complexity is that flutter run may? currently want this information before it (indirectly) calls flutter assemble. Though given my thoughts above, I think it should be sufficient to have that information after it (indirectly) calls flutter assemble.
we make the flutter runtime use a custom library & symbol resolution via Dart_InitializeNativeAssetsResolver instead of using the standalone embedders resolution mechanism
These functions assume the native_assets.yaml has already been used to do the native assets id to to path mapping and take a path as argument. We could change that, but that would prevent us from bundling the native assets mapping inside a kernel file, which would break Isolate.spawnUri in the standalone (and possible other) embedders.
I think the complexity arises because the responsibility of dealing with code assets isn't in one place but split between
Completely agreed. If we could limit it to one place, that would be great.
If we assume that we need the mapping inside the kernel file to support Isolate.spawnUri, maybe there's still a way to reuse what flutter assemble computed in flutter run. Since we already compile the native assets mapping to a separate kernel file (#147158), maybe we could put the output kernel file of KernelSnapshotNativeAssets in a location where flutter run can pick it up and concatenate it with its compiled kernel file.
It may be just the way things got currently wired up with the native_asset.yaml complexity is that flutter run may? currently want this information before it (indirectly) calls flutter assemble. Though given my thoughts above, I think it should be sufficient to have that information after it (indirectly) calls flutter assemble.
This is a correct assessment. The kernel compilation in flutter run (for the support of immediate hot reload and hot restart) happens concurrently with the native build invocation. If the kernel compilation in flutter run would be delayed until after the native build returns, we could indeed reuse the information from flutter assemble (either the BuildConfig info, or the native_assets.yaml, or the kernel file containing the mapping).
We could change that, but that would prevent us from bundling the native assets mapping inside a kernel file, which would break Isolate.spawnUri in the standalone (and possible other) embedders.
This issue is about the flutter embedder, and flutter disallows Isolate.spawnUri (i.e. it's an API that doesn't work in flutter). So if there's not any other reasons flutter may require dry-run, I think getting rid of the dry-run usage in flutter makes sense - it will simplify things considerably.
We can then discuss to what extend the dry-run functionality is needed for our standalone embedder and if we can find another solution there as well - thereby being able to remove it entirely from the protocol
We could change that, but that would prevent us from bundling the native assets mapping inside a kernel file, which would break Isolate.spawnUri in the standalone (and possible other) embedders.
This issue is about the flutter embedder, and flutter disallows Isolate.spawnUri (i.e. it's an API that doesn't work in flutter). So if there's not any other reasons flutter may require dry-run, I think getting rid of the dry-run usage in flutter makes sense - it will simplify things considerably.
We can then discuss to what extend the dry-run functionality is needed for our standalone embedder and if we can find another solution there as well - thereby being able to remove it entirely from the protocol
I think I did not explain this clearly or you misunderstood. We a native assets kernel mapping in the flutter run for hot reload and hot restart support, not for spawnUri support. We need the mapping inside the kernel file for supporting spawnUri. We don't need dry run in Dart standalone.
I am assuming we'd want one set of functions in Dart_InitializeNativeAssetsResolver. That set of functions needs to support spawnUri. So, that set of functions must be in some way linked to embedding the mapping inside the kernel file. (The current way it's linked is that the asset id is resolved to a path in the VM before calling the embedder function.) We could opt to add a new function to the Dart_InitializeNativeAssetsResolver that takes an asset ID and symbol. Then the flutter embedder would only provide that function and leave all the others null, and the Dart embedder would leave the new function null.
I think I did not explain this clearly or you misunderstood. We a native assets kernel mapping in the flutter run for hot reload and hot restart support, not for spawnUri support.
Why is there a need for this mapping to be shipped as part of a kernel file? This information has to be made available to the runtime (in AOT and in JIT / hot-reload) somehow but not necessarily as part of the kernel file.
While reading the flutter code, it seems it already has plenty of infrastructure for having other files as part of the app and synchronizing those to device as part of hot-reload (and bundling them for release builds). So instead of having a specialized solution with this kernel embedding, we could just take advantage of the existing mechanism flutter already uses. It may be as simple as dropping a code_assets.txt file in the build/flutter_assets folder - which flutter's existing infrastructure synchronizes to the device in hot-reload (and bundles in AOT mode) where the runtime can access it.
So my suggestion would be to re-use that - from reading through the code that would be a more natural way of doing it and probably simplify a bunch of things in the flutter tools.
I am assuming we'd want one set of functions in Dart_InitializeNativeAssetsResolver. That set of functions needs to support spawnUri. So, that set of functions must be in some way linked to embedding the mapping inside the kernel file.
Conceptually this interface should maybe different from what it is now: Dart_InitializeNativeAssetsResolver could take these functions:
So the embedder is given an asset id and a symbol (this would be symmetric to how e.g. we'd ask an embedder to load a data asset - or other resources identified by an id).
That means the txt file flutter embedder embeds contains information for each asset_id whether it's in executable, process, shared library (with path), ...
Why is there a need for this mapping to be shipped as part of a kernel file?
Isolate.spawnUri takes one argument for the uri, not a second argument for the native assets mapping uri. Unless we standardize to something like expecting the native assets mapping file to be right next to the kernel file. We had these discussions with @mraleph all the way in the beginning of this design.
Conceptually this interface should maybe different from what it is now: Dart_InitializeNativeAssetsResolver could take these functions:
And then the Dart standalone embedder would need to know about how to find the mapping embedded in the right kernel file. So either Dart_InitializeNativeAssetsResolver needs to be called for every isolate group and ObtainLibraryForAssetId would need to be a closure rather than a function pointer which somehow has access to the kernel mapping. Or ObtainLibraryForAssetId needs a param for the isolate group.
And Flutter can just ignore isolate groups and always use the native assets mapping read from a file it can access somewhere.
The embedder API must not have a current isolate, but must have a current isolate group.
I wanted to clarify this a bit: The symbol resolution shouldn't depend on isolate-specifics as the result of symbol resolution will be cached in object pool and used for all isolates. But if the implementation can be simplified in the embedder by running Dart code, we could decide to not do a IsolateLeaveScope before we call embedder. We'd then no longer enforce that embedder's resolution cannot depend on isolate-specifics - but push the responsibility to the embedder to "not do any isolate-specific / return address that works on one isolate (even if the current one died) / ...".
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Activity
dcharkes commentedon Sep 3, 2024
We can't easily get rid of dry-run in
flutter run.Some information need to run hooks is only defined in the native builds (XCodeBuild, Grade, CMake), and only available to
flutter assemble:flutter run(empty identity) andflutter assemble(the identity chosen in the xcodeproject)Not available in
flutter run, but we could work around it:Some information is available in
flutter run:We could start defining the minimum Android SDK version in Flutter apps in the pubspec. This has the issue that now we have information in two places, in the native build files of the Android app, and in the pubspec. And users can let these go out of sync. An alternative that doesn't work is to invoke the build with Flutter's lowest supported Android minimum SDK version. But that would make flutter run fail if a build hook and app can both do a newer version, but not the lowest version.
I think a somewhat deeper design question is where Flutter wants to have configuration such as which hardware architectures are supported and what the minimum OS SDK is. Currently, these are defined in the native builds. This means they cannot be surfaced on pub for example (dart-lang/pub-dev#8010). cc @andrewkolos @christopherfujino @stuartmorgan
I'd prefer having a way to specify such constraints that is not flutter specific, but that would work in Dart standalone packages as well. Of course that would lead to duplicate specification, both in the generated flutter native build system files for a Flutter app, and the pubspec. But maybe it would be possible to read them from the pubspec in the native build files?
FYI @mkustermann
stuartmorgan-g commentedon Sep 3, 2024
I'm not clear on what the advantage would be that would offset these downsides. If we need this information, why not just read it out of the source-of-truth files?
Why wouldn't they be? The only other use case I've ever heard for this information beyond the native builds themselves is to use as a constraint in pub resolution, and there has been significant resistance to adding those dimensions to the constraint matrix.
Why couldn't pub.dev read the actual value for these from the source of truth, rather than a copy that has no enforcement and could, as you pointed out, easily become out of sync?
The prior art here would be the top-level
platforms:tag, which is used when present, and when it isn't pub.dev uses the plugin source of truth (flutter:plugin:platforms:).mkustermann commentedon Sep 18, 2024
The last few days I've been looking into the code bases of the native asset system in flutter and outside of flutter and I encounter complexity everywhere - and I really wonder whether this complexity is actually needed or justified.
The
flutter assemblecommand will trigger the build of native code in all transitive packages. That command should know everything it needs to know about the target we compile for and can pass that information to all the build scripts of packages that have native code (i.e.flutter assemblewill invokehook/build.dartof packages and provides it with a build config that includes android SDK versions, etc).I think the complexity arises because the responsibility of dealing with code assets isn't in one place but split between
flutter assemble: It does the actual build of native codeflutter run: It does a dry run invocation - to see what native assets will be there (once the build will be done) - it then makes anative_assets.yaml, compiles that to kernel, includes that into the application kernel file.How about we do this differently
flutter runwill not do any dry run, it will simply invoke the native build system, which will then invokeflutter assemble.flutter assemblewill build the native code for all packages as it already does now and puts them intobuild/native_assets/**/*.so=> We'll make it now also emit a small
native_libs.txtfile describing all the native assets it has built (it actually already has abuild/native_assets/**/native_assets.yaml- but that is used for a different purpose and has Dart SDK specified format -- we don't need that here)build/native_assets/*into the actual bundle (e.g.build/linux/x64/debug/bundle/lib/*) - both all the native libraries but also thenative_libs.txtDart_InitializeNativeAssetsResolverinstead of using the standalone embedders resolution mechanism=> The implementation here will then use
native_libs.txtto load the corresponding librariesThis would allow us to remove all the complexity introduced into flutter tools that deals with
native_assets.yaml, it's kernel compilation, kernel concatenation.=> We make flutter decide how to bundle & load code assets (instead of trying to force us to do it the same way as standalone dart embedder does it).
If that works, then we can also get remove this dry-run complexity in the Dart hook build/link protocol.
@dcharkes Would that work?
mkustermann commentedon Sep 18, 2024
Additional question:
flutter run(indirectly, via the native build system) callsflutter assemble. Thereforeflutter assemblecould just return this information toflutter run(e.g. by writing it out somewhere), can't it?It may be just the way things got currently wired up with the
native_asset.yamlcomplexity is thatflutter runmay? currently want this information before it (indirectly) callsflutter assemble. Though given my thoughts above, I think it should be sufficient to have that information after it (indirectly) callsflutter assemble.dcharkes commentedon Sep 24, 2024
These functions assume the native_assets.yaml has already been used to do the native assets id to to path mapping and take a path as argument. We could change that, but that would prevent us from bundling the native assets mapping inside a kernel file, which would break
Isolate.spawnUriin the standalone (and possible other) embedders.Completely agreed. If we could limit it to one place, that would be great.
If we assume that we need the mapping inside the kernel file to support
Isolate.spawnUri, maybe there's still a way to reuse whatflutter assemblecomputed influtter run. Since we already compile the native assets mapping to a separate kernel file (#147158), maybe we could put the output kernel file ofKernelSnapshotNativeAssetsin a location whereflutter runcan pick it up and concatenate it with its compiled kernel file.This is a correct assessment. The kernel compilation in
flutter run(for the support of immediate hot reload and hot restart) happens concurrently with the native build invocation. If the kernel compilation influtter runwould be delayed until after the native build returns, we could indeed reuse the information fromflutter assemble(either theBuildConfiginfo, or thenative_assets.yaml, or the kernel file containing the mapping).mkustermann commentedon Sep 24, 2024
This issue is about the flutter embedder, and flutter disallows
Isolate.spawnUri(i.e. it's an API that doesn't work in flutter). So if there's not any other reasons flutter may require dry-run, I think getting rid of the dry-run usage in flutter makes sense - it will simplify things considerably.We can then discuss to what extend the dry-run functionality is needed for our standalone embedder and if we can find another solution there as well - thereby being able to remove it entirely from the protocol
dcharkes commentedon Sep 24, 2024
I think I did not explain this clearly or you misunderstood. We a native assets kernel mapping in the
flutter runfor hot reload and hot restart support, not forspawnUrisupport. We need the mapping inside the kernel file for supportingspawnUri. We don't need dry run in Dart standalone.I am assuming we'd want one set of functions in
Dart_InitializeNativeAssetsResolver. That set of functions needs to supportspawnUri. So, that set of functions must be in some way linked to embedding the mapping inside the kernel file. (The current way it's linked is that the asset id is resolved to a path in the VM before calling the embedder function.) We could opt to add a new function to theDart_InitializeNativeAssetsResolverthat takes an asset ID and symbol. Then the flutter embedder would only provide that function and leave all the others null, and the Dart embedder would leave the new function null.mkustermann commentedon Sep 24, 2024
Why is there a need for this mapping to be shipped as part of a kernel file? This information has to be made available to the runtime (in AOT and in JIT / hot-reload) somehow but not necessarily as part of the kernel file.
While reading the flutter code, it seems it already has plenty of infrastructure for having other files as part of the app and synchronizing those to device as part of hot-reload (and bundling them for release builds). So instead of having a specialized solution with this kernel embedding, we could just take advantage of the existing mechanism flutter already uses. It may be as simple as dropping a
code_assets.txtfile in thebuild/flutter_assetsfolder - which flutter's existing infrastructure synchronizes to the device in hot-reload (and bundles in AOT mode) where the runtime can access it.So my suggestion would be to re-use that - from reading through the code that would be a more natural way of doing it and probably simplify a bunch of things in the flutter tools.
Conceptually this interface should maybe different from what it is now:
Dart_InitializeNativeAssetsResolvercould take these functions:So the embedder is given an asset id and a symbol (this would be symmetric to how e.g. we'd ask an embedder to load a data asset - or other resources identified by an id).
That means the txt file flutter embedder embeds contains information for each
asset_idwhether it's in executable, process, shared library (with path), ...dcharkes commentedon Sep 24, 2024
Isolate.spawnUritakes one argument for the uri, not a second argument for the native assets mapping uri. Unless we standardize to something like expecting the native assets mapping file to be right next to the kernel file. We had these discussions with @mraleph all the way in the beginning of this design.And then the Dart standalone embedder would need to know about how to find the mapping embedded in the right kernel file. So either
Dart_InitializeNativeAssetsResolverneeds to be called for every isolate group andObtainLibraryForAssetIdwould need to be a closure rather than a function pointer which somehow has access to the kernel mapping. OrObtainLibraryForAssetIdneeds a param for the isolate group.And Flutter can just ignore isolate groups and always use the native assets mapping read from a file it can access somewhere.
dcharkes commentedon Sep 24, 2024
Notes from offline discussion with @mkustermann:
The embedder API must not have a current isolate, but must have a current isolate group. This avoids the need for passing the isolate group in
ObtainLibraryForAssetId. The standalone embedder can access the native assets mapping via https://dart-review.googlesource.com/c/sdk/+/379781, essentially lifting the native assets mapping out of the VM into the standalone embedder. (Addressing dart-lang/sdk#55826 in the process.)mkustermann commentedon Sep 27, 2024
I wanted to clarify this a bit: The symbol resolution shouldn't depend on isolate-specifics as the result of symbol resolution will be cached in object pool and used for all isolates. But if the implementation can be simplified in the embedder by running Dart code, we could decide to not do a
IsolateLeaveScopebefore we call embedder. We'd then no longer enforce that embedder's resolution cannot depend on isolate-specifics - but push the responsibility to the embedder to "not do any isolate-specific / return address that works on one isolate (even if the current one died) / ...".[vm/ffi] Add embedder API for resolving asset ids
[native assets] Consume `NativeAssetsManifest.json` (#56727)
[native assets] Create `NativeAssetsManifest.json` instead of kernel …
[native assets] Consume `NativeAssetsManifest.json` (flutter-team-arc…
github-actions commentedon Feb 17, 2025
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.