Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 31 additions & 7 deletions docs/design/source-maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ cargo-dist under the `@bugsee` scope — the `@sentry/cli` model) already does t
the plugin option `stripSourcesContent` (default off), uploads each map without its embedded source: file,
line and column symbolication are unaffected, the snippet beside a frame is what is lost, and the maps on disk
are untouched. Same release refuses to inject into an SRI build from the CLI side too, so a user driving
`bugsee-cli` directly (Angular, esbuild, Deno) gets the guard the plugin already had.
`bugsee-cli` directly (Angular, Bun) gets the guard the plugin already had.
- **Since v0.7.10** (bugsee-cli #42) the maps upload SEVERAL AT A TIME — `--concurrency N` is a ceiling that, left
unset, scales with the batch (one per 8 maps, min 4, max 8). Measured against a mock with 50 ms of latency: 200 maps
took 23.58 s serially and 4.10 s scaled. The cap is deliberately modest because a CI box on a thin uplink is
Expand Down Expand Up @@ -120,12 +120,35 @@ debugId ⇒ server falls back to `appVersion`/`appBuild`.

## 7. Coverage across targets + map identity (analysis)

**Targets without a plugin hook (Bun's built-in bundler, Deno, `tsc`/`swc`, Angular, custom pipelines).** The plugins
**Targets without a plugin hook (Bun's built-in bundler, `tsc`/`swc`, Angular, custom pipelines).** The plugins
are conveniences, not the mechanism: `bugsee-cli` operates on a finished output dir independent of the bundler, so the
**universal path is the standalone CLI post-build step** (`sourcemaps inject` → `debug-files upload`; run before
deploy). See `docs/source-maps-usage.md §2`. To widen the zero-config plugin experience cheaply, `@bugsee/bundler-plugin-core`
also exports `bugseeRollupPlugin` / `bugseeEsbuildPlugin` / `bugseeRspackPlugin` (same core) — transitively covering
Angular 17+ (esbuild), Vite/meta-framework internals (Rollup) and Rspack/Next-webpack. Deno/Bun stay CLI-post-build.
also exports `bugseeRollupPlugin` / `bugseeEsbuildPlugin` / `bugseeRspackPlugin` (same core). Their end-of-build hooks
are Rollup `writeBundle`, esbuild `onEnd` (registered through unplugin's `esbuild.setup`; skipped on a failed build
or `write: false`, which is what tsup always sets, with a notice; the plugin sets `sourcemap: 'external'` only when it is
unset, and says so when it is `false`) and Rspack `afterEmit`, each calling
`runPluginUpload` with the same SRI guard as Vite and webpack (#184; real esbuild and Rspack builds are exercised in
`plugin.real-build.test.ts`). Rspack has no in-compilation stamp stage wired, so it takes the post-emit inject path
(an SRI build is refused, not stamped). They do **not** cover **Angular**: its builder does not accept esbuild
plugins, so Angular uses the documented CLI post-build recipe (`docs/source-maps-usage.md §2`). Next.js on webpack
uses the webpack plugin. Bun, tsup and `tsc`/`swc` stay CLI-post-build (tsup via `onSuccess`).
esbuild builds work on **per-build file lists**: the plugin sets `metafile: true` and, in `onEnd`, takes the build's own
outputs from `result.metafile`: the `.js`/`.mjs`/`.cjs` scripts and the maps OF THOSE SCRIPTS. It runs
`sourcemaps inject <scripts + their maps>` then `debug-files upload <those maps>` (200 paths per CLI call). Stylesheet
maps (`.css.map`) are withheld from the CLI (named as explicit paths they have no debug-id and fail the whole call) and
are deleted together with the script maps, only after a confirmed upload. Deleting is limited to exactly those files.
Invariants: a build only touches files it wrote, so nothing is deleted without being uploaded, nothing is uploaded
under another build's options, nested/concurrent builds never wait for each other, and there is no per-directory
state (watch mode needs none). A build that emits JavaScript the plugin cannot upload must not pass silently: under
`failOnError` it fails, otherwise it is a notice (once per message). That covers a build with scripts but no script map
(`sourcemap: 'inline'`/`false`), a result with no metafile (another plugin turned it off), and maps whose bundle has an
extension `inject` does not stamp (`outExtension: '.bundle.jsx'`). A stylesheet-only build (metafile present, no
scripts, no stray maps) is a quiet no-op. Build registration reads only the build's own bundles. (An earlier design serialised and retried uploads per directory;
four review rounds each found a new race in it, and per-build file lists remove the whole class.) Rspack/webpack
keep the directory-wide `afterEmit` path, whose delete step is limited to the maps that existed when the upload
started and fails closed if they cannot be listed; a Rspack `MultiCompiler` whose compilers share one `output.path`
keeps the webpack behaviour of joining the in-flight run for that directory.

**Identifying the correct final map (multi-layer).** The identity is the bundle's own `//# sourceMappingURL` + the
debug-ID: `inject` follows each bundle's sourceMappingURL to its declared map and stamps the SAME debug-ID into both,
Expand Down Expand Up @@ -170,9 +193,10 @@ still runs, `{injected: false, uploaded: false}`.
- **Importmap integrity** (`<script type="importmap">` whose JSON body carries an `integrity` map) —
an inline script with no `src`.
- **The standalone CLI path.** The guard lives in `@bugsee/bundler-plugin-core`, which wires
`writeBundle` for vite and rollup only. Users driving `bugsee-cli` directly — Angular 17+, esbuild,
Deno — get no guard, and Angular's `subresourceIntegrity: true` is exactly the config this protects
against. Tracked: the same check belongs in `sourcemaps inject` itself.
`writeBundle` for vite and rollup, `afterEmit` for webpack and Rspack, and `onEnd` for esbuild. Users driving
`bugsee-cli` directly (Angular 17+, Bun, `tsc`) get no guard from the plugin, but the CLI has its own as of
0.7.13: `sourcemaps inject` refuses an SRI build unless `--allow-sri` is passed. Angular's
`subresourceIntegrity: true` is exactly the config this protects against.

It does see, and refuses: `<script integrity src=…>` and `<link rel=modulepreload|preload integrity href=…>`
in any `.html` under the output directory, quoted or bare, in any attribute order, with a whitespace-padded
Expand Down
101 changes: 93 additions & 8 deletions docs/source-maps-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,89 @@ module.exports = { devtool: 'source-map', plugins: [bugseeWebpackPlugin({ appTok
```

Other bundlers are exported from `@bugsee/bundler-plugin-core` (same core, `unplugin`-based):
`bugseeRollupPlugin`, `bugseeEsbuildPlugin`, `bugseeRspackPlugin`. These transitively cover **Angular 17+**
(esbuild builder), **Vite/meta-framework internals** (Rollup), and **Rspack/Next-webpack**.
`bugseeRollupPlugin`, `bugseeEsbuildPlugin`, `bugseeRspackPlugin`. Each injects debug-IDs, uploads the maps and
deletes them at the end of the build, exactly as the Vite/webpack plugins do (Rollup: `writeBundle`; esbuild:
`onEnd`, so it works with the esbuild JS API and any tool that accepts esbuild plugins AND lets esbuild write the files; Rspack:
`afterEmit`), with the same SRI guard. With esbuild the plugin sets `sourcemap: 'external'` when you did not set `sourcemap` at all (external, so the
shipped bundle carries no `//# sourceMappingURL` comment pointing at a map that is deleted after upload; the CLI
pairs a bundle with its sibling `.map` anyway). An explicit value, including `false`, is left alone: with
`false` there are no maps to upload, and the plugin says so through `onNotice`.

**The esbuild plugin works on the files THIS build wrote.** It asks esbuild for the metafile and hands the CLI
exactly the build's own bundles and maps (in batches of 200 paths), never the directory. So a build only ever
touches files it wrote: nothing is deleted without having been uploaded, nothing is uploaded under another build's
options or another plugin instance's, builds sharing an output directory (an ESM + CJS pair, a worker sub-build,
two plugin instances) never wait for or affect each other, and watch mode keeps no state between rebuilds. Under
`failOnError`, a build whose own outputs contain no `.map` (for example `sourcemap: 'inline'`) fails, judged on that
build alone; without `failOnError` it is a notice. The same goes for a build whose result carries no metafile (another
plugin set `metafile: false`) and for maps of outputs that are not `.js`/`.mjs`/`.cjs` (an `outExtension` such as
`.bundle.jsx`, which the CLI does not stamp). A stylesheet-only build is a quiet no-op, and `.css.map` files are
never sent (they are deleted with the script maps after a successful upload). The upload never walks the output
directory (the one read-only exception is the Subresource-Integrity check, which looks at the `.html` files under it).
`outfile` and `outdir` behave alike, wherever they point.
Development builds do not upload and leave the local maps alone unless you set `uploadInDevelopment: true`:
Rspack states its mode (only `development` is skipped; `none` and `production` upload), esbuild states none, so only `NODE_ENV=development` marks a
dev build (a bare `esbuild --watch` without it uploads on every rebuild, so set it or disable the plugin there). Rspack stamps after emit rather than inside the compilation,
so an Rspack build that adds Subresource Integrity hashes is refused, not stamped (§2 covers that case).

**tsup is not covered by the esbuild plugin.** tsup always runs esbuild with `write: false` and writes the files
itself afterwards, so nothing is on disk when the plugin's end-of-build hook runs. The plugin then uploads nothing
and says so through `onNotice`. Use the CLI after the build instead, e.g. in `tsup.config.ts` (with
`sourcemap: true` and `BUGSEE_APP_TOKEN` exported):

```ts
onSuccess:
'bugsee-cli sourcemaps inject dist && ' +
'bugsee-cli debug-files upload dist --type sourcemaps ${APP_VERSION:+--version "$APP_VERSION"} ${APP_BUILD:+--build "$APP_BUILD"} && ' +
'find dist -name "*.map" -delete',
```

**Options** (all optional except a token, which may also come from env):
The `${VAR:+...}` parts leave the flag out when the variable is unset (an empty `--version ""` would be sent
otherwise). The last step deletes the maps after a successful upload, which is what the plugins do by default; the
shipped bundles keep their `//# sourceMappingURL` comment, which then points at a file that is not there
(harmless; drop the line if you want the maps to ship). The same applies to any tool that calls esbuild with
`write: false`.

For **Rspack**, set `devtool: 'source-map'`: its production default is `devtool: false`, which emits no maps, so
there is nothing to upload (the plugin says so through `onNotice`).

**What these do not cover.** **Angular's builder does not accept esbuild plugins**, so `bugseeEsbuildPlugin`
cannot be used with `ng build`: use the post-build recipe in §2. Bun's bundler, `tsc` and `swc` have no hook
either (also §2). Next.js on webpack uses the webpack plugin.

**Where the settings come from.** The lookup order is: the **plugin option**, then the **process environment**,
then the **`.env.bugsee-build-plugin`** file. The setup wizard writes the app token to that file (kept out of
version control) in your project root, as Sentry's plugin does with `.env.sentry-build-plugin`.

- *Which settings.* Only `BUGSEE_APP_TOKEN`, `BUGSEE_APP_VERSION` and `BUGSEE_APP_BUILD` are read from the file.
`BUGSEE_ENDPOINT`, `BUGSEE_CLI_PATH` and the others are environment / plugin-option only, on purpose: a file
that could set them could send every upload, token included, to another host or choose which binary runs.
(`BUGSEE_CLI_PATH` is read from the process environment only.)
- *Which file.* The nearest copy at or above the bundler's project directory (Vite `root`, webpack/Rspack
`context`, esbuild `absWorkingDir`; the working directory for Rollup, which reports none), but only up to the
repository or workspace root (a directory holding `.git` or `pnpm-workspace.yaml`). With no such root, only the
project directory itself is read. The home directory and everything above it are never read, so a stray
`~/.env.bugsee-build-plugin` cannot give a token to every project below it.
- *Format.* UTF-8 `KEY=value` lines; optional `export`, quotes, `#` comments.
- *Precedence.* The file only fills gaps: a variable the environment already carries is never overridden, and
`process.env` is never written to. A blank (whitespace-only) environment variable counts as unset, the same
rule the version and build already follow.
- *Failure.* A missing, unreadable or malformed file never fails the build; without a token the plugin no-ops. A
file that is found but unusable (unreadable, UTF-16/binary, holding none of the three keys or only empty values, or sitting in the home directory when that is your project directory) is reported once
through `onNotice`, naming the file but never its contents.
- `bugsee-cli` run by hand (§2) does NOT read this file: export `BUGSEE_APP_TOKEN` there.

**Options** (all optional except a token, which may also come from env or that file):

| option | env fallback | default | meaning |
|---|---|---|---|
| `appToken` | `BUGSEE_APP_TOKEN` | — | required to upload; absent ⇒ the plugin no-ops |
| `appToken` | `BUGSEE_APP_TOKEN` (then `.env.bugsee-build-plugin`) | — | required to upload; absent ⇒ the plugin no-ops |
| `appVersion` | `BUGSEE_APP_VERSION`, then the `version` of the nearest `package.json` at or above the bundler's project directory (Vite `root`, webpack `context`, esbuild `absWorkingDir`; a nearest manifest with no version means none) | `0.0.0` | recorded on the symbol; a fallback join key. Blank or whitespace-only values count as unset. A real value is also baked into the bundle, so the app's SDK reports it (Vite, esbuild, webpack 5, Rspack and Rollup; on webpack 4 nothing can be baked, so only an explicit value is recorded). A `define` you set for `__BUGSEE_APP_VERSION__` is used as the recorded value when it is a plain string; for an expression (or on webpack 4 with no explicit value) the recorded value is left at the default and the plugin warns through `onNotice` until you set `appVersion` too |
| `appBuild` | `BUGSEE_APP_BUILD`, then the CI commit id (`GITHUB_SHA`, `VERCEL_GIT_COMMIT_SHA`, `CI_COMMIT_SHA`, `CIRCLE_SHA1`, `BUILDKITE_COMMIT`, `SOURCE_VERSION`, `RENDER_GIT_COMMIT`, `COMMIT_REF`, `CF_PAGES_COMMIT_SHA`, `GIT_COMMIT`) | `0` | recorded on the symbol; a fallback join key. A real value is also baked into the bundle, so the app's SDK reports it (same bundler coverage and `define` rule as `appVersion`) |
| `endpoint` | `BUGSEE_ENDPOINT` | `https://api.bugsee.com` | API endpoint |
| `deleteMaps` | — | `true` | delete client `.map`s after upload (privacy) |
| `dryRun` | — | `false` | run the CLI with `--dry-run` (no upload, no delete) |
| `uploadInDevelopment` | — | `false` | esbuild / Rspack only: upload from builds the bundler calls development (see §1) |
| `disabled` | — | `false` | turn the plugin off (e.g. dev builds) |
| `vcs` | — | `true` | capture the build's commit SHA / branch (see §1.1); `false` records nothing at all, even an explicit `commit` |
| `commit` | `BUGSEE_BUILD_COMMIT` | detected | explicit commit SHA for this build |
Expand Down Expand Up @@ -96,16 +166,16 @@ vs the branch head), the tree reads clean and the recorded SHA still is not what
`commit` (or set `BUGSEE_BUILD_COMMIT`) with the SHA. An explicit value skips the dirty check. It must be
7-64 hex characters — a branch name or tag is ignored rather than sent.

## 2. Any other target — the universal CLI step (Bun, Deno, tsc/swc, Angular, no-plugin builds)
## 2. Any other target — the universal CLI step (Bun, tsup, tsc/swc, Angular, no-plugin builds)

The plugins are **not** the mechanism — `bugsee-cli` operates on a finished output directory, whatever produced
it. For toolchains without a usable plugin hook (Bun's built-in bundler, Deno, `tsc`/`swc`, custom pipelines), run
it. For toolchains without a usable plugin hook (Bun's built-in bundler, tsup, `tsc`/`swc`, custom pipelines), run
the CLI as a **post-build step**:

```bash
# 1. build with EXTERNAL source maps enabled, e.g.:
bun build ./src/index.ts --outdir dist --sourcemap=external
# deno … (emits maps) · tsc --sourceMap · ng build --source-map · esbuild --sourcemap …
# tsc --sourceMap · ng build --source-map · esbuild --sourcemap …

# 2. inject debug-IDs into the built bundles + their maps (BEFORE you deploy):
bugsee-cli sourcemaps inject ./dist
Expand All @@ -115,6 +185,21 @@ BUGSEE_APP_TOKEN=… bugsee-cli debug-files upload ./dist \
--type sourcemaps --version "$APP_VERSION" --build "$APP_BUILD"
```

**Angular (17+, `ng build` with the esbuild application builder).** There is no plugin hook, so add the CLI as a
step after the build (for example an npm script or a CI step):

```bash
ng build --source-map # or "sourceMap": true in angular.json; the output lands in dist/<project>/browser
export BUGSEE_APP_TOKEN=… # the CLI does not read .env.bugsee-build-plugin
bugsee-cli sourcemaps inject dist/<project>/browser
bugsee-cli debug-files upload dist/<project>/browser \
--type sourcemaps --version "$APP_VERSION" --build "$APP_BUILD"
```

If you enable `"subresourceIntegrity": true`, bugsee-cli 0.7.13 `inject` refuses to run (it would invalidate the
integrity hashes Angular writes into `index.html`) unless you pass `--allow-sri`, which you should only do if
the hashes are recomputed after it runs; otherwise turn that option off for builds you symbolicate.

`inject` must run **before deployment** so the shipped bundle carries the debug-ID + the `_bugseeDebugIds` runtime
stub the SDK reads. This is exactly what the plugins automate; the CLI is the same engine, just invoked by hand.

Expand All @@ -135,4 +220,4 @@ stub the SDK reads. This is exactly what the plugins automate; the CLI is the sa
(and `debug-files upload --dry-run`) to see the pairings/uploads without performing them.
- **Not covered: bytecode targets.** React-Native/Hermes compiles JS → bytecode, which drops the injected comment
and stub, so the running code reports no debug-ID. That needs a Hermes-specific compose flow and is handled by the
separate React-Native SDK, not this (web/node/bun/deno) SDK.
separate React-Native SDK, not this (web/node/bun) SDK.
Loading