Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(cli): explain a misplaced root key and stop triple-printing it

thinhlpg231cc174aa69
diff --git a/src-tauri/src/core/agent/global_config.rs b/src-tauri/src/core/agent/global_config.rs
index 63f42f17f..f4f5ce803 100644
--- a/src-tauri/src/core/agent/global_config.rs
+++ b/src-tauri/src/core/agent/global_config.rs
@@ -176,7 +176,7 @@ pub(crate) fn load_global_config() -> Result<HashMap<String, ProviderConfig>, St
Err(_) => return Ok(HashMap::new()),
};
let parsed: GlobalConfigToml =
- toml::from_str(&raw).map_err(|e| format!("Failed to parse {}: {e}", path.display()))?;
+ toml::from_str(&raw).map_err(|e| parse_error(&path, &e, e.message()))?;
Ok(parsed
.providers
@@ -354,6 +354,46 @@ pub(crate) fn wave_glyph() -> Option<String> {
}
}
+/// Root-level (non-`[providers.*]`) keys of `~/.jan/config.toml`, used to spot
+/// the one mistake this file invites: appending a top-level key to the end of
+/// the file, where it silently lands inside whichever `[providers.*]` table
+/// happens to be last.
+const ROOT_KEYS: &[&str] = &[
+ "default_model",
+ "smol_model",
+ "mouse",
+ "sandbox",
+ "think_tags",
+ "stream_reasoning",
+ "ask_timeout_secs",
+ "terminal_hint",
+ "wave",
+];
+
+/// Render a TOML parse failure with a fix, not just a location. `toml`'s own
+/// message points at the offending line and stops there, which is useless for
+/// the common case: `echo 'key = v' >> ~/.jan/config.toml` appends *after* the
+/// last `[providers.*]` header, so the key parses as a provider field and
+/// collides on a second append. Naming the root key and the remedy turns a
+/// dead end into a one-step fix.
+fn parse_error(path: &std::path::Path, err: &impl std::fmt::Display, message: &str) -> String {
+ let base = format!("Failed to parse {}: {err}", path.display());
+ let Some(key) = ROOT_KEYS
+ .iter()
+ .find(|key| message.contains(&format!("`{key}`")))
+ else {
+ return base;
+ };
+ format!(
+ "{base}\n\
+ hint: `{key}` is a top-level key, but it landed inside a `[providers.*]` \
+ table - appending to the end of the file puts it under whichever provider \
+ is last.\n\
+ hint: move `{key}` above the first `[providers.*]` header, or set it with \
+ `/settings` in the console, which writes it to the right place."
+ )
+}
+
/// Read `~/.jan/config.toml` into the raw TOML struct for editing. Missing file
/// -> default (empty); malformed file -> error, so a set never silently drops an
/// unparseable file's contents.
@@ -363,7 +403,7 @@ fn load_raw() -> Result<GlobalConfigToml, String> {
Ok(raw) => raw,
Err(_) => return Ok(GlobalConfigToml::default()),
};
- toml::from_str(&raw).map_err(|e| format!("Failed to parse {}: {e}", path.display()))
+ toml::from_str(&raw).map_err(|e| parse_error(&path, &e, e.message()))
}
/// Serialize the config back to `~/.jan/config.toml`, creating `~/.jan` if
@@ -514,7 +554,7 @@ pub(crate) fn set_global_key(key: &str, value: Option<toml_edit::Item>) -> Resul
.map_err(|e| format!("Failed to read {}: {e}", path.display()))?;
let mut doc = raw
.parse::<toml_edit::DocumentMut>()
- .map_err(|e| format!("Failed to parse {}: {e}", path.display()))?;
+ .map_err(|e| parse_error(&path, &e, e.message()))?;
match value {
// `toml_edit` renders a root table's scalars ahead of its sub-tables,
@@ -721,6 +761,42 @@ mod tests {
);
}
+ /// A root key appended to the end of the file lands inside the last
+ /// `[providers.*]` table, and `toml`'s own error says only "duplicate key
+ /// ... in table `providers.opencode`" - true, but it never says the key
+ /// belongs at the root or how to get it there. The hint carries the fix,
+ /// so assert it fires for a misplaced root key and stays quiet otherwise.
+ #[test]
+ fn parse_error_explains_a_root_key_captured_by_a_provider_table() {
+ with_temp_home(|_| {
+ let path = ensure_global_config().expect("ensure");
+ std::fs::write(
+ &path,
+ "[providers.opencode]\napi_key = \"sk-x\"\nask_timeout_secs = 15\nask_timeout_secs = 15\n",
+ )
+ .unwrap();
+
+ let err = load_global_config().expect_err("duplicate key must fail");
+ assert!(
+ err.contains("`ask_timeout_secs` is a top-level key"),
+ "hint must name the misplaced key: {err}"
+ );
+ assert!(
+ err.contains("/settings"),
+ "hint must point at the writer that gets it right: {err}"
+ );
+
+ // Syntax errors that have nothing to do with key placement must not
+ // acquire a hint about it.
+ std::fs::write(&path, "not = = toml\n").unwrap();
+ let err = load_global_config().expect_err("syntax error must fail");
+ assert!(
+ !err.contains("top-level key"),
+ "unrelated parse errors must stay unadorned: {err}"
+ );
+ });
+ }
+
/// The `/settings` write path. Two properties matter and neither is
/// obvious: the commented template survives a write (the typed round-trip
/// `write_raw` does would throw every example line away), and a key added
diff --git a/src-tauri/src/core/cli/providers.rs b/src-tauri/src/core/cli/providers.rs
index c267a0155..b1b420f17 100644
--- a/src-tauri/src/core/cli/providers.rs
+++ b/src-tauri/src/core/cli/providers.rs
@@ -115,6 +115,16 @@ pub fn is_cli_reachable(config: &ProviderConfig) -> bool {
config.base_url.as_deref().is_some_and(|u| !u.is_empty())
}
+/// Log a provider-config load failure at most once per process. Startup probes
+/// this in two independent places (the headless sign-in guard and the model
+/// fallback), and a malformed `~/.jan/config.toml` fails both, so without this
+/// the same multi-line TOML error is printed twice before the fatal error
+/// prints it a third time.
+fn warn_load_failure_once(context: &str, err: &str) {
+ static LOGGED: std::sync::Once = std::sync::Once::new();
+ LOGGED.call_once(|| log::warn!("could not load provider configs{context}: {err}"));
+}
+
/// Whether this install can run a turn at all: some provider is reachable and
/// either credentialed or local (a self-hosted endpoint - typically the desktop
/// app's API server - needs no key). `false` is the fresh-install state that
@@ -125,7 +135,7 @@ pub fn has_usable_provider(project_root: Option<&std::path::Path>) -> bool {
match load_provider_configs(project_root, &overrides) {
Ok(configs) => configs.values().any(is_usable),
Err(e) => {
- log::warn!("could not load provider configs: {e}");
+ warn_load_failure_once("", &e);
false
}
}
@@ -206,7 +216,7 @@ pub fn list_provider_models(project_root: Option<&std::path::Path>) -> Vec<(Stri
match load_provider_configs(project_root, &ProviderOverrides::default().with_env()) {
Ok(configs) => reachable_models(&configs),
Err(e) => {
- log::warn!("could not load provider configs for the model picker: {e}");
+ warn_load_failure_once(" for the model picker", &e);
Vec::new()
}
}