// SM-A3 — user-facing plugin option resolution + the guarded upload entry. Merges explicit plugin options // with environment variables, applies defaults, and decides whether the plugin is active (a token is // required; a build without one is a no-op rather than an error). Pure + injectable for testing. import { type AppIdentityDeps, appIdentityDefines, type FixedIdentity, findAppIdentity, } from './app-identity'; import { collectDebugIdsOf } from './debug-ids'; import { uploadSourcemaps as defaultUploadSourcemaps, type UploadSourcemapsResult, } from './orchestrate'; import { type BundlerBuildContext, registerJsBuild as defaultRegisterJsBuild, type RegisterBuildSetting, type RegisterJsBuildResult, } from './register-build'; import type { EnvRecord } from './run-cli'; import { resolveVcsMetadata as defaultResolveVcsMetadata, type VcsMetadata } from './vcs'; /** User-facing options for the Bugsee bundler plugin. */ export interface BugseePluginOptions { /** Bugsee app token. Falls back to `BUGSEE_APP_TOKEN`. Required to upload; absent ⇒ the plugin no-ops. */ appToken?: string; /** * App version. Falls back to `BUGSEE_APP_VERSION`, then the `version` of the nearest package.json at or * above the bundler's project directory (a nearest package.json without a version means none), else * `0.0.0`. A blank or whitespace-only value counts as unset. A real value is also baked into the bundle * for the app's runtime SDK to read (not on webpack 4, which cannot bake: only an explicit value is used * there). A string `define` of `__BUGSEE_APP_VERSION__` in your own config wins for the upload too; an * expression you define makes the upload use only this explicit option / `BUGSEE_APP_VERSION`. */ appVersion?: string; /** * Build number. Falls back to `BUGSEE_APP_BUILD`, then a 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`), else `0`. A blank or * whitespace-only value counts as unset. A real value is also baked into the bundle for the app's runtime * SDK to read. */ appBuild?: string; /** API endpoint override. Falls back to `BUGSEE_ENDPOINT`. */ endpoint?: string; /** Delete client `.map`s after upload. Default `true`. */ deleteMaps?: boolean; /** Run bugsee-cli with `--dry-run` (no upload, no deletion). */ dryRun?: boolean; /** * Upload source maps WITHOUT their embedded `sourcesContent` (bugsee-cli >= 0.7.11). Default * `false`. * * The map's `sourcesContent` is your source verbatim, and it is what lets a symbolicated crash show * source lines. Stripping it keeps file/line/column resolution and drops the snippet, for teams who * would rather their code did not leave the build machine. The maps on disk are not modified. */ stripSourcesContent?: boolean; /** Disable the plugin entirely (e.g. dev builds). */ disabled?: boolean; /** * FAIL the build when the source-map upload fails. Default `false`. * * A telemetry side effect must not be able to break a production deploy: before this, an expired token, * a transient network error or a `bugsee-cli` missing from PATH aborted the user's build with no escape * hatch. Teams that would rather stop on a failed upload opt in. */ failOnError?: boolean; /** Where a contained failure is reported. Default: a console warning naming the plugin. */ onError?: (error: unknown) => void; /** * Capture the build's VCS metadata (commit SHA, branch, repo) via `bugsee-cli vcs-metadata`. * Default `true`. * * The commit is what lets the backend show a frame's ORIGINAL source when the uploaded map carries no * `sourcesContent` — it fetches the file from the repository connected to the app. Capture costs two * short-lived subprocesses (`bugsee-cli vcs-metadata` and a `git diff` dirtiness probe) per OUTPUT * DIRECTORY, and degrades to nothing at all outside a repository. */ vcs?: boolean; /** Explicit commit SHA for this build. Falls back to `BUGSEE_BUILD_COMMIT`. Overrides detection. */ commit?: string; /** Report a commit even when the working tree has uncommitted changes. Default `false`. */ allowDirtyCommit?: boolean; /** * Repository root the VCS resolver inspects. Defaults to the build's cwd, resolved lazily. It is for VCS * only: the app version is read from the BUNDLER's project directory (Vite `root`, webpack `context`, * esbuild `absWorkingDir`), so a monorepo app reports its own package.json. Pass `appVersion` to override. */ projectRoot?: string; /** * Where user-actionable NOTICES go — a malformed `commit`, a SHA dropped because the tree was dirty, * and (on `dryRun`) what was captured. Default: a `console.warn` naming the plugin. * * Separate from {@link BugseePluginOptions.onError}, which reports contained FAILURES and always * receives an `Error`. These are plain strings about configuration, not failures. */ onNotice?: (message: string) => void; /** * Register this build with Bugsee. Falls back to * `BUGSEE_REGISTER_BUILD` (`release` | `always` | `false`). Default `'release'`. * * `'release'` registers a build the BUNDLER calls production — Vite's resolved `isProduction`, * webpack's `mode: 'production'`, else `NODE_ENV=production` — which is the same default the * Android plugin and the iOS agent ship (`!isDebuggable`; a `Release*` configuration). `'always'` * registers every build; `false` none. * * Register-only: no artefact bytes are uploaded. A failure never fails the build unless * `failOnError` is set, exactly as for the source-map upload. */ registerBuild?: RegisterBuildSetting; /** * The build's `package_id`. Default: the `name` of the nearest package.json at or above the build * output (or {@link BugseePluginOptions.projectRoot}), scope included. */ packageId?: string; /** * Upload from a build the bundler calls DEVELOPMENT. Default `false` (#196 review). * * Every esbuild watch rebuild and every Rspack `mode: 'development'` build would otherwise spawn * `bugsee-cli`, upload dev bundles as symbols and delete the local `.map`s devtools rely on. Rspack states * its mode; esbuild states none, so there only `NODE_ENV=development` marks a dev build. (Vite, Rollup and * webpack are not gated by this option.) */ uploadInDevelopment?: boolean; } /** Map `BUGSEE_REGISTER_BUILD` onto the setting. An unrecognised value is NOT a setting. */ function registerBuildFromEnv(value: string | undefined): RegisterBuildSetting | undefined { switch (value) { case 'release': case 'always': return value; case 'false': return false; default: // A typo must not silently turn registration on for every debug build, or off for releases. return undefined; } } export interface ResolvedPluginOptions { /** Whether the plugin should act (a token is present and it isn't disabled). */ enabled: boolean; appToken: string; appVersion: string; appBuild: string; endpoint: string | undefined; deleteMaps: boolean; dryRun: boolean; stripSourcesContent: boolean; /** Whether a failed upload should abort the build. Default false. */ failOnError: boolean; /** Failure sink for the contained path. */ onError?: (error: unknown) => void; /** Whether to capture the build's VCS metadata. Default true. */ vcs: boolean; /** Explicit commit SHA override (unvalidated here; `resolveVcsMetadata` is the single validator). */ commit: string | undefined; /** Whether a dirty working tree may still report a commit. Default false. */ allowDirtyCommit: boolean; /** * Repository root the VCS resolver inspects; `undefined` means "the build's cwd", resolved LAZILY at * collection time. * * Deliberately not defaulted here. This function runs synchronously from `bugseeUnpluginFactory` at * config-evaluation time, outside every containment layer — not in `runPluginUpload`, not in * `uploadSourcemaps`, not gated on `failOnError`. `process.cwd()` THROWS (`ENOENT … uncwd`) when the * process's working directory has been unlinked, which a build script that recreates its own directory * really does, so calling it here would fail the build from a plugin whose whole contract is that it * cannot — and would do so even for a fully disabled plugin. */ projectRoot: string | undefined; /** Notice sink; `undefined` means the default console warning. */ onNotice: ((message: string) => void) | undefined; /** Which builds register. Default `'release'`. */ registerBuild: RegisterBuildSetting; /** Explicit `package_id`; `undefined` means "the nearest package.json". */ packageId: string | undefined; /** Whether a build the bundler calls development still uploads. Default false. */ uploadInDevelopment: boolean; /** * `define` entries that bake the app's version/build into its bundle (`__BUGSEE_APP_VERSION__`, * `__BUGSEE_APP_BUILD__`, JSON-encoded). Only values that were actually found are present — never the * `0.0.0` / `0` placeholders — and the map is empty when the plugin is `disabled`. * * Deliberately independent of the token: the constants are harmless without one, and a build that * uploads nothing still ships an app that should know its own version. `disabled: true` is the only * switch that turns this off. */ identityDefines: Record; /** NODE_ENV as the plugin was configured under — the release signal for a bundler with no mode. */ nodeEnv: string | undefined; } /** The app identity the upload / registration are labelled with, and the `define`s baked into the bundle. */ export interface ResolvedIdentity { appVersion: string; appBuild: string; identityDefines: Record; } /** * Settle the app identity from the options, the environment and the nearest package.json at or above * `root`. `root` is the BUNDLER's project directory (Vite's `config.root`, webpack's `compiler.context`, * esbuild's `absWorkingDir`), never the VCS root; without one, the working directory is used. The upload and * the baked `define`s come from this one resolution, so they cannot disagree. */ export function resolveIdentity( options: BugseePluginOptions, env: EnvRecord, root: string | undefined, deps: AppIdentityDeps = {}, fixed: FixedIdentity = {}, ): ResolvedIdentity { const found = findAppIdentity(options, env, deps, root, fixed); return { appVersion: found.version ?? '0.0.0', appBuild: found.build ?? '0', identityDefines: options.disabled ? {} : appIdentityDefines(found), }; } /** Merge plugin options with env vars, apply defaults, and decide whether the plugin is active. */ /** In-flight uploads, keyed by output directory. Entries are removed as each run settles. */ const runsByDir = new Map>(); export function resolvePluginOptions( options: BugseePluginOptions, env: EnvRecord, deps: AppIdentityDeps = {}, ): ResolvedPluginOptions { // A whitespace-only token (an `export BUGSEE_APP_TOKEN=" "` left in a CI template) is no token. const rawToken = options.appToken ?? env.BUGSEE_APP_TOKEN ?? ''; const appToken = rawToken.trim() === '' ? '' : rawToken; // The BASELINE identity, from the working directory; never throws (see `projectRoot` on // ResolvedPluginOptions). The bundler hooks refine it from the bundler's own project directory. return { enabled: !options.disabled && appToken !== '', appToken, ...resolveIdentity(options, env, undefined, deps), endpoint: options.endpoint ?? env.BUGSEE_ENDPOINT, deleteMaps: options.deleteMaps ?? true, dryRun: options.dryRun ?? false, stripSourcesContent: options.stripSourcesContent ?? false, failOnError: options.failOnError ?? false, ...(options.onError !== undefined ? { onError: options.onError } : {}), vcs: options.vcs ?? true, // Passed through RAW. `resolveCommitOverride` is the single place a commit value is validated, so a // malformed `BUGSEE_BUILD_COMMIT` is rejected once, in one place, rather than in two that can drift. commit: options.commit ?? env.BUGSEE_BUILD_COMMIT, allowDirtyCommit: options.allowDirtyCommit ?? false, projectRoot: options.projectRoot, onNotice: options.onNotice, registerBuild: options.registerBuild ?? registerBuildFromEnv(env.BUGSEE_REGISTER_BUILD) ?? 'release', packageId: options.packageId, uploadInDevelopment: options.uploadInDevelopment ?? false, nodeEnv: env.NODE_ENV, }; } /** What a plugin run reports: the source-map upload, plus the build registration when it ran. */ export type PluginRunResult = UploadSourcemapsResult & { build?: RegisterJsBuildResult }; /** Run the upload for a resolved config against a build output dir — a no-op when the plugin is disabled. */ export async function runPluginUpload( resolved: ResolvedPluginOptions, outDir: string, deps: { uploadSourcemaps?: typeof defaultUploadSourcemaps; resolveVcs?: typeof defaultResolveVcsMetadata; registerJsBuild?: typeof defaultRegisterJsBuild; /** What the bundler says about this build — its production signal and configuration name. */ bundler?: BundlerBuildContext; /** The bundles were stamped inside the build (stamp-assets.ts); skip the post-emit inject. */ preStamped?: boolean; /** * The build's own outputs (esbuild's metafile). The run then touches exactly these files, never joins another * run of the same directory, and registers from these bundles only. */ files?: { scripts: readonly string[]; maps: readonly string[]; stylesheetMaps?: readonly string[]; }; } = {}, ): Promise { if (!resolved.enabled) { return undefined; } // One pipeline per output DIRECTORY. `writeBundle` fires once per OUTPUT, so a multi-output // config whose entries resolve to the same directory — `[{ dir: 'dist' }, { file: 'dist/legacy.js' }]`, // both `dist` — used to start two concurrent pipelines over one tree, with one deleting maps // while the other was still reading them. A concurrent caller JOINS the in-flight run and // gets its result; the entry is released on settle, so `vite build --watch` still uploads every rebuild. // A run over a build's OWN files (`deps.files`) neither joins nor is joined: it touches nothing another run // of the directory touches, so there is nothing to coordinate. const inFlight = deps.files === undefined ? runsByDir.get(outDir) : undefined; if (inFlight !== undefined) { return inFlight; } const uploadSourcemaps = deps.uploadSourcemaps ?? defaultUploadSourcemaps; const registerJsBuild = deps.registerJsBuild ?? defaultRegisterJsBuild; const run = collectVcs(resolved, deps.resolveVcs).then(async (vcs): Promise => { const upload = await uploadSourcemaps({ outDir, appToken: resolved.appToken, appVersion: resolved.appVersion, appBuild: resolved.appBuild, endpoint: resolved.endpoint, deleteMaps: resolved.deleteMaps, dryRun: resolved.dryRun, stripSourcesContent: resolved.stripSourcesContent, failOnError: resolved.failOnError, ...(resolved.onError !== undefined ? { onError: resolved.onError } : {}), // Omitted, not sent empty: absence is how the backend tells "no VCS context" from "known empty". ...(vcs !== undefined ? { vcs } : {}), // Omitted unless true, so the orchestrator's argument stays exactly what it was for every // build that did not take the in-build path. ...(deps.preStamped === true ? { preStamped: true } : {}), ...(deps.files !== undefined ? { files: deps.files } : {}), }); // AFTER the upload, because the build id is derived from the debug-ids its inject step stamps // into the bundles — and regardless of whether the maps made it, because a build exists either // way. It reads the ids from the bundles, not the maps, so the map deletion above cannot race it. const build = await registerJsBuild({ outDir, appToken: resolved.appToken, appVersion: resolved.appVersion, appBuild: resolved.appBuild, endpoint: resolved.endpoint, setting: resolved.registerBuild, bundler: deps.bundler ?? {}, ...(deps.files !== undefined ? { collectDebugIds: () => collectDebugIdsOf(deps.files?.scripts ?? []) } : {}), env: resolved.nodeEnv !== undefined ? { NODE_ENV: resolved.nodeEnv } : {}, ...(resolved.packageId !== undefined ? { packageId: resolved.packageId } : {}), // The nearest package.json above the OUTPUT names the package that built it. The process cwd // does not: in a monorepo it is usually the workspace root. It is also the one input here that // cannot throw, where `process.cwd()` can (see `projectRoot` on ResolvedPluginOptions). projectRoot: resolved.projectRoot ?? outDir, ...(vcs !== undefined ? { vcs } : {}), dryRun: resolved.dryRun, failOnError: resolved.failOnError, ...(resolved.onError !== undefined ? { onError: resolved.onError } : {}), }); return { ...upload, build }; }); // The ORIGINAL promise is stored, so a joiner sees the same outcome — including the same failure. The // cleanup rides a separate, already-handled chain: storing `run.finally(…)` instead would create a // DERIVED promise that nobody awaits, and a failed run would surface as an unhandled rejection. // // `finally`, not `then`: a failed run must free the slot too, or every later build of that directory // would be blocked by a corpse. // // ORDERING NOTE (measured, not assumed): the slot is freed BEFORE a caller awaiting `run` resumes — // `await runPluginUpload(...)` observes `slot-freed -> caller-resumed`. That is what makes a // sequential caller (watch mode) find the slot free on its next call. It holds because this function // is `async` and so adds a thenable-adoption tick; the non-async variant reverses the two, leaving // the slot still held when the caller resumes. Not the pure refactor it looks like. if (deps.files !== undefined) { return run; } runsByDir.set(outDir, run); void run .catch(() => undefined) .finally(() => { runsByDir.delete(outDir); }); return run; } /** * Collect the build's VCS metadata, absorbing every failure. * * A commit SHA is a *nice-to-have* enrichment of a source-map upload; the maps themselves are the point. * So a resolver that threw must not stop the upload, let alone the build. */ async function collectVcs( resolved: ResolvedPluginOptions, resolveVcs: typeof defaultResolveVcsMetadata = defaultResolveVcsMetadata, ): Promise { if (!resolved.vcs) { // Short-circuited HERE as well as inside the resolver, so a disabled feature resolves no cwd, // spawns nothing and cannot fail for any reason at all. return undefined; } // `process.cwd()` is reached only now — inside the try, on the contained path. See the note on // `ResolvedPluginOptions.projectRoot`. let projectRoot: string; try { projectRoot = resolved.projectRoot ?? process.cwd(); } catch { return undefined; } try { const vcs = await resolveVcs({ projectRoot, enabled: true, ...(resolved.commit !== undefined ? { commit: resolved.commit } : {}), allowDirtyCommit: resolved.allowDirtyCommit, onNotice: notify(resolved), // EMPTY on purpose. The env was already consulted by `resolvePluginOptions` above, and letting // the resolver fall back to `process.env` would let an ambient BUGSEE_BUILD_COMMIT defeat an // injected env — the very duplication the note on `commit` says is being avoided. env: {}, }); if (resolved.dryRun) { // The documented way to confirm capture works. Without it a dry run prints nothing about the // VCS metadata at all, which for a CAPTURE-ONLY feature leaves a user no way to tell whether it // did anything — the plugin discards `uploadSourcemaps`' return value. notify(resolved)( vcs === undefined ? 'no VCS metadata was captured for this build' : `captured VCS metadata: ${JSON.stringify(vcs)}`, ); } return vcs; } catch { return undefined; } } /** * The plugin's notice sink: a user-actionable message, distinct from a contained FAILURE. * * Deliberately NOT `onError`. That option is documented as "where a contained failure is reported" and * everywhere else receives an `Error`, so hosts do `e.message`, `e.stack`, `e instanceof Error`, or * fail their pipeline on a non-empty list. Feeding a plain informational string about a dirty working * tree into it would break all four. */ export function notify( resolved: Pick, ): (message: string) => void { return (message) => { const text = `[bugsee] ${message}`; try { if (resolved.onNotice !== undefined) { resolved.onNotice(text); return; } console.warn(text); } catch { // GUARDED HERE, not only inside the resolver. The resolver wraps its own `notice()` calls, but // the dry-run diagnostic above calls this sink DIRECTLY, inside the try whose catch discards the // metadata — so a throwing host logger cost the caller the whole VcsMetadata object rather than // one message, which is the exact trade the resolver's contract says never to make. } }; }