Skip to content

Commit 157111d

Browse files
giacomocavalierilpil
authored andcommitted
stop crashing when there's an invalid version in a gleam.toml file
fixes #4817
1 parent f13b864 commit 157111d

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,7 @@
138138
expression as safe to remove, instead of just highlighting the double
139139
negation.
140140
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
141+
142+
- Fixed a bug where the compiler would crash if there was an invalid version
143+
requirement in a project's `gleam.toml`.
144+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

compiler-core/src/requirement.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ impl<'de> Visitor<'de> for RequirementVisitor {
122122
where
123123
E: de::Error,
124124
{
125-
Ok(FromStr::from_str(value).expect("expected string"))
125+
match value.parse::<Requirement>() {
126+
Ok(value) => Ok(value),
127+
Err(error) => Err(de::Error::custom(error)),
128+
}
126129
}
127130

128131
fn visit_map<M>(self, visitor: M) -> Result<Self::Value, M::Error>
@@ -165,4 +168,15 @@ mod tests {
165168
Requirement::git("https://github.com/gleam-lang/otp.git", "4d34935")
166169
);
167170
}
171+
172+
#[test]
173+
fn read_wrong_version() {
174+
let toml = r#"
175+
short = ">= 2.0 and < 3.0.0"
176+
"#;
177+
178+
let error =
179+
toml::from_str::<HashMap<String, Requirement>>(toml).expect_err("invalid version");
180+
insta::assert_snapshot!(error.to_string());
181+
}
168182
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: compiler-core/src/requirement.rs
3+
expression: error.to_string()
4+
---
5+
>= 2.0 and < 3.0.0 is not a valid version. missing patch version: 2.0 for key `short` at line 2 column 21

0 commit comments

Comments
 (0)