Skip to content

refactor: ordered startup hooks around runApp (WP-00 R3) - #3

Merged
AdamMusa merged 8 commits into
mainfrom
refactor/entrypoint-hooks
Nov 21, 2026
Merged

AdamMusa merged 8 commits into
mainfrom
refactor/entrypoint-hooks

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

What

WP-00 R3: two ordered startup hook points in lib/main.self.dart and lib/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 of lib/main*.dart.

  • rufletPreRunAppHooks are awaited one at a time just before runApp. By then the binding, every extension and the backend URL are ready.
  • rufletPostFirstFrameHooks are scheduled right after runApp, so they run after the first frame.
  • A hook that throws is reported through FlutterError.reportError (library ruflet bootstrap). The hooks after it still run, and so does the app.

Each entrypoint declares both lists itself, const and empty, between two marker lines. Next to them sit typedef RufletBootstrapHook = FutureOr<void> Function(); and the runner:

// --RUFLET_BOOTSTRAP_HOOKS_START--
const rufletPreRunAppHooks = <RufletBootstrapHook>[];
const rufletPostFirstFrameHooks = <RufletBootstrapHook>[];
// --RUFLET_BOOTSTRAP_HOOKS_END--

Head 08c4a00: 5 files, +463 lines (git diff --stat e32c873 08c4a00). They are README.md, both entrypoints, test/ruflet_bootstrap_test.dart and tool/conformance/test_bootstrap_hooks_contract.rb. The branch is not behind main (e32c873).

The conformance test follows every relative import, export and part (conditional alternatives included) from the entrypoints and lib/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 approved 391f188) changes only the conformance test's DIRECTIVE pattern 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, so import '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's CLIENT_BOOTSTRAP_DIRECTIVE (ruflet#76 c3604c40), 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 commit 0ac2003e and the v0.0.22 tag. 0.0.17's release commit there (5f68436b) copies only lib/main*.dart and removes the three connection_probe files. main.server.dart already imported connection_probe.dart on e32c873, and this PR does not change that.

ruflet build web. The matrix below ran 2026-11-17 at 391f188. 08c4a00 changes only tool/conformance/test_bootstrap_hooks_contract.rb, which no build compiles. On 2026-11-19 I re-ran 0.0.23 into build/client against 08c4a00: exit 0, main.dart.js 8,124,849 B, the block in both entrypoints, no generated file, 0 Error: lines, and flutter test +7, all pass.

Matrix details: Each run uses Flutter 3.41.2 and a fresh ruflet new app. The template is fetched live from a scratch bare repo whose main is the named commit, with a fresh RUFLET_CACHE_DIR. A RUFLET_CLIENT_DIR or ./ruflet_client client starts as a copy of e32c873.

Gem Client this PR (391f188) control: today's main (e32c873)
0.0.23 build/client OK exit 0
0.0.23 RUFLET_CLIENT_DIR OK not run
0.0.23 ./ruflet_client OK not run
0.0.21 build/client OK exit 0
0.0.20 build/client OK exit 0
0.0.19 build/client OK exit 0
0.0.18 RUFLET_CLIENT_DIR OK not run
0.0.16 RUFLET_CLIENT_DIR OK not run
0.0.15 RUFLET_CLIENT_DIR OK not run

OK means exit 0, main.dart.js written, 0 Error: lines, the marked block in both of the client's entrypoints, and no lib/ruflet_bootstrap.g.dart. In the RUFLET_CLIENT_DIR and ./ruflet_client rows, only the fetched template can have put the block there, because the client started as e32c873.

How the older gems were run:

  • 0.0.15 to 0.0.18 pick Flutter through FVM, so their runs pin 3.41.2 as an FVM project would: .fvmrc, .fvm/flutter_sdk and RUFLET_FLUTTER_VERSION=3.41.2. Each client's Generated.xcconfig confirms FLUTTER_ROOT=…/3.41.2.
  • Every run adds a top-level backend_url to ruflet.yaml, which 0.0.19 requires.
  • Not run: 0.0.13, 0.0.17 and 0.0.22, which are not installed here, and 0.0.14, which is installed only for Ruby 3.4.

flutter test in a CLI-built client: same result as on e32c873

Every CLI renames the client package (apply_pubspec_metadata), and build/client keeps test/. So test/ruflet_bootstrap_test.dart reaches lib/main.server.dart relatively, and it never names the two lists, which a hooks build moves.

flutter test in build/client, right after each build/client build above:

CLI on e32c873 on this PR (391f188)
released gem 0.0.23 +3, all pass +7, all pass
released gem 0.0.21 +2 −1 +6 −1
released gem 0.0.20 +2 −1 +6 −1
released gem 0.0.19 +2 −1 +6 −1
ruflet#76 (04b13f2a) +3, all pass +7, all pass
ruflet#76 (c3604c40), against 08c4a00 not run +7, all pass
  • With 0.0.19, 0.0.20 and 0.0.21, the same pre-existing test fails on e32c873 and on this PR: ruflet_namespace_test.dart's "all bundled engine and extension packages use the Ruflet namespace".
  • The 4 new ruflet_bootstrap_test.dart tests pass in every build/client above that has this PR, 0.0.19 to 0.0.21 included (flutter test test/ruflet_bootstrap_test.dart: +4, all pass).
  • With ruflet#76 plus a 2-hook probe provider (build/client routed to the generated file), flutter test also passes 7/7 (see ruflet#76).

Compatible with #2

#2 (draft, 7ff1809, engine f44e7df7) merges with this branch cleanly (git merge-tree exit 0). #2 does not touch lib/. On a scratch export of that merge, run 2026-11-19:

  • test_bootstrap_hooks_contract.rb passes: 9 runs, 221 assertions, in all three locales.
  • test_ruflet_source_integrity.rb (2 runs), test_self_contained_runtime_contract.rb (3) and tool/test_sync_ruflet_source.rb (16) are green.
  • sync_ruflet_source.rb --check verifies 1,388 files at f44e7df7.
  • The merged tree differs from the previous round's merge of 391f188 and 7ff1809 only in the conformance test, so its flutter test result there (11/11) still describes the same Dart files. I did not re-run it.

Contract

  • The gem (Ruflet::CLI::ClientBootstrap) finds the block by its two whole marker lines. The block holds exactly the two empty const lists. The runner and RufletBootstrapHook sit outside it, identical in both entrypoints.
  • lib/ruflet_bootstrap.g.dart is 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

  • No new code runs with both lists empty.
    • The pre-runApp point is if (rufletPreRunAppHooks.isNotEmpty) await …, which adds no await.
    • The post-frame scheduler returns before addPostFrameCallback.
    • The conformance test pins that nothing runs between the block and runApp.
  • H1 with this template vendored. On ruflet#76 (c3604c40) and on ruflet main (dd924eb3), I ran compat.rb vendor-template --ref 08c4a00 and then --regenerate (2026-11-19). The previous round did the same with 391f188 and 20df7e0 on 04b13f2a and c2d547b9.
    • For each ref, the two CLIs produce byte-identical goldens for all 234 entries, and none mentions ruflet_bootstrap.g.dart.
    • Against the committed goldens (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 are lib/main*.dart hash and size lines.
  • flutter analyze: 780 findings at 391f188, identical after normalizing line and column numbers to the runs on 20df7e0 and e32c873. Carried over: no Dart file changed since 20df7e0.

Tests (run 2026-11-19 at 08c4a00)

  • ruby tool/conformance/test_bootstrap_hooks_contract.rb: 9 runs, 221 assertions, 0 failures, with LANG unset, C and en_US.UTF-8.
  • This round's sensitivity. 14 mutants, each planted alone: the review's 12, the new T13 and the new control T14. At 08c4a00 the 12 that should fail do, and both controls (T5, T14) pass. At 391f188 three 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.
  • Earlier rounds' sensitivity (at 391f188). I planted mutants one at a time.
    • The previous round's 20 are all still caught. They cover the generated file named in any directive form, a non-shipped import, a shipped generated file, tests that import the package or name a list, and changes to the block, the guard, the post-frame call or the runner.
    • Eight new ones are caught too: the review's shim (lib/bootstrap_shim.dart exporting the generated file, imported by main.self.dart); the same shim with import and double quotes from main.server.dart; a two-level shim through lib/sub/; a harmless new shipped lib file imported by main.server.dart; a new shipped lib file as a conditional alternative in connection_probe.dart; connection_probe.dart naming the generated file; connection_probe_io.dart exporting it (reachable only as an alternative); and lib/main.dart importing ../test/widget_test.dart.
    • Three controls still pass: main.self.dart importing connection_probe.dart, main.dart importing ./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 at 98377b7f. test_ruflet_source_integrity.rb (2 runs), test_self_contained_runtime_contract.rb (3) and tool/test_sync_ruflet_source.rb (16) are green.
  • test_ruflet_control_surface.rb (RUFLET_SOURCE_ROOT = ruflet main dd924eb3) fails one assertion, advertised_entries expected 386, actual 449, the same on template main e32c873. 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.js from the build/client runs above, same app name:

  • 0.0.23: 8,124,803 B on e32c873 and 8,124,849 B on 391f188 (+46).
  • 0.0.19, 0.0.20 and 0.0.21 show the same +46.
  • ruflet#76 with the 2-hook probe: 8,125,969 B.

Flutter 3.41.2 APIs used (X2)

I re-read each line in /Users/macbookpro/fvm/versions/3.41.2 this round:

  • runApp only schedules work. _runWidget calls scheduleAttachRootWidget and scheduleWarmUpFrame: packages/flutter/lib/src/widgets/binding.dart:1614-1616 and 1679-1683. The attach step is a Timer.run: widgets/binding.dart:1388-1391.
  • The warm-up frame runs from timers: scheduler/binding.dart:1037 and bin/cache/pkg/sky_engine/lib/ui/platform_dispatcher.dart:902-909. So a post-frame callback registered right after runApp fires 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 build fetches 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).

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.
@AdamMusa AdamMusa changed the title refactor: build-written bootstrap hooks around runApp (WP-00 R3) refactor: ordered startup hooks around runApp (WP-00 R3) Nov 8, 2026
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.
@AdamMusa
AdamMusa merged commit 8b08569 into main Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant