Skip to content

Keep Shiki's RegExp feature probes out of the bundler's reach - #2173

Merged
SawyerHood merged 1 commit into
mainfrom
bb/fix-1603-mobile-remote-access
Aug 21, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
bb/fix-1603-mobile-remote-access

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

Issue #1603 reports the web app on an iPhone 8 Plus (iOS 16.x, so Safari 16.x) loading and then hiding its UI, including the Remote access entry. The reproduction report (https://get-bb.github.io/reports/issues/1603.html) found it is not CSS. Shiki's JavaScript regex engine, oniguruma-to-es (pulled in through @pierre/diffs), detects RegExp v-flag and modifier support at module top level with try { new RegExp("[[]]", "v") } catch { return false } return true. Rolldown and the oxc minifier treat new RegExp(<literal>, <literal>) as pure, delete it from the try block and fold the probe to true. The very next statement, envFlags.unicodeSets && RegExp("[[^a]]", "v").test("a"), then runs unguarded and throws Invalid flags supplied to RegExp constructor. on every engine without the v flag (Safari/iOS 16.4-16.6; Vite's default build target is safari16.4). The library degrades gracefully on its own; the bundler removes the guard. Neither treeshake: false nor moduleSideEffects: "no-treeshake" helps, because the minifier applies the same purity model.

On current main the chunk that carries this code (dist-*.js) is no longer in the home route's static closure, so the whole app no longer blanks as in the report. It is still imported statically by plugin-frontend-*.js, BbSourceCode, TimelineFileDiffBlock, FileDiff, File and the diff worker pool. On a Safari 16.x engine the plugin runtime therefore fails to load (plugin runtime load failed: Invalid flags supplied to RegExp constructor.), so plugin-contributed UI such as Remote access and Automations never renders, and any view that renders a diff or source code trips the app error boundary.

What changed

  • patches/[email protected]: the three probes in dist/esm/index.js and dist/cjs/index.js construct the RegExp through Reflect.construct(RegExp, [...]), which the bundler cannot prove pure. Verified shapes: new RegExp(...) and RegExp(...) get folded; Reflect.construct(RegExp, ...) and new globalThis.RegExp(...) survive.
  • patches/@[email protected]: dist/worker/worker-portable.js (loaded by src/lib/diff-worker-pool.ts as a module worker) inlines its own copy of oniguruma-to-es, so the package patch alone does not reach it. Same three-line rewrite.
  • package.json registers the two patches under pnpm.patchedDependencies; pnpm-lock.yaml picks up the patch hashes.
  • apps/app/package.json adds oniguruma-to-es as a devDependency so the new test can bundle it directly.
  • apps/app/src/regex-engine-feature-probes.test.ts: bundles oniguruma-to-es and @pierre/diffs/worker/worker-portable.js with the app's Vite/Rolldown build (minified) and evaluates the output in a vm context whose RegExp rejects the v flag and pattern modifiers like Safari 16. Asserts that the engine loads and compiles a pattern without the v flag, and that the worker reaches its message listener registration.

Deviation from the report's proposal: the report also suggests patching [email protected]. The app reaches that package only through regex/internals (atomic, possessive, RegExpSubclass), which does not include its probes, so patching it would change nothing in the built output; I left it alone. Cause 1 from the report (regex lookbehind literals in @pierre/diffs and mdast-util-gfm-autolink-literal, Safari 16.0-16.3) is below Vite's declared safari16.4 floor and is out of scope here; the app still shows the error card on a Safari 16.0-era engine. No wire changes; no CLI or doc surfaces.

How you verified

Test fails before (patches removed from package.json, pnpm install), both cases at module top level of the bundle:

× oniguruma-to-es loads and picks a v-less target on Safari 16
× the @pierre/diffs portable worker evaluates on Safari 16
SyntaxError: Invalid flags supplied to RegExp constructor.
  ❯ SafariSixteenRegExp src/regex-engine-feature-probes.test.ts:65:13
  ❯ bundle.cjs:48:4094

Test passes after (pnpm exec turbo run test --filter=@bb/app -- src/regex-engine-feature-probes.test.ts): Tests 2 passed (2).

Built output: pnpm exec turbo run build --filter=bb-app --force, then grep -l "unicodeSets:!0" packages/bb-app/app/dist/assets/*.js finds nothing (before: dist-DdvSMfIo.js and worker-portable-DaFcwpf8.js); both chunks now contain the try{Reflect.construct(RegExp,[...])}catch{return!1}return!0 probes.

Browser check with the report's harness (Playwright webkit-1837, iPhone 8 Plus emulation, init script that makes the RegExp constructor reject flag v like Safari 16.4-16.6) against the packaged app on isolated ports:

  • before: sidebar shows New thread, Extensions, Threads, Settings; console: plugin runtime load failed: Invalid flags supplied to RegExp constructor.; has Remote access: false, has Automations: false
  • after: sidebar shows New thread, Extensions, Automations, Threads, Settings, Remote access; no errors; has Remote access: true, has Automations: true

Turbo, run from the committed tree after the final commit:

  • pnpm exec turbo run typecheck lint --filter=@bb/app: Tasks: 4 successful, 4 total
  • pnpm exec turbo run test --filter=@bb/app --force: Tasks: 4 successful, 4 total (Test Files 410 passed (410), Tests 3158 passed | 3 skipped (3161)). One earlier full run had an unrelated flake in TimelineRowDetails.output-preview.test.tsx that passed on rerun.

Only @bb/app depends on the patched packages.

Fixes #1603

AGENT GENERATED: by Claude Opus 5

Independent verification

Checked out bb/fix-1603-mobile-remote-access (c3ed8ad, one commit on top of current origin/main, no conflicts) in a separate worktree; pnpm install --frozen-lockfile --prefer-offline applied both patches (node_modules/.pnpm/[email protected]_patch_hash=..., @[email protected]_patch_hash=...).

Root cause: agrees with the report. regex/internals (atomic.js, subclass.js, utils-internals.js) never imports regex/src/utils.js, where that package's probes live, so leaving regex unpatched is correct. @pierre/diffs inlines the probe only in dist/worker/worker-portable.js. Only apps/app consumes either package in the workspace.

Fail-before / pass-after (same test file, pnpm exec vitest run src/regex-engine-feature-probes.test.ts in apps/app):

  • before (root package.json + pnpm-lock.yaml from origin/main, pnpm install): both tests fail at bundle top level with SyntaxError: Invalid flags supplied to RegExp constructor. at bundle.cjs:48:4094 and bundle.cjs:59:4114.
  • after (PR files restored, frozen install): Tests 2 passed (2).

pnpm exec turbo run typecheck lint test --filter=@bb/app --force: Tasks: 6 successful, 6 total; Test Files 410 passed (410), Tests 3158 passed | 3 skipped (3161); lint 0 errors (155 pre-existing warnings, none in the new file).

Built output: on the PR branch grep -l "unicodeSets:!0" packages/bb-app/app/dist/assets/*.js finds nothing and a scan for RegExp(<literal>, "v") calls finds only the two unicodeSets && RegExp("[[^a]]","v") statements, now behind a real runtime probe. The unpatched rebuild still contains the folded probe in dist-DdvSMfIo.js and worker-portable-DaFcwpf8.js.

Browser repro (report's harness: Playwright 1.33.0 webkit-1837, iPhone 8 Plus emulation, init script making RegExp reject flag v like Safari 16.4-16.6) against the packaged app on isolated ports 45231/45232 with a scratch data dir:

  • unpatched build: plugin runtime load failed: Invalid flags supplied to RegExp constructor.; sidebar New thread Extensions Threads Settings; hasRemoteAccess: false, hasAutomations: false.
  • PR build: no errors or warnings beyond WebKit preload notices; sidebar New thread Extensions Automations Threads Settings Remote access; hasRemoteAccess: true, hasAutomations: true, error boundary not shown.

CI: all checks green (Checks, app-1/2/3, integration, packages, server, both Package Smoke jobs).

Residual risks: the patches are pinned to [email protected] and @pierre/[email protected]; a version bump drops them (pnpm errors on the stale patchedDependencies entry, and the new test fails on the rebuilt output). Safari 16.0-16.3 still fails on regex lookbehind literals, below Vite's safari16.4 target. The test emulates Safari 16 with a vm RegExp wrapper rather than a real WebKit build.

AGENT GENERATED: by Claude Opus 5

oniguruma-to-es (Shiki's JavaScript regex engine, pulled in through
@pierre/diffs) detects RegExp `v`-flag and modifier support at module top
level with `try { new RegExp("[[]]", "v") } catch { return false }`. Rolldown
and the oxc minifier treat `new RegExp(<literal>, <literal>)` as pure, delete
it from the try block and fold the probe to `true`, so the next statement
calls `RegExp("[[^a]]", "v")` unguarded. On Safari/iOS 16.x (no `v` flag)
that throws while the chunk evaluates: the plugin runtime fails to load, so
plugin-contributed UI such as Remote access and Automations never renders,
and any view that imports the chunk trips the app error boundary.

Patch oniguruma-to-es and the copy of it inlined in @pierre/diffs'
worker-portable.js to construct the probe RegExps through
`Reflect.construct`, which the bundler cannot prove pure. A test bundles
both entries with the app's Vite build and evaluates them on a Safari
16-like RegExp.

Co-Authored-By: Claude <[email protected]>
@SawyerHood
SawyerHood marked this pull request as ready for review August 21, 2026 07:48
@SawyerHood
SawyerHood merged commit 9dad3ae into main Aug 21, 2026
13 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1603-mobile-remote-access branch August 21, 2026 14:41
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.

Mobile Remote Access Template - UI/UX Hidden Due to Responsiveness Issues

1 participant