fix(cli): respect rsc: false and avoid adding 'use client' directive #9001
+75
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Remove Stateful Regex Bug in
transform-rsc(Fixes #8991)Problem
The
transformRscfunction uses a global regex with.test()method, which maintains state in the regex'slastIndexproperty. This causes the directive removal to alternate between working and not working on successive component installations.Symptom: When
rsc: falseis configured, components are installed inconsistently:Root Cause
On first call:
.test()returnstrue, advanceslastIndexto endOn second call:
lastIndexis at end,.test()returnsfalse(resets to 0 internally after match failure)On third call:
lastIndexis 0 again,.test()returnstrue→ Alternating behavior
Solution
Replace the stateful regex with simple string comparison:
Advantages:
Changes Made
File:
packages/shadcn/src/utils/transformers/transform-rsc.tsconst directiveRegex = /^["']use client["']$/g.trim()for whitespace handlingFile:
packages/shadcn/test/utils/transform-rsc.test.tsrsc: falsesetting and adds"use client"directive #8991)"Testing
✅ Targeted test pass:
✅ Build success:
Impact Analysis
rsc: falseis used)Before & After
Before (Broken):
After (Fixed):
Related Issue
Fixes #8991: "CLI ignores
rsc: falsesetting and adds"use client"directive"Checklist