Skip to content

Commit

Permalink
Auto merge of #8353 - ehuss:fix-missing-readme, r=alexcrichton
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jun 11, 2020
2 parents ee417cb + 7a5f036 commit 79c769c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
11 changes: 7 additions & 4 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,13 @@ fn transmit(
ref badges,
ref links,
} = *manifest.metadata();
let readme_content = match *readme {
Some(ref readme) => Some(paths::read(&pkg.root().join(readme))?),
None => None,
};
let readme_content = readme
.as_ref()
.map(|readme| {
paths::read(&pkg.root().join(readme))
.chain_err(|| format!("failed to read `readme` file for package `{}`", pkg))
})
.transpose()?;
if let Some(ref file) = *license_file {
if !pkg.root().join(file).exists() {
bail!("the license file `{}` does not exist", file)
Expand Down
9 changes: 1 addition & 8 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,18 +1209,11 @@ impl TomlManifest {
project.namespaced_features.unwrap_or(false),
)?;

let readme = readme_for_project(package_root, project);
if let Some(ref r) = readme {
if !package_root.join(r).is_file() {
bail!("readme file with name '{}' was not found", r);
}
};

let metadata = ManifestMetadata {
description: project.description.clone(),
homepage: project.homepage.clone(),
documentation: project.documentation.clone(),
readme,
readme: readme_for_project(package_root, project),
authors: project.authors.clone().unwrap_or_default(),
license: project.license.clone(),
license_file: project.license_file.clone(),
Expand Down
42 changes: 41 additions & 1 deletion tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use cargo_test_support::git::{self, repo};
use cargo_test_support::paths;
use cargo_test_support::registry::{self, registry_path, registry_url, Package};
use cargo_test_support::{basic_manifest, project, publish};
use cargo_test_support::{basic_manifest, no_such_file_err_msg, project, publish};
use std::fs;

const CLEAN_FOO_JSON: &str = r#"
Expand Down Expand Up @@ -1341,3 +1341,43 @@ See [..]
)
.run();
}

#[cargo_test]
fn publish_with_missing_readme() {
registry::init();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
authors = []
license = "MIT"
description = "foo"
homepage = "https://example.com/"
readme = "foo.md"
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("publish --no-verify --token sekrit")
.with_status(101)
.with_stderr(&format!(
"\
[UPDATING] [..]
[PACKAGING] foo v0.1.0 [..]
[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();
}
15 changes: 1 addition & 14 deletions tests/testsuite/read_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,6 @@ fn cargo_read_manifest_defaults_readme_if_true() {
.build();

p.cargo("read-manifest")
.with_json(&manifest_output(&format!(r#""{}""#, "README.md")))
.with_json(&manifest_output(r#""README.md""#))
.run();
}

// If a file named README.md does not exist, and `readme = true`, it should panic.
#[cargo_test]
#[should_panic]
fn cargo_read_manifest_panics_if_default_readme_not_found() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest_with_readme("foo", "true"))
.file("README.txt", "Sample project")
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
.build();

p.cargo("read-manifest").run();
}

0 comments on commit 79c769c

Please sign in to comment.