fix(toml): Convert warnings that license and readme files do not exist into errors

This commit is contained in:
heisen-li 2024-06-04 12:32:18 +08:00
parent 2f17770a15
commit 193319c66c
4 changed files with 45 additions and 95 deletions

View File

@ -310,6 +310,8 @@ fn build_ar_list(
}); });
} }
let mut invalid_manifest_field: Vec<String> = vec![];
let mut result = result.into_values().flatten().collect(); let mut result = result.into_values().flatten().collect();
if let Some(license_file) = &pkg.manifest().metadata().license_file { if let Some(license_file) = &pkg.manifest().metadata().license_file {
let license_path = Path::new(license_file); let license_path = Path::new(license_file);
@ -324,7 +326,12 @@ fn build_ar_list(
ws, ws,
)?; )?;
} else { } else {
warn_on_nonexistent_file(&pkg, &license_path, "license-file", &ws)?; error_on_nonexistent_file(
&pkg,
&license_path,
"license-file",
&mut invalid_manifest_field,
);
} }
} }
if let Some(readme) = &pkg.manifest().metadata().readme { if let Some(readme) = &pkg.manifest().metadata().readme {
@ -333,10 +340,14 @@ fn build_ar_list(
if abs_file_path.is_file() { if abs_file_path.is_file() {
check_for_file_and_add("readme", readme_path, abs_file_path, pkg, &mut result, ws)?; check_for_file_and_add("readme", readme_path, abs_file_path, pkg, &mut result, ws)?;
} else { } else {
warn_on_nonexistent_file(&pkg, &readme_path, "readme", &ws)?; error_on_nonexistent_file(&pkg, &readme_path, "readme", &mut invalid_manifest_field);
} }
} }
if !invalid_manifest_field.is_empty() {
return Err(anyhow::anyhow!(invalid_manifest_field.join("\n")));
}
for t in pkg for t in pkg
.manifest() .manifest()
.targets() .targets()
@ -406,25 +417,27 @@ fn check_for_file_and_add(
Ok(()) Ok(())
} }
fn warn_on_nonexistent_file( fn error_on_nonexistent_file(
pkg: &Package, pkg: &Package,
path: &Path, path: &Path,
manifest_key_name: &'static str, manifest_key_name: &'static str,
ws: &Workspace<'_>, invalid: &mut Vec<String>,
) -> CargoResult<()> { ) {
let rel_msg = if path.is_absolute() { let rel_msg = if path.is_absolute() {
"".to_string() "".to_string()
} else { } else {
format!(" (relative to `{}`)", pkg.root().display()) format!(" (relative to `{}`)", pkg.root().display())
}; };
ws.gctx().shell().warn(&format!(
let msg = format!(
"{manifest_key_name} `{}` does not appear to exist{}.\n\ "{manifest_key_name} `{}` does not appear to exist{}.\n\
Please update the {manifest_key_name} setting in the manifest at `{}`\n\ Please update the {manifest_key_name} setting in the manifest at `{}`.",
This may become a hard error in the future.",
path.display(), path.display(),
rel_msg, rel_msg,
pkg.manifest_path().display() pkg.manifest_path().display()
)) );
invalid.push(msg);
} }
fn error_custom_build_file_not_in_package( fn error_custom_build_file_not_in_package(

View File

@ -1943,8 +1943,7 @@ fn exclude_dot_files_and_directories_by_default() {
#[cargo_test] #[cargo_test]
fn empty_readme_path() { fn empty_readme_path() {
// Warn but don't fail if `readme` is empty. // fail if `readme` is empty.
// Issue #11522.
let p = project() let p = project()
.file( .file(
"Cargo.toml", "Cargo.toml",
@ -1963,13 +1962,11 @@ fn empty_readme_path() {
.build(); .build();
p.cargo("package --no-verify") p.cargo("package --no-verify")
.with_status(101)
.with_stderr( .with_stderr(
"\ "\
[WARNING] readme `` does not appear to exist (relative to `[..]/foo`). [ERROR] readme `` does not appear to exist (relative to `[..]/foo`).
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml` Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`.
This may become a hard error in the future.
[PACKAGING] foo v1.0.0 ([..]/foo)
[PACKAGED] [..] files, [..] ([..] compressed)
", ",
) )
.run(); .run();
@ -1977,8 +1974,7 @@ This may become a hard error in the future.
#[cargo_test] #[cargo_test]
fn invalid_readme_path() { fn invalid_readme_path() {
// Warn but don't fail if `readme` path is invalid. // fail if `readme` path is invalid.
// Issue #11522.
let p = project() let p = project()
.file( .file(
"Cargo.toml", "Cargo.toml",
@ -1997,13 +1993,11 @@ fn invalid_readme_path() {
.build(); .build();
p.cargo("package --no-verify") p.cargo("package --no-verify")
.with_status(101)
.with_stderr( .with_stderr(
"\ "\
[WARNING] readme `DOES-NOT-EXIST` does not appear to exist (relative to `[..]/foo`). [ERROR] readme `DOES-NOT-EXIST` does not appear to exist (relative to `[..]/foo`).
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml` Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`.
This may become a hard error in the future.
[PACKAGING] foo v1.0.0 ([..]/foo)
[PACKAGED] [..] files, [..] ([..] compressed)
", ",
) )
.run(); .run();
@ -2011,8 +2005,7 @@ This may become a hard error in the future.
#[cargo_test] #[cargo_test]
fn readme_or_license_file_is_dir() { fn readme_or_license_file_is_dir() {
// Test warning when `readme` or `license-file` is a directory, not a file. // Test error when `readme` or `license-file` is a directory, not a file.
// Issue #11522.
let p = project() let p = project()
.file( .file(
"Cargo.toml", "Cargo.toml",
@ -2031,16 +2024,13 @@ fn readme_or_license_file_is_dir() {
.build(); .build();
p.cargo("package --no-verify") p.cargo("package --no-verify")
.with_status(101)
.with_stderr( .with_stderr(
"\ "\
[WARNING] license-file `./src` does not appear to exist (relative to `[..]/foo`). [ERROR] license-file `./src` does not appear to exist (relative to `[..]/foo`).
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml` Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`.
This may become a hard error in the future. readme `./src` does not appear to exist (relative to `[..]/foo`).
[WARNING] readme `./src` does not appear to exist (relative to `[..]/foo`). Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`.
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`
This may become a hard error in the future.
[PACKAGING] foo v1.0.0 ([..]/foo)
[PACKAGED] [..] files, [..] ([..] compressed)
", ",
) )
.run(); .run();
@ -2048,8 +2038,7 @@ This may become a hard error in the future.
#[cargo_test] #[cargo_test]
fn empty_license_file_path() { fn empty_license_file_path() {
// Warn but don't fail if license-file is empty. // fail if license-file is empty.
// Issue #11522.
let p = project() let p = project()
.file( .file(
"Cargo.toml", "Cargo.toml",
@ -2067,15 +2056,13 @@ fn empty_license_file_path() {
.build(); .build();
p.cargo("package --no-verify") p.cargo("package --no-verify")
.with_status(101)
.with_stderr( .with_stderr(
"\ "\
[WARNING] manifest has no license or license-file. [WARNING] manifest has no license or license-file.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info. See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[WARNING] license-file `` does not appear to exist (relative to `[..]/foo`). [ERROR] license-file `` does not appear to exist (relative to `[..]/foo`).
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml` Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`.
This may become a hard error in the future.
[PACKAGING] foo v1.0.0 ([..]/foo)
[PACKAGED] [..] files, [..] ([..] compressed)
", ",
) )
.run(); .run();
@ -2101,13 +2088,11 @@ fn invalid_license_file_path() {
.build(); .build();
p.cargo("package --no-verify") p.cargo("package --no-verify")
.with_status(101)
.with_stderr( .with_stderr(
"\ "\
[WARNING] license-file `does-not-exist` does not appear to exist (relative to `[..]/foo`). [ERROR] license-file `does-not-exist` does not appear to exist (relative to `[..]/foo`).
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml` Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`.
This may become a hard error in the future.
[PACKAGING] foo v1.0.0 ([..]/foo)
[PACKAGED] [..] files, [..] ([..] compressed)
", ",
) )
.run(); .run();

View File

@ -3,7 +3,7 @@
use cargo_test_support::git::{self, repo}; use cargo_test_support::git::{self, repo};
use cargo_test_support::paths; use cargo_test_support::paths;
use cargo_test_support::registry::{self, Package, RegistryBuilder, Response}; use cargo_test_support::registry::{self, Package, RegistryBuilder, Response};
use cargo_test_support::{basic_manifest, no_such_file_err_msg, project, publish}; use cargo_test_support::{basic_manifest, project, publish};
use std::fs; use std::fs;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -2137,54 +2137,6 @@ include `--registry dummy-registry` or `--registry crates-io`
.run(); .run();
} }
#[cargo_test]
fn publish_with_missing_readme() {
// Use local registry for faster test times since no publish will occur
let registry = registry::init();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
authors = []
license = "MIT"
description = "foo"
homepage = "https://example.com/"
readme = "foo.md"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("publish --no-verify")
.replace_crates_io(registry.index_url())
.with_status(101)
.with_stderr(&format!(
"\
[UPDATING] [..]
[WARNING] readme `foo.md` does not appear to exist (relative to `[..]/foo`).
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`
This may become a hard error in the future.
[PACKAGING] foo v0.1.0 [..]
[PACKAGED] [..] files, [..] ([..] compressed)
[UPLOADING] foo v0.1.0 [..]
[ERROR] failed to read `readme` file for package `foo v0.1.0 ([ROOT]/foo)`
Caused by:
failed to read `[ROOT]/foo/foo.md`
Caused by:
{}
",
no_such_file_err_msg()
))
.run();
}
// Registry returns an API error. // Registry returns an API error.
#[cargo_test] #[cargo_test]
fn api_error_json() { fn api_error_json() {

View File

@ -1146,7 +1146,7 @@ fn bad_license_file(registry: &TestRegistry) {
p.cargo("publish -v") p.cargo("publish -v")
.replace_crates_io(registry.index_url()) .replace_crates_io(registry.index_url())
.with_status(101) .with_status(101)
.with_stderr_contains("[ERROR] the license file `foo` does not exist") .with_stderr_contains("[ERROR] license-file `foo` does not appear to exist ([..]).")
.run(); .run();
} }