I'd love to have cargo add keep dependencies in sorted order, without having to pass -s every time. Would you consider adding some sort of configuration option, such as a cargo-add section in .cargo/config, to set the default for this?
Looks like we could use the metadata key: https://doc.rust-lang.org/cargo/reference/manifest.html#the-metadata-table
[package.metadata.cargo-edit]
sort = true
cargo_metadata, which is already a dependency, can parse this.
Uhh, sorry. @joshtriplett is right. Adding a [cargo-add] section in .cargo/config would be more flexible, as the metadata is only limited to a specific manifest path.
Another option to "do the right thing" by default would be to insert new items "sorted" but not change the rest of the list. This way cargo add will maintain a sorted list but wouldn't reorder crates for people who have an unsorted list.
The logic would be to insert the new item before the first item that is lexicographically greater than the new item.
The downside here would be that instead of the current behaviour of inserting at the end it would insert "randomly" for unsorted files. This is probably OK for most people but I'm sure there are some people who would find that less good then always inserting at the end.
Another less obvious heuristic is to insert sorted if the list was previously sorted, otherwise insert at the end. I think the only real benefit here is that it is a closer match to current behaviour.
@kevincox "Insert in sorted order if already sorted, otherwise insert at end" sounds great to me. Then -s is only needed if you have an unsorted list and want it sorted, and we can change its help to "sort currently unsorted dependency list".
Implemented in #421.
I can't wait until this is released 馃槈
cargo-edit 0.7.0 has been released :)
This can probably be closed then.