Skip to content

Commit e35ecbe

Browse files
committed
Warn on unused patches.
1 parent d35f1bd commit e35ecbe

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

src/cargo/ops/resolve.rs

+24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ use crate::sources::PathSource;
1010
use crate::util::errors::{CargoResult, CargoResultExt};
1111
use crate::util::profile;
1212

13+
const UNUSED_PATCH_WARNING: &str = "\
14+
Check that the patched package version and available features are compatible
15+
with the dependency requirements. If the patch has a different version from
16+
what is locked in the Cargo.lock file, run `cargo update` to use the new
17+
version. This may also occur with an optional dependency that is not enabled.";
18+
1319
/// Resolve all dependencies for the workspace using the previous
1420
/// lockfile as a guide if present.
1521
///
@@ -334,6 +340,24 @@ pub fn resolve_with_previous<'cfg>(
334340
warn,
335341
)?;
336342
resolved.register_used_patches(registry.patches());
343+
if warn {
344+
// It would be good if this warning was more targeted and helpful
345+
// (such as showing close candidates that failed to match). However,
346+
// that's not terribly easy to do, so just show a general help
347+
// message.
348+
let warnings: Vec<String> = resolved
349+
.unused_patches()
350+
.iter()
351+
.map(|pkgid| format!("Patch `{}` was not used in the crate graph.", pkgid))
352+
.collect();
353+
if !warnings.is_empty() {
354+
ws.config().shell().warn(format!(
355+
"{}\n{}",
356+
warnings.join("\n"),
357+
UNUSED_PATCH_WARNING
358+
))?;
359+
}
360+
}
337361
if let Some(previous) = previous {
338362
resolved.merge_from(previous)?;
339363
}

tests/testsuite/patch.rs

+65-4
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ fn unused() {
232232
.with_stderr(
233233
"\
234234
[UPDATING] `[ROOT][..]` index
235+
[WARNING] Patch `bar v0.2.0 ([CWD]/bar)` was not used in the crate graph.
236+
[..]
237+
[..]
238+
[..]
239+
[..]
235240
[DOWNLOADING] crates ...
236241
[DOWNLOADED] bar v0.1.0 [..]
237242
[COMPILING] bar v0.1.0
@@ -240,7 +245,18 @@ fn unused() {
240245
",
241246
)
242247
.run();
243-
p.cargo("build").with_stderr("[FINISHED] [..]").run();
248+
p.cargo("build")
249+
.with_stderr(
250+
"\
251+
[WARNING] Patch `bar v0.2.0 ([CWD]/bar)` was not used in the crate graph.
252+
[..]
253+
[..]
254+
[..]
255+
[..]
256+
[FINISHED] [..]
257+
",
258+
)
259+
.run();
244260

245261
// unused patch should be in the lock file
246262
let mut lock = String::new();
@@ -293,6 +309,11 @@ fn unused_git() {
293309
"\
294310
[UPDATING] git repository `file://[..]`
295311
[UPDATING] `[ROOT][..]` index
312+
[WARNING] Patch `bar v0.2.0 ([..])` was not used in the crate graph.
313+
[..]
314+
[..]
315+
[..]
316+
[..]
296317
[DOWNLOADING] crates ...
297318
[DOWNLOADED] bar v0.1.0 [..]
298319
[COMPILING] bar v0.1.0
@@ -301,7 +322,18 @@ fn unused_git() {
301322
",
302323
)
303324
.run();
304-
p.cargo("build").with_stderr("[FINISHED] [..]").run();
325+
p.cargo("build")
326+
.with_stderr(
327+
"\
328+
[WARNING] Patch `bar v0.2.0 ([..])` was not used in the crate graph.
329+
[..]
330+
[..]
331+
[..]
332+
[..]
333+
[FINISHED] [..]
334+
",
335+
)
336+
.run();
305337
}
306338

307339
#[test]
@@ -419,9 +451,38 @@ fn add_ignored_patch() {
419451
));
420452

421453
p.cargo("build")
422-
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
454+
.with_stderr(
455+
"\
456+
[WARNING] Patch `bar v0.1.1 ([CWD]/bar)` was not used in the crate graph.
457+
[..]
458+
[..]
459+
[..]
460+
[..]
461+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
462+
)
463+
.run();
464+
p.cargo("build")
465+
.with_stderr(
466+
"\
467+
[WARNING] Patch `bar v0.1.1 ([CWD]/bar)` was not used in the crate graph.
468+
[..]
469+
[..]
470+
[..]
471+
[..]
472+
[FINISHED] [..]",
473+
)
474+
.run();
475+
476+
p.cargo("update").run();
477+
p.cargo("build")
478+
.with_stderr(
479+
"\
480+
[COMPILING] bar v0.1.1 ([CWD]/bar)
481+
[COMPILING] foo v0.0.1 ([CWD])
482+
[FINISHED] dev [..]
483+
",
484+
)
423485
.run();
424-
p.cargo("build").with_stderr("[FINISHED] [..]").run();
425486
}
426487

427488
#[test]

0 commit comments

Comments
 (0)