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 54e7613

Browse files
committedJun 4, 2024
fix(toml): Convert warnings that license and readme files do not exist into errors
1 parent 2f17770 commit 54e7613

File tree

4 files changed

+46
-98
lines changed

4 files changed

+46
-98
lines changed
 

‎src/cargo/ops/cargo_package.rs

+22-9
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(

‎tests/testsuite/package.rs

+22-39
Original file line numberDiff line numberDiff line change
@@ -1943,8 +1943,7 @@ fn exclude_dot_files_and_directories_by_default() {
19431943

19441944
#[cargo_test]
19451945
fn empty_readme_path() {
1946-
// Warn but don't fail if `readme` is empty.
1947-
// Issue #11522.
1946+
// fail if `readme` is empty.
19481947
let p = project()
19491948
.file(
19501949
"Cargo.toml",
@@ -1963,22 +1962,19 @@ fn empty_readme_path() {
19631962
.build();
19641963

19651964
p.cargo("package --no-verify")
1965+
.with_status(101)
19661966
.with_stderr(
19671967
"\
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)
1968+
[ERROR] readme `` does not appear to exist (relative to `[..]/foo`).
1969+
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`.
19731970
",
19741971
)
19751972
.run();
19761973
}
19771974

19781975
#[cargo_test]
19791976
fn invalid_readme_path() {
1980-
// Warn but don't fail if `readme` path is invalid.
1981-
// Issue #11522.
1977+
// fail if `readme` path is invalid.
19821978
let p = project()
19831979
.file(
19841980
"Cargo.toml",
@@ -1997,22 +1993,19 @@ fn invalid_readme_path() {
19971993
.build();
19981994

19991995
p.cargo("package --no-verify")
1996+
.with_status(101)
20001997
.with_stderr(
20011998
"\
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)
1999+
[ERROR] readme `DOES-NOT-EXIST` does not appear to exist (relative to `[..]/foo`).
2000+
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`.
20072001
",
20082002
)
20092003
.run();
20102004
}
20112005

20122006
#[cargo_test]
20132007
fn readme_or_license_file_is_dir() {
2014-
// Test warning when `readme` or `license-file` is a directory, not a file.
2015-
// Issue #11522.
2008+
// Test error when `readme` or `license-file` is a directory, not a file.
20162009
let p = project()
20172010
.file(
20182011
"Cargo.toml",
@@ -2031,25 +2024,21 @@ fn readme_or_license_file_is_dir() {
20312024
.build();
20322025

20332026
p.cargo("package --no-verify")
2027+
.with_status(101)
20342028
.with_stderr(
20352029
"\
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)
2030+
[ERROR] license-file `./src` does not appear to exist (relative to `[..]/foo`).
2031+
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`.
2032+
readme `./src` does not appear to exist (relative to `[..]/foo`).
2033+
Please update the readme setting in the manifest at `[..]/foo/Cargo.toml`.
20442034
",
20452035
)
20462036
.run();
20472037
}
20482038

20492039
#[cargo_test]
20502040
fn empty_license_file_path() {
2051-
// Warn but don't fail if license-file is empty.
2052-
// Issue #11522.
2041+
// fail if license-file is empty.
20532042
let p = project()
20542043
.file(
20552044
"Cargo.toml",
@@ -2067,15 +2056,11 @@ fn empty_license_file_path() {
20672056
.build();
20682057

20692058
p.cargo("package --no-verify")
2059+
.with_status(101)
20702060
.with_stderr(
20712061
"\
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)
2062+
[ERROR] license-file `` does not appear to exist (relative to `[..]/foo`).
2063+
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`.
20792064
",
20802065
)
20812066
.run();
@@ -2101,13 +2086,11 @@ fn invalid_license_file_path() {
21012086
.build();
21022087

21032088
p.cargo("package --no-verify")
2089+
.with_status(101)
21042090
.with_stderr(
21052091
"\
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)
2092+
[ERROR] license-file `does-not-exist` does not appear to exist (relative to `[..]/foo`).
2093+
Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml`.
21112094
",
21122095
)
21132096
.run();
@@ -3522,7 +3505,7 @@ fn versionless_package() {
35223505
p.cargo("package")
35233506
.with_stderr(
35243507
"\
3525-
warning: manifest has no license, license-file, documentation, homepage or repository.
3508+
warning: manifest has no documentation, homepage or repository.
35263509
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
35273510
Packaging foo v0.0.0 ([CWD])
35283511
Verifying foo v0.0.0 ([CWD])

‎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.