Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
safety: throw error when we call replaceNode(…) multiple times
Other solution is to silently ignore, but that feels even more buggy.
  • Loading branch information
RobinMalfait committed Mar 19, 2025
commit 5d501bc52b9715336ba138d0bb65a0724b1e214b
3 changes: 3 additions & 0 deletions packages/tailwindcss/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ export function walk(
context,
path,
replaceWith(newNode) {
if (replacedNode) {
throw new Error('Cannot replace a node more than once')
}
Copy link
Member Author

Choose a reason for hiding this comment

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

We could also silently ignore this but then we are just hiding a bug.

Maybe we could write a better error message here that this is a Tailwind internal bug? /cc @adamwathan

Copy link
Member

Choose a reason for hiding this comment

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

Can we just not throw here? Is there something the user can do that triggers this or does it only signal a bug in Tailwind?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nothing the user can do and signals a bug in Tailwind for sure because we are the only ones using this API.

Only issue about silently ignoring is that we wouldn't immediately know about an issue. And worse, maybe some APIs start relying on this odd behavior.

If we had thrown here than the bug you ran into would be obvious but would also break everything 🫣

Copy link
Contributor

Choose a reason for hiding this comment

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

Just dropping in a suggestion here:

Cannot replace a node more than once. This is a bug in Tailwind CSS. Please open an issue with a reproduction at https://github.com/tailwindlabs/tailwindcss/issues

Copy link
Member

Choose a reason for hiding this comment

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

I feel like there are probably a million places in the code where we could add conditionals like this if we really wanted right? Feels weird to me that this would be the first time we ever introduced a throw to tell us we made a mistake. I don’t think we’ve ever done this any other time we fixed a bug.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah makes sense. I silently bailed so at least we are not mutating other AST nodes.

replacedNode = true

if (Array.isArray(newNode)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/tailwindcss/src/compat/selector-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export function walk(
visit(node, {
parent,
replaceWith(newNode) {
if (replacedNode) {
throw new Error('Cannot replace a node more than once')
}
replacedNode = true

if (Array.isArray(newNode)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/tailwindcss/src/value-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export function walk(
visit(node, {
parent,
replaceWith(newNode) {
if (replacedNode) {
throw new Error('Cannot replace a node more than once')
}
replacedNode = true

if (Array.isArray(newNode)) {
Expand Down