Skip to content

fix(compiler): prevent shimCssText from adding extra blank lines per CSS comment#67735

Merged
leonsenft merged 1 commit into
angular:mainfrom
mattlewis92:fix/shadow-css-comment-extra-lines
Mar 20, 2026
Merged

fix(compiler): prevent shimCssText from adding extra blank lines per CSS comment#67735
leonsenft merged 1 commit into
angular:mainfrom
mattlewis92:fix/shadow-css-comment-extra-lines

Conversation

@mattlewis92
Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Component sourcemaps using emulated style encapsulation were wrong due to encapsulateStyle adding new lines to CSS files which makes the sourcemap line numbers incorrect, thus clicking to navigate to sourcemaps in devtools would take you to the wrong location

Issue Number: N/A

What is the new behavior?

Component sourcemaps are correct - we patched this into our angular version and verified this does indeed fix the issue we were experiencing.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

…CSS comment

The comment placeholder restoration in `shimCssText` appended an unconditional
`+ '\n'` to each non-hash comment replacement. Because `_commentRe` does not
consume the newline that follows a comment in the source, that newline already
remains in `cssText`. The extra `'\n'` was therefore inserted on top of the
existing one, shifting every line after each comment down by one. In files with
many comments (e.g. large SCSS preambles) this shifts all subsequent CSS rules
far enough that the CSS sourcemap — generated before `shimCssText` runs —
points to completely wrong source locations in browser DevTools.

The fix is to drop the `+ '\n'`; internal newlines within a multi-line comment
are still preserved via `_newLinesRe`, and the trailing newline that follows the
comment in `cssText` is already present without any extra injection.
@pullapprove pullapprove Bot requested a review from crisbeto March 18, 2026 19:27
@angular-robot angular-robot Bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Mar 18, 2026
@ngbot ngbot Bot added this to the Backlog milestone Mar 18, 2026
@crisbeto crisbeto requested review from mattrbeck and removed request for crisbeto March 18, 2026 19:29
@mattrbeck mattrbeck requested a review from alan-agius4 March 19, 2026 22:05
@mattrbeck
Copy link
Copy Markdown
Member

@alan-agius4 Would you mind taking a look too? This change appears to make sense to me, but I'd appreciate your review too since you looked at this most recently. It appears we were adding newlines unconditionally for comments rather than just comments containing newlines, which was throwing off our line count in the sourcemaps.

@mattrbeck
Copy link
Copy Markdown
Member

@mattlewis92 Thanks for the PR, BTW!

alan-agius4

This comment was marked as outdated.

@alan-agius4 alan-agius4 added the target: patch This PR is targeted for the next patch release label Mar 20, 2026
Copy link
Copy Markdown
Contributor

@alan-agius4 alan-agius4 left a comment

Choose a reason for hiding this comment

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

I tested a bit more with this change and the line numbers do not match not using view encapsulation emulated without the additional \n.

Prior to this change
Image

With this change
Image

The source line code is on 6

Image

Can you please provide a reproduction of when the sourcemaps line numbers are not matching?

@mattlewis92
Copy link
Copy Markdown
Contributor Author

mattlewis92 commented Mar 20, 2026

@alan-agius4 that looks correct to me? The source map is meant to point to the start of the declaration, which in your example is * { on line 5.

Clicking on a declaration is meant to take you to the start of the declaration and not the first rule inside of it:
CleanShot 2026-03-20 at 09 34 27@2x

@alan-agius4
Copy link
Copy Markdown
Contributor

Oh you are right :)

Copy link
Copy Markdown
Contributor

@alan-agius4 alan-agius4 left a comment

Choose a reason for hiding this comment

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

LGTM

@alan-agius4 alan-agius4 added the action: merge The PR is ready for merge by the caretaker label Mar 20, 2026
@alan-agius4
Copy link
Copy Markdown
Contributor

@alan-agius4 alan-agius4 added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Mar 20, 2026
@leonsenft leonsenft merged commit 5a712d4 into angular:main Mar 20, 2026
23 of 25 checks passed
@leonsenft
Copy link
Copy Markdown
Contributor

This PR was merged into the repository. The changes were merged into the following branches:

@angular-automatic-lock-bot
Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Apr 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants