Skip to content

Commit 5a22b06

Browse files
committed
Error for invalid dependency specification
Closes #4867
1 parent 8fc37fd commit 5a22b06

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@
261261
dependencies to be imported.
262262
([Surya Rose](https://github.com/GearsDatapacks))
263263

264+
- Erroneous extra fields in `gleam.toml` dependency specifications will no
265+
longer be siltently ignored. An error is now returned highlighting the
266+
problem instead.
267+
([Louis Pilfold](https://github.com/lpil))
268+
264269
- Fixed a bug where renaming a constant which is referenced in another module
265270
inside a guard would generate invalid code.
266271
([Surya Rose](https://github.com/GearsDatapacks))

compiler-core/src/config.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,7 @@ impl PackageConfig {
218218
fs: &FS,
219219
) -> Result<PackageConfig, Error> {
220220
let toml = fs.read(path.as_ref())?;
221-
let config: PackageConfig = toml::from_str(&toml).map_err(|e| Error::FileIo {
222-
action: FileIoAction::Parse,
223-
kind: FileKind::File,
224-
path: path.as_ref().to_path_buf(),
225-
err: Some(e.to_string()),
226-
})?;
227-
Ok(config)
221+
deserialise_config(path, toml)
228222
}
229223

230224
/// Get the locked packages for the current config and a given (optional)
@@ -322,6 +316,37 @@ impl PackageConfig {
322316
}
323317
}
324318

319+
fn deserialise_config<P: AsRef<Utf8Path>>(
320+
path: P,
321+
toml: String,
322+
) -> std::result::Result<PackageConfig, Error> {
323+
let config: PackageConfig = toml::from_str(&toml).map_err(|e| Error::FileIo {
324+
action: FileIoAction::Parse,
325+
kind: FileKind::File,
326+
path: path.as_ref().to_path_buf(),
327+
err: Some(e.to_string()),
328+
})?;
329+
Ok(config)
330+
}
331+
332+
// https://github.com/gleam-lang/gleam/issues/4867
333+
#[test]
334+
fn deny_extra_deps_properties() {
335+
let toml = r#"
336+
name = "wibble"
337+
version = "1.0.0"
338+
339+
[dependencies]
340+
aide_generator = { git = "[email protected]:crowdhailer/aide.git", ref = "f559c5bc", extra = "idk what this is" }
341+
"#;
342+
let error = deserialise_config("gleam.toml", toml.into())
343+
.expect_err("should fail to deserialise because of additional path");
344+
assert_eq!(
345+
error.to_string(),
346+
r#"Parse "gleam.toml" failed: Some("data did not match any variant of untagged enum Requirement for key `dependencies.aide_generator` at line 6 column 18")"#
347+
);
348+
}
349+
325350
#[test]
326351
fn locked_no_manifest() {
327352
let mut config = PackageConfig::default();

compiler-core/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub enum Error {
115115
#[error("cyclical package dependencies")]
116116
PackageCycle { packages: Vec<EcoString> },
117117

118-
#[error("file operation failed")]
118+
#[error("{action:?} {path:?} failed: {err:?}")]
119119
FileIo {
120120
kind: FileKind,
121121
action: FileIoAction,

compiler-core/src/requirement.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ use serde::de::{self, Deserializer, MapAccess, Visitor};
1212
use serde::ser::{Serialize, SerializeMap, Serializer};
1313

1414
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
15-
#[serde(untagged, remote = "Self")]
15+
#[serde(untagged, remote = "Self", deny_unknown_fields)]
1616
pub enum Requirement {
1717
Hex {
1818
#[serde(deserialize_with = "deserialise_range")]
1919
version: Range,
2020
},
21+
2122
Path {
2223
path: Utf8PathBuf,
2324
},

0 commit comments

Comments
 (0)