Skip to content

Conversation

@javiercn
Copy link
Member

Summary

This PR fixes the ScopedCss build pipeline to properly regenerate scoped CSS files when the CssScope metadata changes.

Problem

When users specify a custom CssScope metadata on their scoped CSS files (via <None Update="..." CssScope="custom-scope" />), changing that value did not trigger regeneration of the scoped CSS output. This was because MSBuild's Inputs/Outputs incrementalism only tracks file timestamps, not item metadata like CssScope.

Fixes #50646

Solution

  1. Added _CreateScopedCssScopeCache target - Creates a cache file containing all CssScope values using WriteLinesToFile with WriteOnlyWhenDifferent="true". This file only updates when scope values actually change.

  2. Updated _ProcessScopedCssFiles target - Added the cache file to the target's Inputs, so MSBuild's incrementalism detects when scopes change.

  3. Set SkipIfOutputIsNewer="false" on RewriteCss - Since target-level incrementalism now correctly determines when to run, the task's internal timestamp check is disabled to avoid conflicts.

Testing

  • Added integration test Build_RegeneratesScopedCss_WhenCssScopeMetadataChanges that verifies:
    • Initial build uses auto-generated scope
    • Adding custom CssScope regenerates files with new scope
    • Changing CssScope value regenerates files again
    • Subsequent builds without changes remain incremental

When users specify a custom CssScope metadata on their scoped CSS files,
the build system should regenerate the scoped CSS output when that value
changes. Previously, MSBuild's Inputs/Outputs incrementalism only tracked
file timestamps, not item metadata like CssScope.

This fix:
- Adds a _CreateScopedCssScopeCache target that writes CssScope values to
  a cache file using WriteLinesToFile with WriteOnlyWhenDifferent
- Includes the cache file in _ProcessScopedCssFiles target Inputs
- Sets SkipIfOutputIsNewer=false on RewriteCss since target-level
  incrementalism now handles the decision to run

Fixes #50646
Copilot AI review requested due to automatic review settings December 24, 2025 14:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to fix the ScopedCss build pipeline to properly regenerate scoped CSS files when the CssScope metadata changes. The solution introduces a cache file mechanism to track CssScope values and integrates it with MSBuild's incrementalism.

Key Changes:

  • Added _CreateScopedCssScopeCache target to create a cache file containing CssScope metadata values
  • Updated _ProcessScopedCssFiles target to include the cache file in its Inputs attribute
  • Set SkipIfOutputIsNewer="false" on RewriteCss task to rely on target-level incrementalism
  • Added comprehensive integration test verifying the fix

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.ScopedCss.targets Adds new _CreateScopedCssScopeCache target and updates _ProcessScopedCssFiles to track CssScope metadata changes through a cache file
test/Microsoft.NET.Sdk.StaticWebAssets.Tests/ScopedCssIntegrationTests.cs Adds regression test Build_RegeneratesScopedCss_WhenCssScopeMetadataChanges that verifies files regenerate when CssScope changes and remain incremental when unchanged

Comment on lines +153 to +156
<PropertyGroup>
<_ScopedCssIntermediatePath>$([System.IO.Path]::GetFullPath($(IntermediateOutputPath)scopedcss\))</_ScopedCssIntermediatePath>
<_ScopedCssScopeCacheFile>$(_ScopedCssIntermediatePath)scopedcss.cache</_ScopedCssScopeCacheFile>
</PropertyGroup>
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

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

The property _ScopedCssScopeCacheFile is defined inside the _CreateScopedCssScopeCache target but referenced in the Inputs attribute of _ProcessScopedCssFiles. MSBuild evaluates target attributes like Inputs and Outputs during the evaluation phase before any targets execute, so _ScopedCssScopeCacheFile will be undefined when this evaluation occurs, breaking the incrementalism mechanism.

Both _ScopedCssScopeCacheFile and _ScopedCssIntermediatePath should be defined in a PropertyGroup outside of any target, similar to how _GeneratedStaticWebAssetsInputsCacheFile and _StaticWebAssetsIntermediateOutputPath are defined in Microsoft.NET.Sdk.StaticWebAssets.5_0.targets. These definitions can then be removed from within the targets to avoid redundancy.

Copilot uses AI. Check for mistakes.
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.

StaticWebAssets: ScopedCss does not regenerate on CssScope ItemGroup metadata changes

2 participants