Skip to content

Commit 79c769c

Browse files
committed
Auto merge of #8353 - ehuss:fix-missing-readme, r=alexcrichton
Fix failure with missing readme. #8277 added implicit README support, but it also rejected parsing any manifest where the README was missing. This causes a problem because the README is often missing in many registry packages (for various reasons). This removes the validation at parsing time. Cargo has historically not had hard enforcement at the parsing stage. Whether or not the readme exists has always been enforced during publishing. I have added some extra context to the error message, and added a test to that effect. Fixes #8351
2 parents ee417cb + 7a5f036 commit 79c769c

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

src/cargo/ops/registry.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,13 @@ fn transmit(
233233
ref badges,
234234
ref links,
235235
} = *manifest.metadata();
236-
let readme_content = match *readme {
237-
Some(ref readme) => Some(paths::read(&pkg.root().join(readme))?),
238-
None => None,
239-
};
236+
let readme_content = readme
237+
.as_ref()
238+
.map(|readme| {
239+
paths::read(&pkg.root().join(readme))
240+
.chain_err(|| format!("failed to read `readme` file for package `{}`", pkg))
241+
})
242+
.transpose()?;
240243
if let Some(ref file) = *license_file {
241244
if !pkg.root().join(file).exists() {
242245
bail!("the license file `{}` does not exist", file)

src/cargo/util/toml/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,18 +1209,11 @@ impl TomlManifest {
12091209
project.namespaced_features.unwrap_or(false),
12101210
)?;
12111211

1212-
let readme = readme_for_project(package_root, project);
1213-
if let Some(ref r) = readme {
1214-
if !package_root.join(r).is_file() {
1215-
bail!("readme file with name '{}' was not found", r);
1216-
}
1217-
};
1218-
12191212
let metadata = ManifestMetadata {
12201213
description: project.description.clone(),
12211214
homepage: project.homepage.clone(),
12221215
documentation: project.documentation.clone(),
1223-
readme,
1216+
readme: readme_for_project(package_root, project),
12241217
authors: project.authors.clone().unwrap_or_default(),
12251218
license: project.license.clone(),
12261219
license_file: project.license_file.clone(),

tests/testsuite/publish.rs

Lines changed: 41 additions & 1 deletion
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, registry_path, registry_url, Package};
6-
use cargo_test_support::{basic_manifest, project, publish};
6+
use cargo_test_support::{basic_manifest, no_such_file_err_msg, project, publish};
77
use std::fs;
88

99
const CLEAN_FOO_JSON: &str = r#"
@@ -1341,3 +1341,43 @@ See [..]
13411341
)
13421342
.run();
13431343
}
1344+
1345+
#[cargo_test]
1346+
fn publish_with_missing_readme() {
1347+
registry::init();
1348+
let p = project()
1349+
.file(
1350+
"Cargo.toml",
1351+
r#"
1352+
[package]
1353+
name = "foo"
1354+
version = "0.1.0"
1355+
authors = []
1356+
license = "MIT"
1357+
description = "foo"
1358+
homepage = "https://example.com/"
1359+
readme = "foo.md"
1360+
"#,
1361+
)
1362+
.file("src/lib.rs", "")
1363+
.build();
1364+
1365+
p.cargo("publish --no-verify --token sekrit")
1366+
.with_status(101)
1367+
.with_stderr(&format!(
1368+
"\
1369+
[UPDATING] [..]
1370+
[PACKAGING] foo v0.1.0 [..]
1371+
[UPLOADING] foo v0.1.0 [..]
1372+
[ERROR] failed to read `readme` file for package `foo v0.1.0 ([ROOT]/foo)`
1373+
1374+
Caused by:
1375+
failed to read `[ROOT]/foo/foo.md`
1376+
1377+
Caused by:
1378+
{}
1379+
",
1380+
no_such_file_err_msg()
1381+
))
1382+
.run();
1383+
}

tests/testsuite/read_manifest.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,6 @@ fn cargo_read_manifest_defaults_readme_if_true() {
195195
.build();
196196

197197
p.cargo("read-manifest")
198-
.with_json(&manifest_output(&format!(r#""{}""#, "README.md")))
198+
.with_json(&manifest_output(r#""README.md""#))
199199
.run();
200200
}
201-
202-
// If a file named README.md does not exist, and `readme = true`, it should panic.
203-
#[cargo_test]
204-
#[should_panic]
205-
fn cargo_read_manifest_panics_if_default_readme_not_found() {
206-
let p = project()
207-
.file("Cargo.toml", &basic_bin_manifest_with_readme("foo", "true"))
208-
.file("README.txt", "Sample project")
209-
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
210-
.build();
211-
212-
p.cargo("read-manifest").run();
213-
}

0 commit comments

Comments
 (0)