Repository navigation
refactor: ordered startup hooks around runApp (WP-00 R3) - #3
Merged
Merged
Conversation
tool/conformance/test_bootstrap_hooks_contract.rb requires that: - the template ships lib/ruflet_bootstrap.g.dart byte-identical to what `ruflet build` writes when no feature adds a hook (empty rufletPreRunAppHooks and rufletPostFirstFrameHooks); - lib/main.self.dart and lib/main.server.dart import it, await the pre-runApp hooks only when there are any and with nothing between them and runApp, and schedule the post-first-frame hooks as the statement right after runApp; - both entrypoints carry the same hook runner. test/ruflet_bootstrap_test.dart checks that runner: hooks run one at a time in list order, a failing hook is reported through FlutterError.reportError without stopping the hooks after it, and the post-first-frame hooks run once, after the first frame.
Give the entrypoints two ordered hook points that build features fill without editing lib/main*.dart: rufletPreRunAppHooks, awaited one at a time just before runApp, and rufletPostFirstFrameHooks, run after the first frame. Both lists come from lib/ruflet_bootstrap.g.dart, which `ruflet build` rewrites on every build. A hook that throws is reported through FlutterError.reportError and the app still starts. The template ships the file the CLI writes when no feature adds a hook: both lists are const and empty, and the entrypoints skip the hooks when a list is empty, so an unmodified template starts exactly as before (no extra await before runApp, no post-frame callback). The new import sits before the ruflet_webview import, so the CLI keeps appending extension imports where it did.
Released gems (0.0.19 to 0.0.23) fetch template main at build time and, for a client outside build/client (RUFLET_CLIENT_DIR, ./ruflet_client), refresh lib/main*.dart file by file. They never write lib/ruflet_bootstrap.g.dart, so entrypoints that import it stop compiling there. The entrypoints now declare both hook lists empty themselves, between // --RUFLET_BOOTSTRAP_HOOKS_START-- and // --RUFLET_BOOTSTRAP_HOOKS_END--, next to the RufletBootstrapHook typedef and the runner, and import nothing new. The template no longer ships lib/ruflet_bootstrap.g.dart. Only while a build feature adds hooks does `ruflet build` write that file, import it and empty the marked block (AdamMusa/ruflet#76). The import section is byte-identical to e32c873 again, so the CLI's import edits are unchanged too. The conformance test now checks that the template does not ship the generated file, that the entrypoints import only files the template ships, and that the marked block holds exactly the two empty lists. It fails on e32c873 and on the previous head 8c7e36a.
AdamMusa
added a commit
to AdamMusa/ruflet
that referenced
this pull request
Nov 8, 2026
Every build used to write lib/ruflet_bootstrap.g.dart into the Flutter client, which changed 60 H1 goldens and added an unused file to client dirs the developer owns (RUFLET_CLIENT_DIR, ./ruflet_client, templates/ruflet_flutter_template). The template entrypoints now declare both hook lists empty themselves between // --RUFLET_BOOTSTRAP_HOOKS_START-- and // --RUFLET_BOOTSTRAP_HOOKS_END-- (AdamMusa/ruflet-template#3), so they build with every gem, released ones included. write_client_bootstrap now: - writes and deletes nothing while no provider adds a hook and nothing under lib/ imports the generated file: every build today; - with hooks, writes them to lib/ruflet_bootstrap.g.dart, imports it from lib/main.self.dart and lib/main.server.dart and replaces the marked block with a pointer. An entrypoint without the block (every released template) gets a warning naming it, and is left alone; - keeps an imported file in step (empty lists without hooks), and removes a generated file nothing imports any more. A file it did not write is never removed; - validates every hook before touching any file, and reads lib/ as bytes, so a non-UTF-8 or unreadable file cannot fail a build. The 60 goldens go back to #73's, byte for byte: compat.rb checks 160/160 against them.
The conformance test scanned only single-quoted imports, so `import "ruflet_bootstrap.g.dart";` in an entrypoint passed it. That is the failure class of the earlier blocker: no released gem writes the file, so such an import stops the client compiling with them. flutter test would see it in main.server.dart, but nothing compiles main.self.dart in this repo. The test now reads every import, export and part directive up to its semicolon, in either quote style and across lines, including each alternative of a conditional import. An entrypoint may name only dart:, package: or files the template ships, and no directive or code line (only comments) may name lib/ruflet_bootstrap.g.dart.
test/ruflet_bootstrap_test.dart imported package:ruflet_client/, but every `ruflet build` (gems 0.0.15 to 0.0.23, ruflet#73, ruflet#76) renames the client package to the app's package name, and build/client keeps test/ as it is. There the import did not resolve, so a plain `flutter test` in build/client, which passed before, failed to load that file. The test now reaches lib/main.server.dart relatively (with the avoid_relative_lib_imports lint silenced for that one import), which resolves under any package name. The conformance test pins that no Dart file under lib/ or test/ imports the client package by its template name.
test/ruflet_bootstrap_test.dart named rufletPreRunAppHooks and rufletPostFirstFrameHooks. Once a build feature adds hooks, `ruflet build` moves both lists into lib/ruflet_bootstrap.g.dart, which the entrypoints import but do not export. build/client keeps test/ as it is, so in such a build `flutter test` would no longer compile that file. No build adds hooks yet, so nothing is affected today. The test now exercises only the runner, and the conformance test pins that no Dart file under test/ names either list. The template's own empty lists stay pinned by the conformance test's marked-block check.
The conformance test only checked the directives in the entrypoints and lib/main.dart, so a lib file that re-exported the generated bootstrap, imported from an entrypoint, passed. It now follows every relative import, export and part (conditional alternatives included) from those files, and requires each file it reaches to be one of the six lib files released gems copy into an existing client dir (lib/main*.dart and lib/connection_probe*.dart) and to name no generated file. Test and README only: lib/ is unchanged.
DIRECTIVE only saw a directive at the start of a line, so `import 'package:flutter/foundation.dart'; import 'bootstrap_shim.dart';` or `/* x */ import 'extra.dart';` reached a file the test never checked. A directive now also counts after another directive or a block comment on its line. A line comment still does not count. The pattern stays character for character the CLI's CLIENT_BOOTSTRAP_DIRECTIVE (ruflet#76), which changes with it. Only this test changes; lib/ and test/ are as they were.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
WP-00 R3: two ordered startup hook points in
lib/main.self.dartandlib/main.server.dart. Deep links (WP-02), the debug restart channel (WP-06), the startup trace (WP-08) and the splash and error hooks (WP-09) then each add a hook provider in the CLI, instead of editing the same lines oflib/main*.dart.rufletPreRunAppHooksare awaited one at a time just beforerunApp. By then the binding, every extension and the backend URL are ready.rufletPostFirstFrameHooksare scheduled right afterrunApp, so they run after the first frame.FlutterError.reportError(libraryruflet bootstrap). The hooks after it still run, and so does the app.Each entrypoint declares both lists itself,
constand empty, between two marker lines. Next to them sittypedef RufletBootstrapHook = FutureOr<void> Function();and the runner:lib/ruflet_bootstrap.g.dart, imports it, and replaces the marked block with a pointer comment.Head
08c4a00: 5 files, +463 lines (git diff --stat e32c873 08c4a00). They areREADME.md, both entrypoints,test/ruflet_bootstrap_test.dartandtool/conformance/test_bootstrap_hooks_contract.rb. The branch is not behindmain(e32c873).The conformance test follows every relative
import,exportandpart(conditional alternatives included) from the entrypoints andlib/main.dart, transitively. Every file it reaches must be one of the six lib files released gems copy into an existing client (lib/main*.dart,lib/connection_probe*.dart), and none may name the generated file.This round (
08c4a00, on top of the approved391f188) changes only the conformance test'sDIRECTIVEpattern and its comment (git diff --stat 391f188 08c4a00: 1 file, +5 −2).lib/,test/and the README are unchanged. Before, the pattern only saw a directive at the start of a line, soimport 'package:flutter/foundation.dart'; import 'bootstrap_shim.dart';or/* x */ import 'extra.dart';reached a file the test never checked (review mutants T8 and T9, and the new T13). A directive now also counts after another directive or a block comment on its line. A//comment still does not count. The pattern stays character for character the CLI'sCLIENT_BOOTSTRAP_DIRECTIVE(ruflet#76c3604c40), which changed with it.Works with released gems
Every released gem fetches this repository's default branch live. For a client outside
build/client(RUFLET_CLIENT_DIR,./ruflet_client), the gems copy only a fixed list of files again on each build (refresh_managed_client_template_files). The six lib files above are in that list for 0.0.10 to 0.0.16 and 0.0.18 to 0.0.23. I read this in the installed gem sources. For 0.0.13 and 0.0.22, I read it in AdamMusa/ruflet's history: 0.0.13's version commit0ac2003eand thev0.0.22tag. 0.0.17's release commit there (5f68436b) copies onlylib/main*.dartand removes the threeconnection_probefiles.main.server.dartalready importedconnection_probe.dartone32c873, and this PR does not change that.ruflet build web. The matrix below ran 2026-11-17 at391f188.08c4a00changes onlytool/conformance/test_bootstrap_hooks_contract.rb, which no build compiles. On 2026-11-19 I re-ran 0.0.23 intobuild/clientagainst08c4a00: exit 0,main.dart.js8,124,849 B, the block in both entrypoints, no generated file, 0Error:lines, andflutter test+7, all pass.Matrix details: Each run uses Flutter 3.41.2 and a fresh
ruflet newapp. The template is fetched live from a scratch bare repo whosemainis the named commit, with a freshRUFLET_CACHE_DIR. ARUFLET_CLIENT_DIRor./ruflet_clientclient starts as a copy ofe32c873.391f188)main(e32c873)build/clientRUFLET_CLIENT_DIR./ruflet_clientbuild/clientbuild/clientbuild/clientRUFLET_CLIENT_DIRRUFLET_CLIENT_DIRRUFLET_CLIENT_DIROK means exit 0,
main.dart.jswritten, 0Error:lines, the marked block in both of the client's entrypoints, and nolib/ruflet_bootstrap.g.dart. In theRUFLET_CLIENT_DIRand./ruflet_clientrows, only the fetched template can have put the block there, because the client started ase32c873.How the older gems were run:
.fvmrc,.fvm/flutter_sdkandRUFLET_FLUTTER_VERSION=3.41.2. Each client'sGenerated.xcconfigconfirmsFLUTTER_ROOT=…/3.41.2.backend_urltoruflet.yaml, which 0.0.19 requires.flutter testin a CLI-built client: same result as one32c873Every CLI renames the client package (
apply_pubspec_metadata), andbuild/clientkeepstest/. Sotest/ruflet_bootstrap_test.dartreacheslib/main.server.dartrelatively, and it never names the two lists, which a hooks build moves.flutter testinbuild/client, right after eachbuild/clientbuild above:e32c873391f188)04b13f2a)c3604c40), against08c4a00e32c873and on this PR:ruflet_namespace_test.dart's "all bundled engine and extension packages use the Ruflet namespace".ruflet_bootstrap_test.darttests pass in everybuild/clientabove that has this PR, 0.0.19 to 0.0.21 included (flutter test test/ruflet_bootstrap_test.dart: +4, all pass).build/clientrouted to the generated file),flutter testalso passes 7/7 (see ruflet#76).Compatible with #2
#2 (draft,
7ff1809, enginef44e7df7) merges with this branch cleanly (git merge-treeexit 0). #2 does not touchlib/. On a scratch export of that merge, run 2026-11-19:test_bootstrap_hooks_contract.rbpasses: 9 runs, 221 assertions, in all three locales.test_ruflet_source_integrity.rb(2 runs),test_self_contained_runtime_contract.rb(3) andtool/test_sync_ruflet_source.rb(16) are green.sync_ruflet_source.rb --checkverifies 1,388 files atf44e7df7.391f188and7ff1809only in the conformance test, so itsflutter testresult there (11/11) still describes the same Dart files. I did not re-run it.Contract
Ruflet::CLI::ClientBootstrap) finds the block by its two whole marker lines. The block holds exactly the two emptyconstlists. The runner andRufletBootstrapHooksit outside it, identical in both entrypoints.lib/ruflet_bootstrap.g.dartis never added here. Nothing the entrypoints reach may name it, and tests never name the lists it would hold. The CLI decides whether to keep that file with this test's two directive patterns.Proof of zero change
if (rufletPreRunAppHooks.isNotEmpty) await …, which adds noawait.addPostFrameCallback.runApp.c3604c40) and on rufletmain(dd924eb3), I rancompat.rb vendor-template --ref 08c4a00and then--regenerate(2026-11-19). The previous round did the same with391f188and20df7e0on04b13f2aandc2d547b9.ruflet_bootstrap.g.dart.e32c873), 140 differ in 383 lines, the same lines for both refs. All 227 "files identical" lines grow by exactly 2 (this PR's two new non-lib/files). The other 156 lines arelib/main*.darthash and size lines.flutter analyze: 780 findings at391f188, identical after normalizing line and column numbers to the runs on20df7e0ande32c873. Carried over: no Dart file changed since20df7e0.Tests (run 2026-11-19 at
08c4a00)ruby tool/conformance/test_bootstrap_hooks_contract.rb: 9 runs, 221 assertions, 0 failures, withLANGunset,Canden_US.UTF-8.08c4a00the 12 that should fail do, and both controls (T5, T14) pass. At391f188three of them pass: T8 and T9 (a second import on the line, of a new shipped file or of a shim to the generated file) and the new T13 (/* extra */ import 'extra.dart';). The new control T14 (// import 'extra.dart';) passes on both.391f188). I planted mutants one at a time.lib/bootstrap_shim.dartexporting the generated file, imported bymain.self.dart); the same shim withimportand double quotes frommain.server.dart; a two-level shim throughlib/sub/; a harmless new shipped lib file imported bymain.server.dart; a new shipped lib file as a conditional alternative inconnection_probe.dart;connection_probe.dartnaming the generated file;connection_probe_io.dartexporting it (reachable only as an alternative); andlib/main.dartimporting../test/widget_test.dart.main.self.dartimportingconnection_probe.dart,main.dartimporting./main.self.dart, and a shipped lib file that names the generated file but that nothing reaches.flutter test: 7/7.ruby tool/sync_ruflet_source.rb --check: 987 files verified at98377b7f.test_ruflet_source_integrity.rb(2 runs),test_self_contained_runtime_contract.rb(3) andtool/test_sync_ruflet_source.rb(16) are green.test_ruflet_control_surface.rb(RUFLET_SOURCE_ROOT= rufletmaindd924eb3) fails one assertion,advertised_entriesexpected 386, actual 449, the same on templatemaine32c873. It is not touched here; chore: sync ruflet-engine 00d1df3c and the ruflet main control surface (CRIT-04a) #2 syncs the control surface.Size (X8)
Release
main.dart.jsfrom thebuild/clientruns above, same app name:e32c873and 8,124,849 B on391f188(+46).Flutter 3.41.2 APIs used (X2)
I re-read each line in
/Users/macbookpro/fvm/versions/3.41.2this round:runApponly schedules work._runWidgetcallsscheduleAttachRootWidgetandscheduleWarmUpFrame:packages/flutter/lib/src/widgets/binding.dart:1614-1616and1679-1683. The attach step is aTimer.run:widgets/binding.dart:1388-1391.scheduler/binding.dart:1037andbin/cache/pkg/sky_engine/lib/ui/platform_dispatcher.dart:902-909. So a post-frame callback registered right afterrunAppfires at the end of the first frame.SchedulerBinding.addPostFrameCallback:scheduler/binding.dart:818.FlutterError.reportError:foundation/assertions.dart:1202.FlutterErrorDetails::386.ErrorDescription::259.Spec note (for the lead)
Spec §3 R3 says the lists are "generated from a build-written
lib/ruflet_bootstrap.g.dart". That now holds only for builds that add hooks. Without hooks, the lists live in the entrypoints, which keeps released gems working. The spec text needs that amendment.Release coupling: do not merge without the owner (D1)
ruflet buildfetches this template live, so merging it ships it to every user. No release order is needed (see the matrix above). Companion: AdamMusa/ruflet#76 (c3604c40).