Skip to content

fix(cli): support pre-hash esbuild sourcemap IDs - #85637

Draft
posthog[bot] wants to merge 2 commits into
masterfrom
posthog-self-driving/fixcli-warn-that-sourcemap-inject-91ba56
Draft

fix(cli): support pre-hash esbuild sourcemap IDs#85637
posthog[bot] wants to merge 2 commits into
masterfrom
posthog-self-driving/fixcli-warn-that-sourcemap-inject-91ba56

Conversation

@posthog

@posthog posthog Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Problem

posthog-cli sourcemap inject rewrites JavaScript after Angular has generated ngsw.json and after bundlers or CDNs have chosen content-hashed filenames. This invalidates service-worker versions, SRI values, and immutable cache keys.

Editing Angular's generated manifest in the CLI would couple a generic source-map tool to a framework-private format. A source-map-only ID is also insufficient unless the deployed JavaScript registers the same ID at runtime.

Fixes #86046 together with PostHog/posthog-js#4644.

Changes

  • Keep the existing warnings whenever CLI injection or --delete-after rewrites built JavaScript.
  • Add a narrow upload contract for first-party build plugins:
    • the deployed JavaScript must contain PostHog's output-filename runtime-registration marker;
    • the source-map chunk_id must exactly match the JavaScript filename;
    • normal source-injected chunk IDs remain authoritative.
  • Reject mismatched filename/map IDs.
  • Reject output-filename IDs in event release mode, which requires a resolved release ID inside JavaScript.
  • Document the @posthog/esbuild-plugin + non-mutating sourcemap upload Angular flow.
  • Keep official ngsw-config regeneration as the fallback for projects that cannot customize their builder.

Companion plugin

PostHog/posthog-js#4644 adds @posthog/esbuild-plugin. It inserts a deterministic import.meta.url runtime banner before esbuild computes output hashes, then stamps the final content-hashed filename into source-map metadata without rewriting JavaScript. Angular can safely generate ngsw.json afterward.

Verification

  • cargo test --lib (148 passed)
  • cargo test --test sourcemap (28 passed)
  • cargo clippy --lib --tests -- -D warnings
  • cargo fmt -- --check
  • Added coverage for accepted filename IDs, metadata mismatches, and unsupported event release mode.
  • Companion plugin was tested end-to-end against Angular 22 PWA output: every ngsw.json JavaScript SHA-1 matched, map IDs matched content-hashed filenames, and repeat builds were byte-identical.

Automatic notifications

  • Publish to changelog?

Docs update

CLI README updated. Public Angular source-map docs should move to the esbuild plugin workflow after both packages are released.

@trunk-io

trunk-io Bot commented Aug 19, 2026

Copy link
Copy Markdown

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@posthog

posthog Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

🦔 ReviewHog reviewed this pull request

Nothing worth raising this time, so here's a calming picture instead:

Someone relaxing in a sunny garden

@github-actions

Copy link
Copy Markdown
Contributor

🤖 CI report

Trunk lane — non-backend lane

This PR is assigned to the non-backend lane. It does not run backend Python tests and may merge in parallel with PRs in other lanes.

stamphog[bot]
stamphog Bot previously approved these changes Aug 19, 2026

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additive, contained change to the sourcemap CLI: it only adds warning logs and a README caveat when injection/cleanup rewrites built files, without altering the actual injection behavior; includes a targeted regression test. No risky territory (no data model, API, billing, auth, dependency, or CI/deploy config changes) and no unresolved review concerns.

Gate mechanics and policy version
Gate Result
prerequisites all clear
deny-list no deny categories matched
size 122L, 2F substantive, 148L/3F incl. docs/generated/snapshots — within ceiling
tier T1-agent / T1c-medium (148L, 3F, single-area, fix)
stamphog 2.0.0b4 .stamphog/policy.yml @ fcfd143 · reviewed head fcfd143

@stamphog
stamphog Bot dismissed their stale review August 25, 2026 13:36

New commits were pushed — dismissing the stamphog approval from an earlier head. This PR no longer qualifies for automatic review.

@hpouillot hpouillot changed the title fix(cli): warn that sourcemap injection rewrites built files fix(cli): keep Angular service worker hashes valid after sourcemap injection Aug 25, 2026
@hpouillot
hpouillot force-pushed the posthog-self-driving/fixcli-warn-that-sourcemap-inject-91ba56 branch from e2cf647 to 6dd3f43 Compare August 25, 2026 13:37
@marandaneto
marandaneto requested review from a team, ablaszkiewicz, cat-ph and hpouillot August 25, 2026 13:55
@marandaneto

Copy link
Copy Markdown
Member

cc @PostHog/team-error-tracking

Injection appends a chunk id (and, in event mode, a release id) to each JS chunk, then writes the file back in place. Cleanup strips sourceMappingURL comments the same way. Any content hash pinned before these steps stops matching, which silently breaks service worker manifests, Subresource Integrity, and deploy manifests.

The CLI now logs which built files it rewrote, so a pipeline can regenerate its manifest. A README caveat states the ordering rule with an Angular ngsw-config example.

Generated-By: PostHog Desktop
Task-Id: f594195d-afac-41b7-8d87-db49eb2ef661
Replace Angular manifest rewriting with a generic build-time contract. Accept a source-map-only output-filename ID when the deployed JavaScript contains the matching runtime registration marker and the map ID exactly matches the filename.

Keep normal source-injected IDs authoritative, reject mismatches, and reject output-filename IDs in event release mode. Document the esbuild plugin workflow and retain the official ngsw regeneration fallback.

Generated-By: PostHog Desktop
Task-Id: 3385093b-f0e6-46eb-9d6e-33ce42daa018
@hpouillot
hpouillot force-pushed the posthog-self-driving/fixcli-warn-that-sourcemap-inject-91ba56 branch from 52ba1b0 to 8ea5a40 Compare August 25, 2026 14:32
@hpouillot hpouillot changed the title fix(cli): keep Angular service worker hashes valid after sourcemap injection fix(cli): support pre-hash esbuild sourcemap IDs Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug report: error tracking - sourcemap inject breaks Angular service worker updates

2 participants