Skip to content

Unused path dependency patches don't emit warning or show up in lock file on update #12464

@alcolmenar

Description

@alcolmenar

Problem

While investigating #12419, a solution is to use the unused_patches field of the Resolve returned from resolve_ws. It turns out this field wasn't being populated for path dependency patches. As a result, an unused patch doesn't emit a warning or show up in the lock file as an unused patch during cargo update

Steps

# Cargo.toml

[workspace]
members = ["serde", "serde_derive"]

[patch.crates-io]
serde = { path = "serde" }
# serde/Cargo.toml

[package]
name = "serde"
version = "1.0.0"

[dependencies]
serde_derive = { path = "../serde_derive" }
# serde_derive/Cargo.toml

[package]
name = "serde_derive"
version = "1.0.0"

Same test case in #12419, but running cargo update instead.

Current:
No warning and lock file below:

# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "serde"
version = "1.0.0"
dependencies = [
 "serde_derive",
]

[[package]]
name = "serde_derive"
version = "1.0.0"

Expectation:
The expectation is if a patch is unused, a warning would be emitted and would show up as a patch.unused section in the lock file

The following warning is emitted:

warning: Patch `serde v1.0.0 (/Users/alcolmenar/dev/3rd/test-patch/serde)` was not used in the crate graph.
Perhaps you misspelled the source URL being patched.
Possible URLs for `[patch.<URL>]`:
    /Users/alcolmenar/dev/3rd/test-patch/serde

Cargo file:

# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "serde"
version = "1.0.0"
dependencies = [
 "serde_derive",
]

[[package]]
name = "serde_derive"
version = "1.0.0"

[[patch.unused]]
name = "serde"
version = "1.0.0"

Possible Solution(s)

Haven't really looked into how to do it but I think the errant code is here:

pub fn register_used_patches(&mut self, patches: &[Summary]) {
for summary in patches {
if !self.graph.contains(&summary.package_id()) {
self.unused_patches.push(summary.package_id())
};
}
}

Notes

No response

Version

cargo 1.73.0-nightly (45782b6b8 2023-07-05)
release: 1.73.0-nightly
commit-hash: 45782b6b8afd1da042d45c2daeec9c0744f72cc7
commit-date: 2023-07-05
host: aarch64-apple-darwin
libgit2: 1.6.4 (sys:0.17.2 vendored)
libcurl: 7.79.1 (sys:0.4.63+curl-8.1.2 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1u  30 May 2023
os: Mac OS 12.5.1 [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.A-lockfileArea: Cargo.lock issuesA-patchArea: [patch] table overrideC-bugCategory: bugE-mediumExperience: MediumS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions