Skip to content

fix(vite-plugin-angular): avoid stack overflow for large inline sourcemaps - #2796

Merged
brandonroberts merged 1 commit into
analogjs:betafrom
jjohnnystwsss:fix/2669-inline-sourcemap-stack-overflow
Nov 19, 2026
Merged

brandonroberts merged 1 commit into
analogjs:betafrom
jjohnnystwsss:fix/2669-inline-sourcemap-stack-overflow

Conversation

@jjohnnystwsss

Copy link
Copy Markdown
Contributor

PR Checklist

Closes #2669

extractInlineSourceMap (used by the build optimizer when build.sourcemap is enabled) matched the whole inline sourceMappingURL comment with one regular expression. On @angular/compiler's transformed output, which has a ~4 MiB base64 map, that String.match call threw RangeError: Maximum call stack size exceeded and failed the build (vite-plugin-angular: extractInlineSourceMap causes stack overflow for large Angular compiler sourcemaps analogjs/analog#2669).

This picks up fix(vite-plugin-angular): avoid stack overflow for large inline sourcemaps analogjs/analog#2704 by mootec. That PR was closed after its review feedback went unanswered. This PR addresses that feedback (details below).

Affected scope

  • Primary scope: vite-plugin-angular
  • Secondary scopes: none

Recommended merge strategy for maintainer [optional]

  • Squash merge
  • Rebase merge
  • Other

Commit preservation note [optional]

N/A

What is the new behavior?

extractInlineSourceMap now finds the comment with string searches instead of one regex over the payload:

  1. lastIndexOf('//# sourceMappingURL=data:application/json')
  2. trimEnd() the rest of the file and split it at the first ;base64,
  3. Check the part before ;base64, against /^(?:;charset=[^;,]+)?$/. This part is short.
  4. Reject the payload if it is empty or if /[^A-Za-z0-9+/=]/ finds a character outside the base64 alphabet. This single-character test doesn't backtrack.

It accepts and rejects the same inputs as the old regex. A map is extracted only when the comment is at the end of the file, with nothing but whitespace after the payload. The comment is removed together with one preceding \n. In every other case, the code is returned unchanged with map: null.

How this handles the review feedback on #2704:

Review point on #2704 How this PR handles it
Remove the preceding newline for terminal comments. The existing extraction tests expected sourceCode, not sourceCode + '\n'. One preceding \n is removed, matching the old \n?. The existing tests are unchanged and pass.
Only extract a terminal source-map comment. Return the code unchanged when the marker is inside a string or when content follows the comment. After ;base64,, only base64 characters and trailing whitespace are allowed. New test: the marker inside a string literal is left alone.
Recognize all JavaScript line terminators (CR, U+2028, U+2029), so that code after the comment isn't dropped. Add a regression test for each terminator. No line-end search is needed. Any non-whitespace after the payload, after any line terminator, rejects the match. New it.each test covers LF, CRLF, CR, LS and PS followed by console.log(a);. With #2704's final revision, the CR, LS and PS cases fail.
(Unverified finding) Keep the code that follows the source-map comment. Covered by the previous row: code is never truncated when something follows the comment.

On #2704, prettier:check also failed in CI. This branch passes pnpm prettier:check.

Test plan

  • nx format:check (also pnpm prettier:check for the whole repo)
  • pnpm build: not run for the whole workspace. pnpm nx build vite-plugin-angular passes, and so does the root prepare build (tools/scripts/build-release.mts).
  • pnpm test: not run for the whole workspace. pnpm nx test vite-plugin-angular passes: 66 files, 1093 tests, 8 of them new.
  • Manual verification

New spec cases in angular-build-optimizer-plugin.spec.ts:

  • a multi-megabyte inline map (about 5.6 MB base64, larger than the ~4.3 MiB in the issue) is extracted and decoded correctly
  • trailing whitespace ( \r\n) after the comment is still accepted
  • code after the comment on LF, CRLF, CR, LS or PS is kept unchanged (map: null)
  • a sourceMappingURL marker inside a string literal is kept unchanged

Manual checks:

  • Differential fuzzing: I compared the new function with the old regex on 500,000 generated inputs. The inputs mix markers, charset parameters, base64 and non-base64 characters, all line terminators, and Unicode whitespace. About 15,000 of them contain an extractable map. The code and map outputs were identical for every input.
  • Large inputs: I ran the new function on payloads of 4.26 MiB and 80 MiB. Valid maps, maps followed by trailing code, and very long ;charset= values all completed in about 8 to 275 ms, with no exceptions.
  • Lint: oxlint --quiet and eslint report no errors on the changed files.

About reproducing the crash: I couldn't reproduce the stack overflow locally. On Node 24.14.1 / macOS arm64, the old regex matched payloads of up to 80 MiB without throwing. I also tried two-byte strings, --regexp-interpret-all, and workers with 0.5 MB stacks. Because the old code doesn't fail here, I didn't add a child-process --stack-size test, since it couldn't fail. The large-payload case therefore checks that large maps still extract correctly; it doesn't reproduce the crash. In #2669, the reporter (Windows 11, Node 24.22.0) confirmed that an index-based extraction fixed their Storybook build.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

  • Locally, the vite-plugin-angular suite sometimes aborts a test worker. The error is FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal in node::cjs_lexer::Parse, and it happens in ngp-compile-plugin.spec.ts or composable-plugins.spec.ts. This also happens on unmodified beta with my Node 24.14.1 (.node-version pins 24.22.0), so it isn't caused by this change. Clean runs pass.
  • AI assistance: I prepared this PR with Claude Code (Claude Opus 5.5). It helped with the implementation, tests, fuzz and stress checks, and this description. I reviewed the diff and the test results before opening the PR.

[optional] What gif best describes this PR or how it makes you feel?

…emaps

Replace the regular expression in `extractInlineSourceMap` with
index-based string searches. Matching a multi-megabyte base64 payload
with the regex threw "Maximum call stack size exceeded" while the build
optimizer processed `@angular/compiler` with sourcemaps enabled.

Extraction accepts the same input as before: only a terminal
`//# sourceMappingURL=data:application/json[;charset=...];base64,...`
comment followed by nothing but whitespace is removed, together with one
preceding newline. Code that follows the comment after any JavaScript
line terminator, or a marker inside a string literal, is left untouched.

Closes analogjs#2669
@netlify

netlify Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for analog-blog ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 8b5f501
🔍 Latest deploy log https://app.netlify.com/projects/analog-blog/deploys/6aff126d9cc14bd62bb303ba
😎 Deploy Preview https://deploy-preview-2796--analog-blog.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for analog-app ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 8b5f501
🔍 Latest deploy log https://app.netlify.com/projects/analog-app/deploys/6aff126d399685d4704363a5
😎 Deploy Preview https://deploy-preview-2796--analog-app.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for analog-docs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 8b5f501
🔍 Latest deploy log https://app.netlify.com/projects/analog-docs/deploys/6aff126d694e9c523be2f30f
😎 Deploy Preview https://deploy-preview-2796--analog-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the scope:vite-plugin-angular Changes in @analogjs/vite-plugin-angular label Nov 19, 2026
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

extractInlineSourceMap now uses index-based parsing to locate the last inline source-map prefix. It validates the separator and payload, returns the original code with a null map when extraction is invalid, and removes and decodes a valid terminal map. New tests cover a 4 MiB map, trailing whitespace, multiple line terminators, code after the comment, and map-like text inside a string literal.

Estimated code review effort: 2 (Simple) | ~10 minutes

Severity of issue fixed: Medium

Merge Risk: Minimal · up to 8b5f5

This change replaces regex-based inline sourcemap extraction with string searches so that very large sourcemaps no longer risk a stack overflow in sourcemap-enabled builds. It keeps the previous behavior of extracting only a terminal inline map. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title uses the Conventional Commit form fix(vite-plugin-angular): with a supported package scope, and it states the main change: avoiding a stack overflow for large inline sourcemaps in extractInl…
Description check Passed The description explains the crash in extractInlineSourceMap, the regex-to-index-based parsing change, the review feedback addressed from the earlier PR, the new spec cases, and the test and fuzzing r…
Linked Issues check Passed [#2669] The issue asks that extractInlineSourceMap stop matching the multi-megabyte base64 payload with one regex. It names index-based extraction (lastIndexOf, indexOf, slice) as the verified…
Out of Scope Changes check Passed The implementation diff touches only extractInlineSourceMap in packages/vite-plugin-angular/src/lib/angular-build-optimizer-plugin.ts. The spec file changes add tests for the same function. Both a…
Full details: Title check

Explanation

The title uses the Conventional Commit form fix(vite-plugin-angular): with a supported package scope, and it states the main change: avoiding a stack overflow for large inline sourcemaps in extractInlineSourceMap.

Full details: Description check

Explanation

The description explains the crash in extractInlineSourceMap, the regex-to-index-based parsing change, the review feedback addressed from the earlier PR, the new spec cases, and the test and fuzzing results. It is clearly related to the changeset.

Full details: Linked Issues check

Explanation

[#2669] The issue asks that extractInlineSourceMap stop matching the multi-megabyte base64 payload with one regex. It names index-based extraction (lastIndexOf, indexOf, slice) as the verified fix. The reviewed diff in angular-build-optimizer-plugin.ts replaces the whole-comment String.match with lastIndexOf on the //# sourceMappingURL=data:application/json prefix, trimEnd(), and indexOf(';base64,'). The charset check uses a short regex on the prefix segment. The payload check uses a single-character class test (/[^A-Za-z0-9+/=]/), which does not backtrack. Valid terminal maps are still extracted and decoded, and the comment and one preceding \n are removed, as the issue's verification does. Other inputs return code unchanged with map: null. The PR also adds spec cases for a multi-megabyte map, trailing whitespace, line terminators, and string-literal markers. I did not execute the spec suite. The test results are author-reported, and I did not verify them.

Full details: Out of Scope Changes check

Explanation

The implementation diff touches only extractInlineSourceMap in packages/vite-plugin-angular/src/lib/angular-build-optimizer-plugin.ts. The spec file changes add tests for the same function. Both are within the primary scope of vite-plugin-angular and the linked issue. The change does not alter exported declarations, and the PR description states it is not a breaking change. The PR description's remarks on the old regex not reproducing the crash locally are context and do not add out-of-scope code.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between cd66363 and 8b5f501.

Files selected for processing: 2

  • packages/vite-plugin-angular/src/lib/angular-build-optimizer-plugin.spec.ts
  • packages/vite-plugin-angular/src/lib/angular-build-optimizer-plugin.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@nx-cloud

nx-cloud Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit 8b5f501

Command Status Duration Result
nx run-many --target e2e --projects=analog-app-... ✅ Succeeded 1m 38s View ↗
nx run-many --target test ✅ Succeeded 2m 49s View ↗
nx run-many --target build --all --exclude=astr... ✅ Succeeded 1m 47s View ↗
nx run-many --target lint --all --exclude=conte... ✅ Succeeded 1m 53s View ↗
nx-cloud record -- pnpm prettier:check ✅ Succeeded 11s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-19 15:25:13 UTC

@brandonroberts brandonroberts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM


Generated by Claude Code

@brandonroberts
brandonroberts merged commit af69897 into analogjs:beta Nov 19, 2026
17 checks passed
@brandonroberts brandonroberts mentioned this pull request Nov 19, 2026
6 of 9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope:vite-plugin-angular Changes in @analogjs/vite-plugin-angular

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vite-plugin-angular: extractInlineSourceMap causes stack overflow for large Angular compiler sourcemaps

2 participants