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

Sort dependency group keys when adding new group #11591

Merged
merged 2 commits into from
Feb 18, 2025
Merged

Conversation

jtfmumm
Copy link
Contributor

@jtfmumm jtfmumm commented Feb 18, 2025

This change keeps dependency group keys sorted when adding new ones.

If earlier dependency group keys were not sorted, we just append the new group key to avoid churn in pyproject.toml. See discussion on #11447. I've added a new snapshot test to capture this case.

Closes #11447.

However, if earlier keys were not sorted, we just append to avoid churn.
@jtfmumm jtfmumm force-pushed the jtfm/sorted-entries branch from 2ee5ea8 to 58fa23f Compare February 18, 2025 09:28
@jtfmumm jtfmumm requested a review from zanieb February 18, 2025 10:16
@@ -4826,6 +4826,112 @@ fn add_group() -> Result<()> {

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
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 add a few test cases where there are comments between the groups, to understand how they move around?

Copy link
Member

Choose a reason for hiding this comment

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

👍 this is an unfortunately frequent cause of bug reports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added two new tests

@@ -4826,6 +4826,112 @@ fn add_group() -> Result<()> {

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
Copy link
Member

Choose a reason for hiding this comment

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

Random, but it doesn't actually look like you need the filters for this snapshot. You could reduce nesting by omitting them — but I don't mind either way.

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 filters on these tests

Comment on lines +4925 to +4932
[dependency-groups]
alpha = [
"anyio==3.7.0",
]
# This is our dev group
dev = [
"anyio==3.7.0",
]
Copy link
Contributor

@T-256 T-256 Feb 18, 2025

Choose a reason for hiding this comment

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

Could sort default-groups separately and put them in top-most priority?

(it is implicitly set to tool.uv.default-groups = ["dev"])

Copy link
Member

Choose a reason for hiding this comment

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

I think I'd prefer not to do that, otherwise when you change your default groups the ordering is unsorted and we won't respect it anymore. Though it is interesting.

Copy link
Contributor

Choose a reason for hiding this comment

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

Correct, I would like to see it as a lint rule in future.

.iter()
.filter_map(|(dotted_ks, _)| dotted_ks.first())
.map(|k| k.get())
.is_sorted();
Copy link
Member

Choose a reason for hiding this comment

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

The logic we use for detecting sorted dependencies is a bit more complex. Specifically, we check whether the values are sorted case-insensitively or case-sensitively, and then respect that sort if so. We could do the same here, but I don't consider it blocking (it seems pretty rare for users to use capitalization in these identifiers, unlike in package names).

@jtfmumm jtfmumm merged commit b086437 into main Feb 18, 2025
73 checks passed
@jtfmumm jtfmumm deleted the jtfm/sorted-entries branch February 18, 2025 18:12
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.

Insert pyproject.toml [dependency-groups] entries in sorted order
4 participants