|
| 1 | +--- |
| 2 | +name: repack-debug-runtime |
| 3 | +description: > |
| 4 | + Use when inspecting a running Re.Pack app or dev server to find out why |
| 5 | + something fails at runtime: chunks or scripts not loading (404, timeouts, |
| 6 | + cache), Module Federation remotes/shared packages misbehaving, compilation or |
| 7 | + HMR errors, LogBox stacks that are not symbolicated, or uncaught JS errors. |
| 8 | + Covers starting the tester apps with the Re.Pack DevTools plugin, the |
| 9 | + `rozenite agent` tools of `callstack/repack-devtools`, the |
| 10 | + `/repack-devtools/*` dev-server endpoints, and playbooks per failure type. |
| 11 | +--- |
| 12 | + |
| 13 | +# Debug a running Re.Pack app |
| 14 | + |
| 15 | +`packages/repack-devtools-plugin` (private, workspace only) records what |
| 16 | +Re.Pack does at runtime and on the dev server, computes diagnostics, and |
| 17 | +exposes them two ways: |
| 18 | + |
| 19 | +- **App side** (needs a running app): `rozenite agent callstack/repack-devtools <tool>` |
| 20 | +- **Dev-server side** (works without an app, even when the bundle fails to |
| 21 | + build): `curl <origin>/repack-devtools/state` |
| 22 | + |
| 23 | +Both are wired into every tester app behind `WITH_ROZENITE=true`; without it |
| 24 | +nothing is loaded or bundled. Never touch |
| 25 | +ports or devices you did not start: other sessions often own `8081` and |
| 26 | +`emulator-5554`. |
| 27 | + |
| 28 | +## 1. Start |
| 29 | + |
| 30 | +```sh |
| 31 | +cd apps/tester-app # or tester-federation, tester-federation-v2, tester-expo |
| 32 | +WITH_ROZENITE=true pnpm start --port 8091 # MF testers: start:hostapp / start:miniapp |
| 33 | +``` |
| 34 | + |
| 35 | +The log must show `[Rozenite] Loaded N plugin(s)` including |
| 36 | +`@callstack/repack-devtools-plugin`. Check the endpoint before launching the app: |
| 37 | + |
| 38 | +```sh |
| 39 | +curl -s localhost:8091/repack-devtools/state?requests=none |
| 40 | +``` |
| 41 | + |
| 42 | +Point a debug build at the port (Android; package ids are in each `app.json`): |
| 43 | + |
| 44 | +```sh |
| 45 | +adb -s <serial> reverse tcp:8091 tcp:8091 |
| 46 | +adb -s <serial> shell "run-as <pkg> sh -c 'mkdir -p shared_prefs && echo \"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\" standalone=\\\"yes\\\" ?><map><string name=\\\"debug_http_host\\\">localhost:8091</string></map>\" > shared_prefs/<pkg>_preferences.xml'" |
| 47 | +adb -s <serial> shell am force-stop <pkg> |
| 48 | +adb -s <serial> shell am start -n <pkg>/com.microsoft.reacttestapp.MainActivity |
| 49 | +``` |
| 50 | + |
| 51 | +MF remotes have fixed URLs in the host config (`localhost:8082`, `8083`); also |
| 52 | +`adb reverse` those ports. On iOS simulators, `localhost` already reaches the |
| 53 | +host; use the dev menu or `RCT_jsLocation` to change the port. |
| 54 | + |
| 55 | +## 2. Inspect |
| 56 | + |
| 57 | +Run from an app directory so `rozenite` resolves (`pnpm exec rozenite …`). |
| 58 | +Pass `--port <dev server port>` to every call. |
| 59 | + |
| 60 | +```sh |
| 61 | +pnpm exec rozenite agent targets --port 8091 # app connected? |
| 62 | +pnpm exec rozenite agent callstack/repack-devtools get-overview --port 8091 |
| 63 | +pnpm exec rozenite agent session stop --port 8091 # when done |
| 64 | +``` |
| 65 | + |
| 66 | +**Always start with `get-overview`.** It returns app info (platform, RN |
| 67 | +version, bundle URL, public path, dev server), record counts, and |
| 68 | +severity-sorted `diagnostics` merged from the app, its dev server and the dev |
| 69 | +servers of MF remotes (those are prefixed with `[origin]`; see |
| 70 | +`remoteDevServers` for their compile status). Every |
| 71 | +diagnostic has a `ref` that tells you which tool gives details: |
| 72 | + |
| 73 | +| `ref` | Follow up with | |
| 74 | +| --- | --- | |
| 75 | +| `<script uniqueId>` | `get-script --uniqueId <ref>`, `get-script-manager` | |
| 76 | +| `call:<seq>` | `list-script-calls --uniqueId <id>` | |
| 77 | +| `fault-<n>` / `server-fault-<n>` | `list-faults` (injected by you; not a real bug) | |
| 78 | +| `federation-event:<seq>` | `list-federation-events --errorsOnly true` | |
| 79 | +| `<instance>:<scope>/<pkg>` | `get-federation` | |
| 80 | +| `err-<n>` | `get-error --id <ref>`, then `symbolicate --errorId <ref>` | |
| 81 | +| `request:<seq>` | `get-dev-server --requests failed` | |
| 82 | +| `compilation:<platform>` | `curl <origin>/repack-devtools/compilation?platform=<platform>` | |
| 83 | + |
| 84 | +### Tools |
| 85 | + |
| 86 | +Read-only: `get-overview [--devServer false]`, `list-scripts [--status failed] [--search x]`, |
| 87 | +`get-script --uniqueId`, `list-script-events [--uniqueId] [--since seq]`, |
| 88 | +`get-federation`, `list-federation-events [--type] [--errorsOnly true]`, |
| 89 | +`list-errors [--source global-handler|script-manager|federation]`, `get-error --id`, |
| 90 | +`symbolicate [--stack "<raw stack>"] [--errorId]`, |
| 91 | +`get-dev-server [--origin http://localhost:8082] [--requests failed|all|none]`, |
| 92 | +`list-script-calls [--uniqueId] [--method loadScript] [--errorsOnly true]`, |
| 93 | +`get-script-manager`, `list-faults [--origin]`. |
| 94 | +List tools paginate with `--limit` and `--cursor` (take the cursor from `page`). |
| 95 | + |
| 96 | +Actions, to reproduce issues without driving the UI: |
| 97 | +`load-script --scriptId <id> [--caller <id>]`, `prefetch-script`, |
| 98 | +`invalidate-scripts [--scriptIds a,b]`, `load-remote --id Remote/Module [--instance]`, |
| 99 | +`clear [--scope scripts|federation|errors|all]`, `reload`. |
| 100 | + |
| 101 | +Fault injection, to test error handling and recovery without editing app code: |
| 102 | + |
| 103 | +- `inject-script-fault --stage resolve|load [--scriptId x] [--caller y] |
| 104 | + [--action fail|delay] [--delayMs n] [--times n] [--code c]`: through |
| 105 | + ScriptManager hooks. `resolve` throws before resolvers run (a resolver that |
| 106 | + fetches a manifest while offline); `load` throws before the download with |
| 107 | + `NetworkFailure` (loadScript only). |
| 108 | +- `inject-server-fault --match <substring|/regex/> [--status 503] |
| 109 | + [--delayMs n] [--drop true] [--times n] [--platform] [--origin]`: on any |
| 110 | + dev server wrapped with `withRepackDevTools`, including MF remotes. Faulted |
| 111 | + responses carry `fault` in the request log; dropped connections are logged |
| 112 | + with `status: 0`. |
| 113 | +- `clear-faults [--id] [--origin]`. Faults stay active until `--times` is |
| 114 | + used up; always clear them when done, and remember that the overview's |
| 115 | + `faults` diagnostics mean failures you caused yourself. |
| 116 | + |
| 117 | +### Dev-server endpoints (curl, no app needed) |
| 118 | + |
| 119 | +- `GET /repack-devtools/state?requests=failed|all|none`: per-platform |
| 120 | + compilation (`status`, `builds`, errors, warnings, assets, changed files), |
| 121 | + request log (failed requests keep the first 4 KB of the response body), and |
| 122 | + diagnostics. |
| 123 | +- `GET /repack-devtools/compilation?platform=android`: one compilation in full. |
| 124 | +- `POST /repack-devtools/clear`: empties the request log. |
| 125 | +- `GET|POST|DELETE /repack-devtools/faults[?id=]`: list, add (JSON body like |
| 126 | + `inject-server-fault`) or remove server faults. |
| 127 | + |
| 128 | +Every MF dev server has its own endpoint (host and each mini app). |
| 129 | +`builds: 0` with `status: compiling` means that platform was never requested. |
| 130 | + |
| 131 | +## 3. Playbooks |
| 132 | + |
| 133 | +**Chunk / script fails to load.** `list-scripts --status failed`, then |
| 134 | +`get-script`. `error.stage` says where it broke (`resolve` = no resolver |
| 135 | +matched or the resolver threw; `load` = download or evaluation; |
| 136 | +`create` = bad locator). Compare `locator.url` with the dev server: a 404 |
| 137 | +diagnostic lists emitted assets with similar names. If the record shows |
| 138 | +`source: cache`, call `invalidate-scripts` and `load-script` again to tell |
| 139 | +cache problems from server problems. |
| 140 | + |
| 141 | +**Retry / recovery after a failure.** Inject the failure once |
| 142 | +(`inject-script-fault --stage resolve --scriptId X --times 1`, or |
| 143 | +`inject-server-fault --match X --status 503 --times 1`), call `load-script` |
| 144 | +twice, then `list-script-calls --uniqueId X`. A healthy retry shows |
| 145 | +`resolverRan: true` on the second call. `resolverRan: false` with |
| 146 | +`joined: rejected load` means ScriptManager returned the stored failure |
| 147 | +instead of trying again; `get-script-manager` then lists that promise as |
| 148 | +`rejected` (issue #1506). The same check works for prefetch-then-load and for |
| 149 | +concurrent callers (`joined: pending ...` is expected deduplication). |
| 150 | + |
| 151 | +**Resolver and cache behaviour.** `get-script-manager` lists resolvers with |
| 152 | +priorities, taps per hook and cache entries. `list-script-calls` tells how |
| 153 | +often resolvers really ran versus calls served from a stored promise; compare |
| 154 | +with the dev server request log to see what was actually downloaded. |
| 155 | + |
| 156 | +**Module Federation.** `get-federation` shows each host instance's remotes |
| 157 | +(`loaded`), shared packages per scope with every registered version, who |
| 158 | +provided it (`from`), and which one is `loaded`. Diagnostics flag singletons |
| 159 | +loaded twice and loaded versions that miss a `requiredVersion`. For load |
| 160 | +failures, `list-federation-events --errorsOnly true` gives the hook, |
| 161 | +remote entry URL and error. Then check the remote's own dev server: |
| 162 | +`get-dev-server --origin http://localhost:8082 --requests failed`. |
| 163 | +Use `load-remote --id MiniApp/<exposed>` to retry a single remote load. |
| 164 | +If `runtimePluginInstalled` is false, MF events are not recorded: add |
| 165 | +`require.resolve('@callstack/repack-devtools-plugin/mf-runtime-plugin')` to |
| 166 | +`runtimePlugins`, or call `installRepackDevTools()` in the entry. |
| 167 | + |
| 168 | +**Compilation or HMR broken.** Use curl, since the app may not load. |
| 169 | +`compilations[].errors` carry ANSI-stripped messages with module names, and |
| 170 | +`changedFiles` lists what triggered the last rebuild. |
| 171 | + |
| 172 | +**Unsymbolicated stacks / `/symbolicate` 400.** Run `symbolicate` (no args |
| 173 | +throws and symbolicates a probe error in the app) or |
| 174 | +`symbolicate --errorId err-N`. Frames with `unmapped: true` still point at a |
| 175 | +bundle URL; `sourceMaps[]` gives the HTTP status of each bundle's `.map`. |
| 176 | +Re.Pack's `/symbolicate` only keeps frames whose `file` is an http(s) URL, and |
| 177 | +answers 400 "Cannot infer platform" when none carries a platform (query |
| 178 | +`platform=` or `name.<platform>.bundle`). |
| 179 | + |
| 180 | +**Uncaught errors.** `list-errors`, `get-error --id`, then |
| 181 | +`symbolicate --errorId`. |
| 182 | + |
| 183 | +## Gotchas |
| 184 | + |
| 185 | +- **Agent tool calls time out after about 30 s**: a `load-script` that waits |
| 186 | + on a long injected delay times out on the agent side while the app keeps |
| 187 | + going. Read the outcome afterwards with `list-script-calls`. |
| 188 | +- **`resolverRan` reads as "ScriptManager reached its resolve step during |
| 189 | + this call".** It is false when an injected `resolve` fault fired (check the |
| 190 | + `faults` column) and for calls that only joined another attempt (`joined` |
| 191 | + set). |
| 192 | +- **MF manifest errors hide the HTTP status**: MF parses error pages as JSON, |
| 193 | + so a 4xx/5xx shows up as `JSON Parse error`. The `afterLoadManifest` event |
| 194 | + `detail` has `HTTP <status>`. |
| 195 | + |
| 196 | +- **Startup is only captured** when the entry calls |
| 197 | + `installRepackDevTools({ boot: true })` before other code |
| 198 | + (`app.bootRecording: true`). The testers do this. |
| 199 | +- **After a reload the agent session goes stale**: tool calls time out with |
| 200 | + `Tool call timeout` or `ReactNativeApplication.enable timed out`. Run |
| 201 | + `rozenite agent session stop` and retry; the new runtime starts with empty |
| 202 | + records. |
| 203 | +- **Rebuilding the plugin package while a dev server runs** (`pnpm build` wipes |
| 204 | + `dist` first) leaves the server's resolver with a stale |
| 205 | + `Cannot find module '@callstack/repack-devtools-plugin'`. Restart the dev |
| 206 | + server, then relaunch the app. |
| 207 | +- **"More than one device is connected"**: every app pointed at the port |
| 208 | + (including a previous tester left running on the same emulator) is a |
| 209 | + target. Pass `--device <id>` from `rozenite agent targets` or set |
| 210 | + `ROZENITE_DEVICE`, or force-stop the other app. |
| 211 | +- **`Unknown domain "callstack/repack-devtools"`** means the app has not |
| 212 | + called `useRepackDevTools()` yet: it crashed or is stuck before the first |
| 213 | + render. Read `rozenite agent console getMessages --limit 40` and take a |
| 214 | + screenshot. |
| 215 | +- Launch Android apps with |
| 216 | + `adb shell am start -n <pkg>/com.microsoft.reacttestapp.MainActivity` for |
| 217 | + the testers; `monkey` launches are unreliable. |
| 218 | +- Ignore the `/_expo/*` and `/json*` requests made by DevTools; the request log |
| 219 | + already filters them. |
| 220 | +- `rozenite agent domains` lists only built-in domains on some versions. Call |
| 221 | + `rozenite agent callstack/repack-devtools` to list the plugin's tools. |
| 222 | +- Rozenite's own docs: `pnpm exec rozenite skills list` / `skills show <name>`. |
| 223 | +- From scripts, use `@rozenite/agent-sdk` with the typed descriptors in |
| 224 | + `@callstack/repack-devtools-plugin/sdk` (`repackDevToolsTools.getOverview`). |
0 commit comments