Skip to content

cargo add does not maintain sort order of dependencies table #11744

@lopopolo

Description

@lopopolo

Problem

Adding a new dependency to the [dependencies] table with cargo add does not maintain sort order.

Steps

cargo new xxx
cd xxx
cargo new --lib scolapasta-path
cargo new --lib scolapasta-string-escape

Then use this Cargo.toml:

[package]
name = "xxx"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.1.0", optional = true }
# XXX: load-bearing unused dependency.
#
# `rustyline` improperly declares its minimum version on `log` as `0.4` despite
# requiring `>=0.4.5` to compile. Link in at least the minimum version here so
# cargo pulls in at least 0.4.5, e.g. when using `-Zminimal-versions`.
#
# Upstream has not been willing to merge a patch to fix this, so hack around it
# here.
#
# See: https://github.com/kkawakam/rustyline/pull/583
log = { version = "0.4.5", optional = true }
scolapasta-path = { version = "0.1.0", path = "scolapasta-path" }
scolapasta-string-escape = { version = "0.1.0", path = "scolapasta-string-escape", default-features = false }
rustyline = { version = "10.0.0", optional = true, default-features = false }
termcolor = { version = "1.1.0", optional = true }

[workspace]
members = ["scolapasta-path", "scolapasta-string-escape"]

Then: cargo add directories and observe this diff:

diff --git i/Cargo.toml w/Cargo.toml
index 80a27ab..862e90d 100644
--- i/Cargo.toml
+++ w/Cargo.toml
@@ -22,6 +22,7 @@ scolapasta-path = { version = "0.1.0", path = "scolapasta-path" }
 scolapasta-string-escape = { version = "0.1.0", path = "scolapasta-string-escape", default-features = false }
 rustyline = { version = "10.0.0", optional = true, default-features = false }
 termcolor = { version = "1.1.0", optional = true }
+directories = "4.0.1"

 [workspace]
 members = ["scolapasta-path", "scolapasta-string-escape"]

Possible Solution(s)

No response

Notes

This ticket was created at the suggestion of @weihanglo and @epage from #11743.

Version

cargo 1.67.1 (8ecd4f20a 2023-01-10)
release: 1.67.1
commit-hash: 8ecd4f20a9efb626975ac18a016d480dc7183d9b
commit-date: 2023-01-10
host: x86_64-apple-darwin
libgit2: 1.5.0 (sys:0.16.0 vendored)
libcurl: 7.86.0 (sys:0.4.59+curl-7.86.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
os: Mac OS 13.2.1 [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions