Skip to content

Commit ab33bc0

Browse files
committed
Add a warning if license-file points to a file that does not exist.
1 parent d6fa260 commit ab33bc0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,26 @@ fn check_metadata(pkg: &Package, config: &Config) -> CargoResult<()> {
200200
things = things
201201
))?
202202
}
203+
204+
if let Some(license_path) = &md.license_file {
205+
let license_path = Path::new(license_path);
206+
let abs_license_path = pkg.root().join(license_path);
207+
if !abs_license_path.exists() {
208+
let rel_msg = if license_path.is_absolute() {
209+
"".to_string()
210+
} else {
211+
format!(" (relative to `{}`)", pkg.root().display())
212+
};
213+
config.shell().warn(&format!(
214+
"license-file `{}` does not appear to exist{}.\n\
215+
Please update the license-file setting in the manifest at `{}`\n\
216+
This may become a hard error in the future.",
217+
license_path.display(),
218+
rel_msg,
219+
pkg.manifest_path().display()
220+
))?;
221+
}
222+
}
203223
Ok(())
204224
}
205225

tests/testsuite/package.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,3 +1445,33 @@ fn exclude_dot_files_and_directories_by_default() {
14451445
",
14461446
);
14471447
}
1448+
1449+
#[cargo_test]
1450+
fn invalid_license_file_path() {
1451+
// Test warning when license-file points to a non-existent file.
1452+
let p = project()
1453+
.file(
1454+
"Cargo.toml",
1455+
r#"
1456+
[package]
1457+
name = "foo"
1458+
version = "1.0.0"
1459+
license-file = "does-not-exist"
1460+
description = "foo"
1461+
homepage = "foo"
1462+
"#,
1463+
)
1464+
.file("src/lib.rs", "")
1465+
.build();
1466+
1467+
p.cargo("package --no-verify")
1468+
.with_stderr(
1469+
"\
1470+
[WARNING] license-file `does-not-exist` does not appear to exist (relative to `[..]/foo`).
1471+
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`
1472+
This may become a hard error in the future.
1473+
[PACKAGING] foo v1.0.0 ([..]/foo)
1474+
",
1475+
)
1476+
.run();
1477+
}

0 commit comments

Comments
 (0)