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

cargo add should check [dependencies] and [dependencies.*] sections separately when determining whether it was sorted #11584

Closed
figsoda opened this issue Jan 15, 2023 · 3 comments · Fixed by #11612
Assignees
Labels

Comments

@figsoda
Copy link

figsoda commented Jan 15, 2023

Problem

Here is an example Cargo.toml, I will omit the package section from now on

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

[dependencies]
rand = "*"
serde = "*"

[dependencies.cfg-if]
version = "*"

When I run cargo add libc, I get

[dependencies]
rand = "*"
serde = "*"
libc = "0.2.139"

[dependencies.cfg-if]
version = "*"

instead of

[dependencies]
libc = "0.2.139"
rand = "*"
serde = "*"

[dependencies.cfg-if]
version = "*"

since cfg-if goes after serde and cargo add thinks the dependencies are unsorted

Steps

No response

Possible Solution(s)

check the sorting of the [dependencies] section and the [dependencies.*] sections separately, the same applies for build and dev dependencies

Notes

No response

Version

cargo 1.68.0-nightly (d992ab4e9 2023-01-10)
release: 1.68.0-nightly
commit-hash: d992ab4e9034930e7809749f04077045af8f4d79
commit-date: 2023-01-10
host: x86_64-unknown-linux-gnu
libgit2: 1.5.0 (sys:0.16.0 vendored)
libcurl: 7.86.0-DEV (sys:0.4.59+curl-7.86.0 vendored ssl:OpenSSL/1.1.1q)
os: NixOS 23.5.0 [64-bit]


<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"hi-rustin"}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->
@figsoda figsoda added the C-bug Category: bug label Jan 15, 2023
@epage
Copy link
Contributor

epage commented Jan 16, 2023

For someone wanting to take this on, the relevant line is at https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/cargo_add/mod.rs#L101

table_option.map_or(true, |table| is_sorted(table.iter().map(|(name, _)| name)))

We'd basically need to add a filter(|item| !item.is_table()). The most likely corner case to deal with is the presence of dotted keys (like dep.version = "1.2.3") which just might mean we'd need to check Table::is_dotted. Longer term, I'm wondering if I should prevent Table from being dotted and only allow that on inline tables to simplify things.

@Rustin170506
Copy link
Member

@rustbot claim

@epage
Copy link
Contributor

epage commented Jan 17, 2023

Just remembered, Table::get_values is likely a simpler way of doing it even if its a bit overkill. You only really need to check the first key in each Vec and the extra overhead likely won't be an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants