Skip to content

Commit 8b20451

Browse files
refuse to publish packages with no readme
1 parent 97ed52b commit 8b20451

8 files changed

Lines changed: 91 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
README generated by the `gleam new` command.
7575
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
7676

77+
- The build tool will now refuse to publish any package that has no README.
78+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
79+
7780
### Language server
7881

7982
- The language server now allows extracting the start of a pipeline into a

compiler-cli/src/publish.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ HTML documentation will work:
134134

135135
fn check_for_default_readme(config: &PackageConfig, paths: &ProjectPaths) -> Result<(), Error> {
136136
let default_readme = default_readme(config.name.as_str());
137-
let project_readme = fs::read(paths.readme())?;
137+
let project_readme = match fs::read(paths.readme()) {
138+
Err(Error::FileIo {
139+
err: Some(message), ..
140+
}) if message.contains("No such file or directory") => {
141+
return Err(Error::CannotPublishWithNoReadme);
142+
}
143+
Err(error) => return Err(error),
144+
Ok(project_readme) => project_readme,
145+
};
138146

139147
// We consider the two READMEs equal modulo whitespace, otherwise it would
140148
// be pretty trivial to trick this check by just formatting the default

compiler-core/src/error.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ file_names.iter().map(|x| x.as_str()).join(", "))]
315315
#[error("Publishing a package with the default README is not permitted")]
316316
CannotPublishWithDefaultReadme,
317317

318+
#[error("Publishing a package with no README is not permitted")]
319+
CannotPublishWithNoReadme,
320+
318321
#[error("Publishing packages to reserve names is not permitted")]
319322
HexPackageSquatting,
320323

@@ -883,6 +886,19 @@ published package should have its own carefully written README.
883886
),
884887
}]
885888
}
889+
890+
Error::CannotPublishWithNoReadme => {
891+
let text = "You appear to be attempting to publish a package with no README.
892+
All published packages should have one.
893+
"
894+
.into();
895+
896+
vec![Diagnostic {
897+
title: "Cannot publish with no README".into(),
898+
text,
899+
level: Level::Error,
900+
location: None,
901+
hint: Some("Add a README to your project before publishing.".into()),
886902
}]
887903
}
888904

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.beam
2+
*.ez
3+
build
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name = "no_readme"
2+
version = "1.0.0"
3+
description = "Test project for no readme"
4+
licences = ["Apache-2.0"]
5+
6+
[dependencies]
7+
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file was generated by Gleam
2+
# You typically do not need to edit this file
3+
4+
packages = [
5+
{ name = "gleam_stdlib", version = "0.62.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "0080706D3A5A9A36C40C68481D1D231D243AF602E6D2A2BE67BA8F8F4DFF45EC" },
6+
]
7+
8+
[requirements]
9+
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import gleam/io
2+
3+
/// This tests that a project with no readme doesn't get published.
4+
///
5+
pub fn main() -> Nil {
6+
greeting()
7+
first_line()
8+
second_line()
9+
}
10+
11+
fn greeting() {
12+
io.println("Hello from no_readme!")
13+
}
14+
15+
fn first_line() {
16+
io.println("Here we have some additional code so that this is not mistaken")
17+
}
18+
19+
fn second_line() {
20+
io.println("for a default main project, that would be rejected as well!")
21+
}

test/publishing_no_readme/test.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
GLEAM_COMMAND=${GLEAM_COMMAND:-"cargo run --quiet --"}
6+
7+
g() {
8+
echo "Running: $GLEAM_COMMAND $@"
9+
$GLEAM_COMMAND "$@"
10+
}
11+
12+
echo Resetting the build directory to get to a known state
13+
rm -fr build
14+
15+
echo Running publish should not publish anything
16+
if yes "n" | g publish; then
17+
echo "Expected publish to fail, but it succeeded"
18+
exit 1
19+
fi
20+
21+
echo
22+
echo Success! 💖
23+
echo

0 commit comments

Comments
 (0)