Skip to content

Commit f3109bb

Browse files
Add path context to invalid TOML config errors (#4815)
Fixes #4749. ## Summary The old report was filed against a `spacetime.toml` path from 1.11-era CLI flows. On current `master`, the shared TOML config parser already returns parse failures, but the top-level error does not say which config file was invalid. This patch makes the shared TOML config loader add file-path context while preserving the underlying TOML span details, so users get an error like `invalid TOML syntax in ...` followed by the parser's line and column information. ## Testing - `cargo test -p spacetimedb-core parse_config_reports_the_invalid_file_path -- --nocapture` - manual test: ``` ➜ cargo run -pspacetimedb-cli -- list Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.33s Running `target/debug/spacetimedb-cli list` Error: config file /home/work/.config/spacetime/cli.toml is invalid Caused by: TOML parse error at line 5, column 17 | 5 | [[server_configs] | ^ invalid table header expected `.`, `]]` ``` Co-authored-by: clockwork-labs-bot <clockwork-labs-bot@users.noreply.github.com>
1 parent 809aebd commit f3109bb

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

crates/core/src/config.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::path::Path;
22
use std::time::Duration;
33
use std::{fmt, io};
44

5+
use anyhow::Context;
56
use serde::Deserialize;
67
use spacetimedb_lib::ConnectionId;
78
use spacetimedb_paths::cli::{ConfigDir, PrivKeyPath, PubKeyPath};
@@ -12,7 +13,11 @@ use spacetimedb_paths::server::{ConfigToml, MetadataTomlPath};
1213
/// **WARNING**: Comments and formatting in the file will be lost.
1314
pub fn parse_config<T: serde::de::DeserializeOwned>(path: &Path) -> anyhow::Result<Option<T>> {
1415
match std::fs::read_to_string(path) {
15-
Ok(contents) => Ok(Some(toml::from_str(&contents)?)),
16+
Ok(contents) => {
17+
let config =
18+
toml::from_str(&contents).with_context(|| format!("invalid TOML syntax in {}", path.display()))?;
19+
Ok(Some(config))
20+
}
1621
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
1722
Err(e) => Err(e.into()),
1823
}
@@ -310,6 +315,7 @@ where
310315
#[cfg(test)]
311316
mod tests {
312317
use super::*;
318+
use tempfile::TempDir;
313319

314320
fn mkver(major: u64, minor: u64, patch: u64) -> semver::Version {
315321
semver::Version::new(major, minor, patch)
@@ -341,6 +347,21 @@ mod tests {
341347
}
342348
}
343349

350+
#[test]
351+
fn parse_config_reports_the_invalid_file_path() {
352+
let temp = TempDir::new().unwrap();
353+
let path = temp.path().join("config.toml");
354+
std::fs::write(&path, "[module]\nname = \"my-project\n").unwrap();
355+
356+
let err = match parse_config::<ConfigFile>(&path) {
357+
Ok(_) => panic!("expected invalid TOML to fail"),
358+
Err(err) => format!("{err:#}"),
359+
};
360+
assert!(err.contains("invalid TOML syntax"));
361+
assert!(err.contains("config.toml"));
362+
assert!(err.contains("line 2"));
363+
}
364+
344365
#[test]
345366
fn check_metadata_compatibility_checking() {
346367
assert_eq!(

0 commit comments

Comments
 (0)