fix(browser): fix slim bundle + extension bundles crash from inconsistent property mangling#3316
Merged
dustinbyrne merged 5 commits intomainfrom Apr 1, 2026
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
12 tasks
5031030 to
2281beb
Compare
Regression tests that load the actual built module.slim.js and extension-bundles.js in a browser and exercise every extension bundle.
…ngling Without a shared nameCache, terser independently mangles _-prefixed properties to different short names in each rollup entry, causing runtime crashes when module.slim.js and extension-bundles.js are combined. Top-level variable names (vars) are reset per-entry since each ES module has its own scope. Fixes #3313
Parses source maps after every build to verify that all shared _-prefixed properties were mangled to the same short names. Fails the build with exact diagnostics if they diverge.
2281beb to
40079d7
Compare
Contributor
Prompt To Fix All With AIThis is a comment left during a code review.
Path: packages/browser/playwright/mocked/slim-bundle.spec.ts
Line: 112-441
Comment:
**Prefer parameterised tests for identical "init does not crash" cases**
Eight tests (`FeatureFlagsExtensions`, `SiteAppsExtensions`, `SessionReplayExtensions`, `ExperimentsExtensions`, `ConversationsExtensions`, `LogsExtensions`, `ProductToursExtensions`, `TracingExtensions`) share the same body and differ only in the extension variable name. Per the project's preference for parameterised tests, these should use `test.each` (or Playwright's `for...of` pattern):
```ts
for (const extName of [
'FeatureFlagsExtensions',
'SiteAppsExtensions',
'SessionReplayExtensions',
'ExperimentsExtensions',
'ConversationsExtensions',
'LogsExtensions',
'ProductToursExtensions',
'TracingExtensions',
] as const) {
test(`${extName}: init does not crash`, async ({ page }) => {
const errors: string[] = []
page.on('pageerror', (error) => errors.push(error.message))
await page.goto(SLIM_BUNDLE_URL)
await waitForSlimBundleReady(page)
const initError = await initPostHogWithExtensions(page, extName)
await page.waitForTimeout(1000)
expect(initError).toBeNull()
expect(errors).toEqual([])
})
}
```
This removes ~130 lines of duplicated code while keeping the same test coverage.
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: packages/browser/scripts/check-mangled-property-consistency.js
Line: 76-84
Comment:
**Mismatch check is permissive when sets have partial overlap**
The check passes as long as `common.length > 0` (i.e., at least one mangled name is the same across both bundles). In practice, terser guarantees each property maps to exactly one name per compilation unit, so `slim[name]` and `ext[name]` will both be single-element arrays. However, the intent reads clearer if stated directly, and the current form would silently pass if one bundle used two different mangled names for the same original property (an unlikely but detectable sign of terser misbehaviour).
Consider asserting that each set contains exactly one element and that they are equal:
```js
for (const name of shared) {
const s = slim[name] // array of mangled names seen in slim bundle
const e = ext[name] // array of mangled names seen in ext bundle
// Terser should produce exactly one mangled name per property per compilation unit.
if (s[0] !== e[0] || s.length !== 1 || e.length !== 1) {
mismatches.push({ property: name, slim: s, ext: e })
}
}
```
This would also catch the (admittedly improbable) case where a single bundle contains two different mangled names for the same property, which would indicate a broken nameCache within a single entry.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "chore: add changeset" | Re-trigger Greptile |
Contributor
marandaneto
approved these changes
Apr 1, 2026
Contributor
|
Size Change: +1.95 kB (+0.03%) Total Size: 6.65 MB
ℹ️ View Unchanged
|
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.
Problem
Using
FeatureFlagsExtensions(or any extension bundle containing feature flags) fromposthog-js/dist/extension-bundleswith the slim bundle (posthog-js/dist/module.slim) crashes duringinit():The two files are compiled as separate rollup entries. Each entry got its own independent terser
nameCache, so_-prefixed properties like_internalEventEmitterwere mangled to different short names (oevsti), crashing at runtime when the extension bundle tries to access a property on the PostHog instance from the slim bundle.This affects any extension that accesses mangled properties cross-bundle. In the current build, 21 properties diverge, breaking
FeatureFlagsExtensions,SurveysExtensions,ExperimentsExtensions,ProductToursExtensions,AnalyticsExtensions(autocapture), andAllExtensions.Fixes #3313
Changes
Share terser nameCache across rollup entries (
rollup.config.mjs)The
nameCachewas previously only passed to terser whenWRITE_MANGLED_PROPERTIES=1. Now it is always shared. This is safe because the rollup CLI processes config-array entries sequentially (for (const opts of options) { await build(opts) }inrollup/dist/bin/rollup) — entry N's mangled names are fully merged back into the cache before entry N+1 starts.A
reset-vars-name-cacheplugin clears thevars(top-level variable names) portion of the cache between entries, since each ES module has its own scope and sharing variable names across modules causesReferenceErrors.History: commit
eeb3f972in PR #1902 disabled the shared cache for normal builds, citing race conditions. The concern was that@rollup/plugin-terserdispatches work to a worker-thread pool and concurrent workers could independently assign different names. This would be a real problem if rollup processed configs in parallel, but it processes them sequentially.Post-build consistency check (
scripts/check-mangled-property-consistency.js)A static analysis script that runs on every
pnpm buildviapostbuild. It parses the source maps ofmodule.slim.jsandextension-bundles.js, extracts every_-prefixed property that was mangled (filtering to actual.propertyaccesses, ignoring local variables), and asserts every property appearing in both bundles was mangled to the same name. When the nameCache sharing is broken, it catches all 21 mismatches and fails the build with exact diagnostics.Playwright regression tests (
playwright/mocked/slim-bundle.spec.ts)15 tests that load the actual built
module.slim.jsandextension-bundles.jsin a real browser and exercise every extension bundle:FeatureFlagsExtensions,ErrorTrackingExtensions,ToolbarExtensions,SurveysExtensions,AnalyticsExtensions,SessionReplayExtensions,ExperimentsExtensions,ConversationsExtensions,LogsExtensions,ProductToursExtensions,SiteAppsExtensions,TracingExtensions, andAllExtensions.Release info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file