Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
df97fd3
feat(bundler-plugin-core): export stampAssets, AssetStore, notify, re…
krassx Nov 23, 2026
00ceee2
feat(nextjs): withBugsee() build integration for webpack and Turbopac…
krassx Nov 23, 2026
abd7254
test(nextjs-e2e): real next build for webpack and Turbopack on Next 1…
krassx Nov 23, 2026
e6dc884
docs(nextjs): withBugsee README, source-maps design section 7.3, PROG…
krassx Nov 23, 2026
048b998
docs(source-maps): Turbopack pairs bundles and maps by pointer; share…
krassx Nov 23, 2026
57b632b
test(nextjs-e2e): run the edge-build test's Next by path (the next16 …
krassx Nov 23, 2026
119abb8
fix(nextjs): review round 1 (cache dirs, contained hook, version floo…
krassx Nov 23, 2026
c0d6d6e
Merge remote-tracking branch 'origin/feat/in-build-stamping' into fea…
krassx Nov 23, 2026
d07b6a8
Merge remote-tracking branch 'origin/feat/in-build-stamping' into fea…
krassx Nov 23, 2026
bd997b4
fix(nextjs): follow the frozen in-build stamping contract (own files …
krassx Nov 23, 2026
85a192e
fix(nextjs): stamped webpack assets as STRING sources; raw define val…
krassx Nov 23, 2026
54dbe52
Merge remote-tracking branch 'origin/feat/in-build-stamping' into fea…
krassx Nov 23, 2026
be09540
refactor(nextjs): use the core's store.check contract for non-text as…
krassx Nov 23, 2026
233617b
Merge remote-tracking branch 'origin/feat/in-build-stamping' into fea…
krassx Nov 23, 2026
3869cc7
fix(nextjs): SRI fallback guard, no public maps when no upload is pos…
krassx Nov 23, 2026
56f3a6f
fix(nextjs): no public maps Bugsee asked for when nothing is uploaded…
krassx Nov 23, 2026
68d2db8
fix(nextjs): the SRI discard path cannot hide a failure, fail the bui…
krassx Nov 23, 2026
331f4a3
fix(nextjs): round-6 lows (nothing-staged reason is reported, accurat…
krassx Nov 23, 2026
501c9f5
fix(nextjs): CI-hermetic packed test; review-thread sweep
krassx Nov 23, 2026
63b65a3
Merge remote-tracking branch 'origin/feat/in-build-stamping' into fea…
krassx Nov 23, 2026
ad1ba2d
fix(nextjs): re-check the final on-disk bundle names after webpack re…
krassx Nov 23, 2026
2ebd3d1
fix(nextjs): review threads (no-maps notice only when expected, dev-q…
krassx Nov 23, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/nextjs-with-bugsee.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@bugsee/nextjs': minor
'@bugsee/bundler-plugin-core': minor
---

`@bugsee/nextjs/config` adds `withBugsee(nextConfig, options)`: `next build` now stamps debug-IDs and uploads source maps on both webpack and Turbopack (Next 15.4.0+ for Turbopack, through `compiler.runAfterProductionCompile`). Browser maps are deleted after a confirmed upload; server and edge maps are kept. `@bugsee/bundler-plugin-core` exports `stampAssets`, `AssetStore`, `notify`, `resolveIdentity`, `loadBuildPluginEnv` and `mergeBuildPluginEnv` for it.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ reports/
# `biome check .` by the thousand.
.next/

# Fixture apps the Next source-map e2e generates and builds (packages/nextjs-e2e/test/source-maps.e2e.ts).
packages/nextjs-e2e/.fixtures/
packages/nextjs-e2e/.scratch/

# tsup writes a bundled config next to the package when tsup.config.ts needs transpiling, and leaves
# it behind if the build is interrupted. Untracked, it fails `biome check .` on a formatting rule.
tsup.config.bundled_*.mjs
Expand Down
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Runnable SDKs exist for **Node, Browser, Bun, Electron, Cloudflare/Vercel edge,
- Electron platform: `@bugsee/electron` (`docs/design/electron.md`, slices E0–E8, convergent-reviewed) — converges main (Node/`@bugsee/node`) + renderers (Chromium/`@bugsee/browser`) + native (`crashReporter` minidumps) into ONE session. Main OWNS the session; renderers stream capture UP over IPC (reusing the WebView **streaming CaptureStore** core, extracted to `@bugsee/core`) and are driven DOWN via a control channel (session handshake + pause/resume/flush/stop). Native crashes via Electron's built-in `crashReporter` (Crashpad), session-correlated `extra` (no bespoke native SDK). Visual capture D8: **rrweb DOM-replay is the DEFAULT** (rides the renderer streaming path free); **opt-in pixel video** (`video:{source}`) is a permission-gated report-time snapshot source (added the mobile-canonical `video` FileType + a generic node `reportSnapshots`/`fileEncoders` forwarding seam) supporting capturePage + MediaRecorder. Entries: `.`/`./main`/`./renderer`/`./preload`. `electron`/DOM taken as injected args — the package has **no electron/DOM dependency**.
- Extensions / umbrella: `@bugsee/performance` (APM — web-vitals + metric catalog, on-by-default via the umbrella) and `@bugsee/bugsee` (umbrella, with per-runtime browser/node `exports` conditions).
- E2E: `@bugsee/instrumentation-tests` — boots the REAL SDK in real node/bun processes against a mock collector and asserts the uploaded bundle (`pnpm test:e2e`; not in `pnpm test`).
- Build integration (source maps): **`@bugsee/nextjs/config`** `withBugsee()` — webpack (per-compilation plugin) AND Turbopack (`compiler.runAfterProductionCompile`) upload, real-Next e2e in `@bugsee/nextjs-e2e` (`docs/design/source-maps.md` §7.3).
- Framework adapters: the **per-request context foundation** (portable `RequestContext` + `ContextProvider` in core; `AsyncLocalStorage` binding in node, with `run` + `enterWith`; **correlation-by-tagging** — entries stamped with `contextId`/trace, reports merge the active context) + **`@bugsee/express`** (`requestHandler`/`errorHandler`/`setupExpress`) + **`@bugsee/fastify`** (hook-based `setupFastify`) + **`@bugsee/nestjs`** (`setupNest`: `enterWith` context middleware + a configurable error seam — default non-intrusive interceptor, opt-in global ExceptionFilter or `both` deduped; real-Nest e2e on express+fastify) + **`@bugsee/hono`/`elysia`/`hapi`/`koa`** (each structural-peer, per-framework error-capture probed before building; real-framework e2e) are built. (Restify was built then dropped — unmaintained, doesn't import on Node ≥18.) Plus **incoming-server auto-instrumentation** (`docs/design/incoming-server-instrumentation.md`): a **shared server-instrument core in `@bugsee/node`** (`server-instrument.ts` — plain values → `ServerRequestSpan`; `runServerRequest`/`openServerRequest`/split `openServerContext`+`startServerSpan`/`getActiveServerSpan`; **first-owner-wins re-entrancy** = one context + one `http.server` txn even when the http layer AND an adapter both run; only a `run`-scoped owner is refinable) auto-instruments incoming requests via the **`node:http` `Server.prototype.emit` patch** + native **`Bun.serve`** wraps, **ON BY DEFAULT** (`instrumentIncomingRequests`, default `true`; `false` opts out). **All 7 adapters were refactored onto this core** (each keeps its own `shouldReport`/route extraction). `@bugsee/server-adapters` was **retired** (engine absorbed; `openBugsee*`→`server*`). See `docs/design/framework-adapters.md` + `docs/design/incoming-server-instrumentation.md` (`generic-server-adapter.md` is superseded).
- `launch(appToken, options)` returns the started client (every platform).

Expand Down
3 changes: 3 additions & 0 deletions docs/PROGRESS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1531,3 +1531,6 @@ Verified against current majors, all green with no adapter code changes: `@bugse

### Debug-ID stamping inside the build for Vite, Rollup and Rspack; verified stamps; safe map deletion (2026-11-23)
`@bugsee/bundler-plugin-core` now stamps INSIDE the build wherever the bundler has a pre-write hook, so Subresource Integrity hashes taken later describe the stamped bytes: webpack 5 and **Rspack** at `PROCESS_ASSETS_STAGE_DEV_TOOLING + 1` (shared `tapProcessAssetsStamp`), **Vite and Rollup** in `generateBundle` (`order: 'pre'`; `rollupAssetStore` over chunks + `.map` assets; the stamped id is also put on `chunk.map` because Vite regenerates the map of a lazy-loading chunk after user hooks). esbuild and the standalone CLI stay post-emit with the SRI refusal (its message now names the workaround). Every stamp is verified (`verify-stamp.ts`: parses, sourceMappingURL intact, map debugId matches, original bytes + appended stamp): in-build all-or-nothing, post-emit snapshot + restore of the failing pair (`stamp-files.ts`); an in-build stamp is re-checked on disk (`verifyStampedPairs`) and against the HTML (`findIntegrityMismatches`). `.map` deletion now requires a confirmed upload AND a debugId on the map (`partitionByKeyed`). Real-build proof: `plugin.in-build.real-build.test.ts` (Vite 7 + 8, Rollup, Rspack with its built-in SRI plugin) and a Chromium run in `instrumentation-tests/test/sri-vite-rollup.e2e.ts`. Measured ordering limits and what the guards do not see: `docs/design/source-maps.md` §7.1.

### Next.js build integration: `withBugsee` (`@bugsee/nextjs/config`), webpack AND Turbopack (2026-11-23)
`withBugsee(nextConfig, options)` makes `next build` stamp debug-IDs and upload source maps (gap #1 of the source-map gap analysis). Design + measured limits: `docs/design/source-maps.md` §7.3; user docs: `packages/nextjs/README.md`. **webpack** (`--webpack`, default <= 15): one per-compilation plugin (client, server, edge) stamping inside the compilation (`PROCESS_ASSETS_STAGE_DEV_TOOLING + 1`, SRI-safe) and uploading exactly the files that compilation emitted (`files` mode, nothing walks `.next`). The generic `@bugsee/webpack-plugin` was NOT reused for Next: measured against a real build it cannot stamp the server compilation (assets named `../app/page.js`, output path `.next/server/chunks`) and its directory-wide upload of `.next` collides across the three compilations. **Turbopack** (default 16; `--turbopack` 15.x): `compiler.runAfterProductionCompile` (15.4.0+) runs the real `inject` + upload over `.next/static` and `.next/server`, bundles paired with maps by the `sourceMappingURL` pointer (Turbopack does not name a map after its bundle; Next 15.5 shares one map between two runtime chunks, which are left unstamped with a notice). Measured limits: Turbopack writes the SRI hashes BEFORE the hook, so an `experimental.sri` Turbopack build is left unmodified with a notice (webpack handles SRI); Next 16 errors on a `webpack` function without a `turbopack` config, so none is added on Turbopack builds; `compiler.define` (not `defineServer`) bakes the app identity. Policy: browser maps deleted after a confirmed upload, server/edge maps kept (`deleteServerMaps`). Composes with a user's `webpack`, `compiler.runAfterProductionCompile`, `compiler.define`, `productionBrowserSourceMaps`, function/async configs; inert for dev/start/export. Core change: `@bugsee/bundler-plugin-core` now also exports `stampAssets`, `AssetStore`, `notify`, `resolveIdentity`, `loadBuildPluginEnv`, `mergeBuildPluginEnv`. Found by the independent reviews and fixed: stamped webpack assets are written as STRING sources (a Buffer is opaque to webpack's RealContentHashPlugin, which renames `[contenthash]` chunks after the stamp, so every lazy chunk 404'd; the e2e now opens each build in headless Chromium and asserts the lazy chunk runs), the baked define is the RAW value (Next JSON-encodes `compiler.define` itself; a JSON string arrived double-quoted), own-files-only deletion on Turbopack, contained post-build hook, version floors (hook 15.4.0, define 15.1.0), idempotent application. Real builds (`@bugsee/nextjs-e2e`, `test/source-maps.e2e.ts`, `next` 15.5.28 and 16.4.6 x webpack/Turbopack + SRI cases, real `bugsee-cli`, mock collector, `next start` smoke): see the PR for the run.
60 changes: 59 additions & 1 deletion docs/design/source-maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,67 @@ Windows hosts were not run. Bun does not map `Error.stack` and has no

---

## 7.3 Next.js: `withBugsee` (`@bugsee/nextjs/config`)

Status: **BUILT** (`packages/nextjs/src/config/`, real-build e2e `packages/nextjs-e2e/test/source-maps.e2e.ts`).
`withBugsee(nextConfig, options)` wraps a Next config (object, phase function, async, or a promise) so
`next build` stamps debug-IDs and uploads source maps, on **both** bundlers. It takes the plugin options of §4
(through `resolvePluginOptions`, same env / `.env.bugsee-build-plugin` lookup, same identity resolution) plus
`serverSourceMaps` (default on), `deleteServerMaps` (default off) and `silent`. It never touches `next dev`
(`next start`, `next export`): a function config is gated on the phase, an object config on `dev` in the webpack
function and on the hook only existing in a production build.

**Why not the generic webpack plugin (measured).** Adding `@bugsee/webpack-plugin` in `webpack(config)` runs it
three times (client, server, edge) and breaks on Next's layout: the server compilation's `output.path` is
`.next/server/chunks` while its assets are named `../app/page.js`, so (1) the in-build stamp skipped them (an asset
name may not escape the staging directory; the verify step then failed the run) and (2) the plugin's directory-wide
upload of `.next` swept up the server compilation's files. `withBugsee` therefore ships a small per-compilation
plugin that reuses the same core (`stampAssets`, `runPluginUpload`) but stages `../` names under a stand-in
directory and uploads **exactly the files its own compilation emitted** (`files` mode; nothing walks `.next`).

**Detecting the bundler.** `process.env.TURBOPACK` (set by `next build` before the config loads, Next 15.5 and 16
alike); otherwise webpack. `NEXT_RSPACK` (the experimental Rspack adapter) is not special-cased and unverified.

| | webpack (`--webpack`; default <= 15) | Turbopack (default 16; `--turbopack` in 15.x) |
|---|---|---|
| stamp | in the compilation, `PROCESS_ASSETS_STAGE_DEV_TOOLING + 1` (SRI-safe), client + server + edge | after the build, in `compiler.runAfterProductionCompile`: the real `inject` over the written files, with the post-emit verify-and-restore |
| upload | each compilation's `afterEmit`, its own files | the hook: `.next/static` (browser) then `.next/server` (node + edge), explicit file lists |
| maps | client: `productionBrowserSourceMaps` (forced on only if unset); server/edge: `devtool: 'source-map'` unless the app set one | always emitted (browser: `productionBrowserSourceMaps`; server maps are emitted regardless of `serverSourceMaps`, measured on 16.4) |
| SRI (`experimental.sri`) | works: the hashes are of the stamped bytes (verified against the written HTML) | **not stampable**: Turbopack writes `server/subresource-integrity-manifest.json` and the HTML hashes BEFORE the hook (appending one byte in the hook made every `integrity` wrong), so the build is left unmodified, a notice says why, and nothing is uploaded |
| min Next | any with `webpack()` | 15.4.0 (the first release with `compiler.runAfterProductionCompile`: present in the 15.4.0 types, schema and build, absent in 15.3.x, checked against the published tarballs); older Turbopack builds get a notice. `compiler.define` (the baked version/build) needs 15.1.0; below that it is not added |

**Policy.** Browser maps live in `.next/static`, which `next start` and CDNs serve to every visitor: `deleteMaps`
(default true) removes them after a *confirmed* upload, exactly as the plugins do (and, as there, a failed upload
leaves them for a retry). `.next/server` is never served and a server run with source maps reads it, so server and
edge maps stay unless `deleteServerMaps`. The build is registered once (browser run). The app version and build are baked
through `compiler.define` (applies to all three runtimes; repeating the keys in `defineServer` is a Next error,
measured). Only bundles that have a map are stamped on Turbopack, and bundles are paired with maps by the
`sourceMappingURL` pointer inside each bundle: Turbopack does not name a map after its bundle (Next 16.4:
`0ihtwxitjxx9w.js` points at `1wkfrb_3m2k-z.js.map`), and Next 15.5's Turbopack points two runtime chunks at ONE map.
A map carries one debug-ID (the hash of bundle + map), so such bundles are left unstamped (a notice counts them) and
their shared map stays on disk, unuploaded; they are Turbopack's runtime, not the app.

**SRI fallback guard.** A webpack in-build stamp that failed falls back (core) to stamping the written files; on an SRI build that rewrites bytes Next already pinned (measured: Chromium blocked every script). The per-compilation plugin therefore skips the fallback and the upload when `experimental.sri` is set or Next's integrity manifest sits next to the output (`sri.ts`, shared with the Turbopack step), with a notice. `productionBrowserSourceMaps` is forced on only when this build can upload (not Turbopack + SRI, not Turbopack < 15.4.0), so an upload that cannot happen never leaves Bugsee-requested public maps. On webpack, when no upload follows (an SRI build whose stamp failed, or an abandoned stamp), the browser maps Bugsee itself turned on are deleted from this compilation's own files; maps the app enabled stay and the notice says they are public. Every removal is attempted and each failure reported through `onError` (console warning without one; `failOnError` rethrows the first); a throwing `onError` aborts like the core's. Nothing is deleted on a dry run. The reason a stamp did not apply is always reported through `onError`/console, never only through the (mutable) notice sink.

**Composition.** The app's `webpack` function runs first and its result is extended (Next itself rejects an async one, measured on 16.4.6); its
`compiler.runAfterProductionCompile` runs before ours; its `compiler.define` keys win; `productionBrowserSourceMaps`
is respected when set. Without a token the integration changes nothing Next emits.

**Open.** Both runs resolve the app identity, the token file and the installed Next from `process.cwd()` at config
load, so run `next build` from the project directory (Next passes no project dir to a config function). The
Turbopack hook does receive `metadata.projectDir` and says so when it differs from the directory the config was
resolved for (`next build <dir>` from a monorepo root); it does not re-resolve, so the baked define and the upload
identity cannot disagree. Applying `withBugsee` twice is a no-op the second time (a WeakSet of extended configs). Any
failure in the post-build hook is contained (reported, never thrown) unless `failOnError`, because Next fails the
build on anything the hook throws. A failed upload leaves the browser maps in the public `.next/static` (forced on
by `withBugsee`), and Next 15's shared Turbopack runtime map stays there too (unuploaded, not the app's code). A Turbopack SRI build
cannot be symbolicated by upload (see the table); an in-hook alternative would have to re-hash the integrity
manifests and the HTML Next writes later, which is Next's contract, not ours.

## 8. Deferred
- A real-`bugsee-cli` integration e2e (needs the built Rust binary in the harness) — follow-up once CI has it.
- Turbopack loader for Next.js (`turbopack.rules`) — Next-adapter follow-up.
- ~~Turbopack loader for Next.js (`turbopack.rules`)~~ — superseded by §7.3 (a post-build hook, not a loader: a loader
sees one module at a time and cannot stamp a finished chunk).
- Node/edge runtime debug-ID wiring (browser done in SM7) — trivial.
- The exact debug-ID WIRE FORMAT — additive ` debugId=<id>` string suffix now; a structured `debug_meta`/per-frame
field is a refinement gated on the JS-backend symbolication contract.
Expand Down
5 changes: 5 additions & 0 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
findPackageVersion,
} from './app-identity';
export { deriveBuildUuid } from './build-id';
export { loadBuildPluginEnv, mergeBuildPluginEnv } from './env-file';
export {
defaultDeleteMapFiles,
type RunFn,
Expand All @@ -36,8 +37,11 @@ export {
} from './register-build';
export {
type BugseePluginOptions,
notify,
type PluginRunResult,
type ResolvedIdentity,
type ResolvedPluginOptions,
resolveIdentity,
resolvePluginOptions,
runPluginUpload,
} from './resolve';
Expand All @@ -52,6 +56,7 @@ export {
type SpawnResult,
spawnProcess,
} from './run-cli';
export { type AssetStore, type StampAssetsOptions, stampAssets } from './stamp-assets';
export {
isWorkingTreeDirty,
type ResolveVcsMetadataOptions,
Expand Down
3 changes: 3 additions & 0 deletions packages/nextjs-e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
},
"devDependencies": {
"next": "^15.5.28",
"next16": "npm:next@16.4.6",
"playwright": "^1.64.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"tsx": "4.23.15",
"vitest": "^4.0.0"
}
}
16 changes: 11 additions & 5 deletions packages/nextjs-e2e/test/edge-build.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ describe('@bugsee/nextjs — real `next build`', () => {

beforeAll(() => {
rmSync(outDir, { recursive: true, force: true });
build = spawnSync('pnpm', ['exec', 'next', 'build'], {
cwd: appDir,
encoding: 'utf8',
env: { ...process.env, NEXT_TELEMETRY_DISABLED: '1' },
});
// The `next` binary is invoked by path: this package also installs Next 16 (as `next16`, for the source-map
// e2e), whose bin of the same name would win a `pnpm exec next`.
build = spawnSync(
process.execPath,
[join(appDir, 'node_modules', 'next', 'dist', 'bin', 'next'), 'build'],
{
cwd: appDir,
encoding: 'utf8',
env: { ...process.env, NEXT_TELEMETRY_DISABLED: '1' },
},
);
});

afterAll(() => {
Expand Down
Loading
Loading