Repository navigation
Keep Shiki's RegExp feature probes out of the bundler's reach - #2173
Merged
Merged
Conversation
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]>
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 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 RegExpv-flag and modifier support at module top level withtry { new RegExp("[[]]", "v") } catch { return false } return true. Rolldown and the oxc minifier treatnew RegExp(<literal>, <literal>)as pure, delete it from thetryblock and fold the probe totrue. The very next statement,envFlags.unicodeSets && RegExp("[[^a]]", "v").test("a"), then runs unguarded and throwsInvalid flags supplied to RegExp constructor.on every engine without thevflag (Safari/iOS 16.4-16.6; Vite's default build target issafari16.4). The library degrades gracefully on its own; the bundler removes the guard. Neithertreeshake: falsenormoduleSideEffects: "no-treeshake"helps, because the minifier applies the same purity model.On current
mainthe 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 byplugin-frontend-*.js,BbSourceCode,TimelineFileDiffBlock,FileDiff,Fileand 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 indist/esm/index.jsanddist/cjs/index.jsconstruct the RegExp throughReflect.construct(RegExp, [...]), which the bundler cannot prove pure. Verified shapes:new RegExp(...)andRegExp(...)get folded;Reflect.construct(RegExp, ...)andnew globalThis.RegExp(...)survive.patches/@[email protected]:dist/worker/worker-portable.js(loaded bysrc/lib/diff-worker-pool.tsas a module worker) inlines its own copy ofoniguruma-to-es, so the package patch alone does not reach it. Same three-line rewrite.package.jsonregisters the two patches underpnpm.patchedDependencies;pnpm-lock.yamlpicks up the patch hashes.apps/app/package.jsonaddsoniguruma-to-esas a devDependency so the new test can bundle it directly.apps/app/src/regex-engine-feature-probes.test.ts: bundlesoniguruma-to-esand@pierre/diffs/worker/worker-portable.jswith the app's Vite/Rolldown build (minified) and evaluates the output in avmcontext whoseRegExprejects thevflag and pattern modifiers like Safari 16. Asserts that the engine loads and compiles a pattern without thevflag, and that the worker reaches itsmessagelistener registration.Deviation from the report's proposal: the report also suggests patching
[email protected]. The app reaches that package only throughregex/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/diffsandmdast-util-gfm-autolink-literal, Safari 16.0-16.3) is below Vite's declaredsafari16.4floor 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: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, thengrep -l "unicodeSets:!0" packages/bb-app/app/dist/assets/*.jsfinds nothing (before:dist-DdvSMfIo.jsandworker-portable-DaFcwpf8.js); both chunks now contain thetry{Reflect.construct(RegExp,[...])}catch{return!1}return!0probes.Browser check with the report's harness (Playwright webkit-1837, iPhone 8 Plus emulation, init script that makes the
RegExpconstructor reject flagvlike Safari 16.4-16.6) against the packaged app on isolated ports:New thread, Extensions, Threads, Settings; console:plugin runtime load failed: Invalid flags supplied to RegExp constructor.;has Remote access: false, has Automations: falseNew thread, Extensions, Automations, Threads, Settings, Remote access; no errors;has Remote access: true, has Automations: trueTurbo, run from the committed tree after the final commit:
pnpm exec turbo run typecheck lint --filter=@bb/app:Tasks: 4 successful, 4 totalpnpm 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 inTimelineRowDetails.output-preview.test.tsxthat passed on rerun.Only
@bb/appdepends on the patched packages.Fixes #1603
Independent verification
Checked out
bb/fix-1603-mobile-remote-access(c3ed8ad, one commit on top of currentorigin/main, no conflicts) in a separate worktree;pnpm install --frozen-lockfile --prefer-offlineapplied 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 importsregex/src/utils.js, where that package's probes live, so leavingregexunpatched is correct.@pierre/diffsinlines the probe only indist/worker/worker-portable.js. Onlyapps/appconsumes either package in the workspace.Fail-before / pass-after (same test file,
pnpm exec vitest run src/regex-engine-feature-probes.test.tsinapps/app):package.json+pnpm-lock.yamlfromorigin/main,pnpm install): both tests fail at bundle top level withSyntaxError: Invalid flags supplied to RegExp constructor.atbundle.cjs:48:4094andbundle.cjs:59:4114.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/*.jsfinds nothing and a scan forRegExp(<literal>, "v")calls finds only the twounicodeSets && RegExp("[[^a]]","v")statements, now behind a real runtime probe. The unpatched rebuild still contains the folded probe indist-DdvSMfIo.jsandworker-portable-DaFcwpf8.js.Browser repro (report's harness: Playwright 1.33.0 webkit-1837, iPhone 8 Plus emulation, init script making
RegExpreject flagvlike Safari 16.4-16.6) against the packaged app on isolated ports 45231/45232 with a scratch data dir:plugin runtime load failed: Invalid flags supplied to RegExp constructor.; sidebarNew thread Extensions Threads Settings;hasRemoteAccess: false, hasAutomations: false.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 stalepatchedDependenciesentry, and the new test fails on the rebuilt output). Safari 16.0-16.3 still fails on regex lookbehind literals, below Vite'ssafari16.4target. The test emulates Safari 16 with avmRegExp wrapper rather than a real WebKit build.