Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-deterministic variant sorting #14835

Merged
merged 10 commits into from
Oct 31, 2024

Conversation

thecrypticace
Copy link
Contributor

Right now our variant sorting is sensitive to the authoring order of classes or the order in which we scan files because we weren't comparing variant values when sorting.

This PR addresses this by:

  • "default" values in variants appear first
  • Then any named values
  • Then any arbitrary values
  • Finally, compare the values themselves when all else is equal

We were using `peer-*/parent-name:*` instead of `peer-*/sibling-name:*`
@thecrypticace thecrypticace requested a review from a team as a code owner October 30, 2024 18:39
packages/tailwindcss/src/variants.ts Outdated Show resolved Hide resolved
Comment on lines +264 to +265
if (aValue === null) return -1
if (zValue === null) return 1
Copy link
Member

Choose a reason for hiding this comment

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

Same thing here — none of the tests in variants.test.ts fail without this code so I don't think this path is actually tested at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah this won't ever happen in pratice because:

  • In core all our functional variants return null if given no value
  • matchVariant — which does support a "default" value has a dedicated compare function

Verifying this did reveal that sorting broken there though so I'm fixing that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed and added a note explaining why this isn't actually necessary.

Copy link
Member

Choose a reason for hiding this comment

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

Removing these do result in failure of the postcss tests. I think we should figure out why they are failing there and add them to the variants.test.ts if possible.

Maybe as a dedicated sorting test at the bottom of that file?

I added SAFETY notes because a comparison function that does not return 0 on equal values can cause undefined behavior. We’re just guaranteed that this doesn’t happen.
let aValue = options?.values?.[a.value.value] ?? a.value.value
let zValue = options?.values?.[z.value.value] ?? z.value.value
let aValueKey = a.value ? a.value.value : 'DEFAULT'
let zValueKey = z.value ? z.value.value : 'DEFAULT'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@RobinMalfait I'm not 100% sold that this is the "right" fix. Thoughts?

@RobinMalfait
Copy link
Member

The @tailwindcss/postcss tests seems to be failing locally, and it looks like GitHub is not running CI on the last 2 commits.

@RobinMalfait
Copy link
Member

Fixed the changelog conflict, which in turn triggered the tests again

)
}

let classLists = permute(['is-data:flex', 'is-data-foo:flex', 'is-data-bar:flex'])
Copy link
Member

Choose a reason for hiding this comment

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

Should we add a couple of arbitrary values here as well?

CHANGELOG.md Outdated Show resolved Hide resolved
@adamwathan adamwathan merged commit 7cc2d89 into next Oct 31, 2024
1 check passed
@adamwathan adamwathan deleted the fix/v4-deterministic-variant-sorting branch October 31, 2024 14:39
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.

3 participants