Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2faa4a0

Browse files
committedMay 29, 2024
Auto merge of #13921 - heisen-li:licence_readme_warning, r=<try>
fix(toml): Convert warnings that `licence` and `readme` files do not exist into errors ### What does this PR try to resolve? In this PR: - Changed the warning to a hard error and modified the associated test function; - Removed what should have been a redundant test function:`publish::publish_with_missing_readme`; - Since `cargo publish` is preceded by the execution of `cargo package`, the error message in the test `function bad_license_file` needs to be modified. issue: #13629 (comment). ### Additional information It seems that this is not enough, the current situation is that `cargo package` warns if `package.readme` is an empty string or the wrong file location, but if I cancel `package.readme`, no warning is generated. I'm wondering if I should judge `package.readme&licence` when executing `cargo package` and return an error if it doesn't exist? As this has not been done before, your advice is sought.
2 parents cbc12a2 + 5c35e29 commit 2faa4a0

File tree

4 files changed

+49
-106
lines changed

4 files changed

+49
-106
lines changed
 

‎src/cargo/ops/cargo_package.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ fn build_ar_list(
310310
});
311311
}
312312

313+
let mut invalid_manifest_field: Vec<String> = vec![];
314+
313315
let mut result = result.into_values().flatten().collect();
314316
if let Some(license_file) = &pkg.manifest().metadata().license_file {
315317
let license_path = Path::new(license_file);
@@ -324,7 +326,12 @@ fn build_ar_list(
324326
ws,
325327
)?;
326328
} else {
327-
warn_on_nonexistent_file(&pkg, &license_path, "license-file", &ws)?;
329+
error_on_nonexistent_file(
330+
&pkg,
331+
&license_path,
332+
"license-file",
333+
&mut invalid_manifest_field,
334+
);
328335
}
329336
}
330337
if let Some(readme) = &pkg.manifest().metadata().readme {
@@ -333,10 +340,14 @@ fn build_ar_list(
333340
if abs_file_path.is_file() {
334341
check_for_file_and_add("readme", readme_path, abs_file_path, pkg, &mut result, ws)?;
335342
} else {
336-
warn_on_nonexistent_file(&pkg, &readme_path, "readme", &ws)?;
343+
error_on_nonexistent_file(&pkg, &readme_path, "readme", &mut invalid_manifest_field);
337344
}
338345
}
339346

347+
if !invalid_manifest_field.is_empty() {
348+
return Err(anyhow::anyhow!(invalid_manifest_field.join("\n")));
349+
}
350+
340351
for t in pkg
341352
.manifest()
342353
.targets()
@@ -406,25 +417,27 @@ fn check_for_file_and_add(
406417
Ok(())
407418
}
408419

409-
fn warn_on_nonexistent_file(
420+
fn error_on_nonexistent_file(
410421
pkg: &Package,
411422
path: &Path,
412423
manifest_key_name: &'static str,
413-
ws: &Workspace<'_>,
414-
) -> CargoResult<()> {
424+
invalid: &mut Vec<String>,
425+
) {
415426
let rel_msg = if path.is_absolute() {
416427
"".to_string()
417428
} else {
418429
format!(" (relative to `{}`)", pkg.root().display())
419430
};
420-
ws.gctx().shell().warn(&format!(
431+
432+
let msg = format!(
421433
"{manifest_key_name} `{}` does not appear to exist{}.\n\
422-
Please update the {manifest_key_name} setting in the manifest at `{}`\n\
423-
This may become a hard error in the future.",
434+
Please update the {manifest_key_name} setting in the manifest at `{}`.",
424435
path.display(),
425436
rel_msg,
426437
pkg.manifest_path().display()
427-
))
438+
);
439+
440+
invalid.push(msg);
428441
}
429442

430443
fn error_custom_build_file_not_in_package(
@@ -500,11 +513,7 @@ fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
500513
)*
501514
}}
502515
}
503-
lacking!(
504-
description,
505-
license || license_file,
506-
documentation || homepage || repository
507-
);
516+
lacking!(description, documentation || homepage || repository);
508517

509518
if !missing.is_empty() {
510519
let mut things = missing[..missing.len() - 1].join(", ");

‎tests/testsuite/package.rs

+24-42
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ fn metadata_warning() {
8585
p.cargo("package")
8686
.with_stderr(
8787
"\
88-
warning: manifest has no description, license, license-file, documentation, \
89-
homepage or repository.
88+
warning: manifest has no description, documentation, homepage or repository.
9089
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
9190
[PACKAGING] foo v0.0.1 ([CWD])
9291
[VERIFYING] foo v0.0.1 ([CWD])
@@ -1943,8 +1942,7 @@ fn exclude_dot_files_and_directories_by_default() {
19431942

19441943
#[cargo_test]
19451944
fn empty_readme_path() {
1946-
// Warn but don't fail if `readme` is empty.
1947-
// Issue #11522.
1945+
// fail if `readme` is empty.
19481946
let p = project()
19491947
.file(
19501948
"Cargo.toml",
@@ -1963,22 +1961,19 @@ fn empty_readme_path() {
19631961
.build();
19641962

19651963
p.cargo("package --no-verify")
1964+
.with_status(101)
19661965
.with_stderr(
19671966
"\
1968-
[WARNING] readme `` does not appear to exist (relative to `[..]/foo`).
1969-
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`
1970-
This may become a hard error in the future.
1971-
[PACKAGING] foo v1.0.0 ([..]/foo)
1972-
[PACKAGED] [..] files, [..] ([..] compressed)
1967+
[ERROR] readme `` does not appear to exist (relative to `[..]/foo`).
1968+
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`.
19731969
",
19741970
)
19751971
.run();
19761972
}
19771973

19781974
#[cargo_test]
19791975
fn invalid_readme_path() {
1980-
// Warn but don't fail if `readme` path is invalid.
1981-
// Issue #11522.
1976+
// fail if `readme` path is invalid.
19821977
let p = project()
19831978
.file(
19841979
"Cargo.toml",
@@ -1997,22 +1992,19 @@ fn invalid_readme_path() {
19971992
.build();
19981993

19991994
p.cargo("package --no-verify")
1995+
.with_status(101)
20001996
.with_stderr(
20011997
"\
2002-
[WARNING] readme `DOES-NOT-EXIST` does not appear to exist (relative to `[..]/foo`).
2003-
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`
2004-
This may become a hard error in the future.
2005-
[PACKAGING] foo v1.0.0 ([..]/foo)
2006-
[PACKAGED] [..] files, [..] ([..] compressed)
1998+
[ERROR] readme `DOES-NOT-EXIST` does not appear to exist (relative to `[..]/foo`).
1999+
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`.
20072000
",
20082001
)
20092002
.run();
20102003
}
20112004

20122005
#[cargo_test]
20132006
fn readme_or_license_file_is_dir() {
2014-
// Test warning when `readme` or `license-file` is a directory, not a file.
2015-
// Issue #11522.
2007+
// Test error when `readme` or `license-file` is a directory, not a file.
20162008
let p = project()
20172009
.file(
20182010
"Cargo.toml",
@@ -2031,25 +2023,21 @@ fn readme_or_license_file_is_dir() {
20312023
.build();
20322024

20332025
p.cargo("package --no-verify")
2026+
.with_status(101)
20342027
.with_stderr(
20352028
"\
2036-
[WARNING] license-file `./src` does not appear to exist (relative to `[..]/foo`).
2037-
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`
2038-
This may become a hard error in the future.
2039-
[WARNING] readme `./src` does not appear to exist (relative to `[..]/foo`).
2040-
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`
2041-
This may become a hard error in the future.
2042-
[PACKAGING] foo v1.0.0 ([..]/foo)
2043-
[PACKAGED] [..] files, [..] ([..] compressed)
2029+
[ERROR] license-file `./src` does not appear to exist (relative to `[..]/foo`).
2030+
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`.
2031+
readme `./src` does not appear to exist (relative to `[..]/foo`).
2032+
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`.
20442033
",
20452034
)
20462035
.run();
20472036
}
20482037

20492038
#[cargo_test]
20502039
fn empty_license_file_path() {
2051-
// Warn but don't fail if license-file is empty.
2052-
// Issue #11522.
2040+
// fail if license-file is empty.
20532041
let p = project()
20542042
.file(
20552043
"Cargo.toml",
@@ -2067,15 +2055,11 @@ fn empty_license_file_path() {
20672055
.build();
20682056

20692057
p.cargo("package --no-verify")
2058+
.with_status(101)
20702059
.with_stderr(
20712060
"\
2072-
[WARNING] manifest has no license or license-file.
2073-
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
2074-
[WARNING] license-file `` does not appear to exist (relative to `[..]/foo`).
2075-
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`
2076-
This may become a hard error in the future.
2077-
[PACKAGING] foo v1.0.0 ([..]/foo)
2078-
[PACKAGED] [..] files, [..] ([..] compressed)
2061+
[ERROR] license-file `` does not appear to exist (relative to `[..]/foo`).
2062+
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`.
20792063
",
20802064
)
20812065
.run();
@@ -2101,13 +2085,11 @@ fn invalid_license_file_path() {
21012085
.build();
21022086

21032087
p.cargo("package --no-verify")
2088+
.with_status(101)
21042089
.with_stderr(
21052090
"\
2106-
[WARNING] license-file `does-not-exist` does not appear to exist (relative to `[..]/foo`).
2107-
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`
2108-
This may become a hard error in the future.
2109-
[PACKAGING] foo v1.0.0 ([..]/foo)
2110-
[PACKAGED] [..] files, [..] ([..] compressed)
2091+
[ERROR] license-file `does-not-exist` does not appear to exist (relative to `[..]/foo`).
2092+
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`.
21112093
",
21122094
)
21132095
.run();
@@ -3522,7 +3504,7 @@ fn versionless_package() {
35223504
p.cargo("package")
35233505
.with_stderr(
35243506
"\
3525-
warning: manifest has no license, license-file, documentation, homepage or repository.
3507+
warning: manifest has no documentation, homepage or repository.
35263508
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
35273509
Packaging foo v0.0.0 ([CWD])
35283510
Verifying foo v0.0.0 ([CWD])
@@ -3728,7 +3710,7 @@ fn symlink_manifest_path() {
37283710
.arg(foo_symlink.join("Cargo.toml"))
37293711
.with_stderr(
37303712
"\
3731-
warning: manifest has no description, license, license-file, documentation, homepage or repository.
3713+
warning: manifest has no description, documentation, homepage or repository.
37323714
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
37333715
[PACKAGING] foo v1.0.0 ([..]foo-symlink)
37343716
[PACKAGED] 6 files[..]

‎tests/testsuite/publish.rs

+1-49
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use cargo_test_support::git::{self, repo};
44
use cargo_test_support::paths;
55
use cargo_test_support::registry::{self, Package, RegistryBuilder, Response};
6-
use cargo_test_support::{basic_manifest, no_such_file_err_msg, project, publish};
6+
use cargo_test_support::{basic_manifest, project, publish};
77
use std::fs;
88
use std::sync::{Arc, Mutex};
99

@@ -2137,54 +2137,6 @@ include `--registry dummy-registry` or `--registry crates-io`
21372137
.run();
21382138
}
21392139

2140-
#[cargo_test]
2141-
fn publish_with_missing_readme() {
2142-
// Use local registry for faster test times since no publish will occur
2143-
let registry = registry::init();
2144-
2145-
let p = project()
2146-
.file(
2147-
"Cargo.toml",
2148-
r#"
2149-
[package]
2150-
name = "foo"
2151-
version = "0.1.0"
2152-
edition = "2015"
2153-
authors = []
2154-
license = "MIT"
2155-
description = "foo"
2156-
homepage = "https://example.com/"
2157-
readme = "foo.md"
2158-
"#,
2159-
)
2160-
.file("src/lib.rs", "")
2161-
.build();
2162-
2163-
p.cargo("publish --no-verify")
2164-
.replace_crates_io(registry.index_url())
2165-
.with_status(101)
2166-
.with_stderr(&format!(
2167-
"\
2168-
[UPDATING] [..]
2169-
[WARNING] readme `foo.md` does not appear to exist (relative to `[..]/foo`).
2170-
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`
2171-
This may become a hard error in the future.
2172-
[PACKAGING] foo v0.1.0 [..]
2173-
[PACKAGED] [..] files, [..] ([..] compressed)
2174-
[UPLOADING] foo v0.1.0 [..]
2175-
[ERROR] failed to read `readme` file for package `foo v0.1.0 ([ROOT]/foo)`
2176-
2177-
Caused by:
2178-
failed to read `[ROOT]/foo/foo.md`
2179-
2180-
Caused by:
2181-
{}
2182-
",
2183-
no_such_file_err_msg()
2184-
))
2185-
.run();
2186-
}
2187-
21882140
// Registry returns an API error.
21892141
#[cargo_test]
21902142
fn api_error_json() {

‎tests/testsuite/registry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ fn bad_license_file(registry: &TestRegistry) {
11461146
p.cargo("publish -v")
11471147
.replace_crates_io(registry.index_url())
11481148
.with_status(101)
1149-
.with_stderr_contains("[ERROR] the license file `foo` does not exist")
1149+
.with_stderr_contains("[ERROR] license-file `foo` does not appear to exist ([..]).")
11501150
.run();
11511151
}
11521152

0 commit comments

Comments
 (0)
Failed to load comments.