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(desktop): persist app config in Tauri app data dir (#7903)

Full Stack Engineer372ad1518328
diff --git a/src-tauri/src/core/app/commands.rs b/src-tauri/src/core/app/commands.rs
index 0f3aadc24..ffd0e8390 100644
--- a/src-tauri/src/core/app/commands.rs
+++ b/src-tauri/src/core/app/commands.rs
@@ -1,39 +1,112 @@
-use std::{fs, path::PathBuf};
+use std::{
+ fs,
+ path::{Path, PathBuf},
+};
use tauri::{AppHandle, Manager, Runtime, State};
use super::{
- constants::CONFIGURATION_FILE_NAME, helpers::copy_dir_recursive, models::AppConfiguration,
+ constants::{CONFIGURATION_FILE_NAME, TAURI_BUNDLE_IDENTIFIER},
+ helpers::copy_dir_recursive,
+ models::AppConfiguration,
};
use crate::core::state::AppState;
-/// Resolve the Jan config file path without an AppHandle (for CLI use).
-/// Mirrors the logic in get_configuration_file_path() using the dirs crate.
-pub fn resolve_config_file_path() -> PathBuf {
- let package_name = env!("CARGO_PKG_NAME");
+/// Canonical Jan app support directory (`%APPDATA%/Jan` on Windows).
+fn resolve_human_readable_app_data_dir() -> Option<PathBuf> {
+ dirs::data_dir().map(|d| d.join(env!("CARGO_PKG_NAME")))
+}
- // On Linux, prefer the XDG config dir first (matches Tauri behaviour)
- #[cfg(target_os = "linux")]
- if let Some(config_dir) = dirs::config_dir() {
- let path = config_dir.join(package_name);
- if path.exists() {
- return path.join(CONFIGURATION_FILE_NAME);
+/// Tauri bundle-id app support directory (e.g. `%APPDATA%/jan.ai.app` on Windows).
+fn resolve_bundle_app_data_dir() -> Option<PathBuf> {
+ dirs::data_dir().map(|d| d.join(TAURI_BUNDLE_IDENTIFIER))
+}
+
+/// Keep `%APPDATA%/Jan/settings.json` as canonical, but recover from legacy or
+/// alternate locations if users removed one directory (#7898).
+fn migrate_legacy_app_configuration(app_data_dir: &Path) -> std::io::Result<()> {
+ fs::create_dir_all(app_data_dir)?;
+ let canonical = app_data_dir.join(CONFIGURATION_FILE_NAME);
+ if canonical.exists() {
+ return Ok(());
+ }
+
+ migrate_from_candidates(&canonical, legacy_app_config_candidate_paths(app_data_dir))
+}
+
+fn migrate_from_candidates(canonical: &Path, candidates: Vec<PathBuf>) -> std::io::Result<()> {
+ if let Some(parent) = canonical.parent() {
+ fs::create_dir_all(parent)?;
+ }
+
+ for legacy in candidates {
+ if legacy.is_file() {
+ log::info!(
+ "Recovering app configuration from {} to {}",
+ legacy.display(),
+ canonical.display()
+ );
+ fs::copy(&legacy, canonical)?;
+ return Ok(());
}
}
+ Ok(())
+}
+
+fn legacy_app_config_candidate_paths(app_data_dir: &Path) -> Vec<PathBuf> {
+ let mut paths = Vec::new();
+
+ if let Some(bundle_dir) = resolve_bundle_app_data_dir() {
+ paths.push(bundle_dir.join(CONFIGURATION_FILE_NAME));
+ }
- // Primary path: data_dir/Jan (e.g. ~/Library/Application Support/Jan on macOS)
- if let Some(data_dir) = dirs::data_dir() {
- let path = data_dir.join(package_name);
- if !path.exists() {
- let _ = fs::create_dir_all(&path);
+ #[cfg(target_os = "linux")]
+ {
+ let package_name = env!("CARGO_PKG_NAME");
+ if let Some(config_dir) = dirs::config_dir() {
+ let legacy = config_dir.join(package_name).join(CONFIGURATION_FILE_NAME);
+ if legacy != app_data_dir.join(CONFIGURATION_FILE_NAME) {
+ paths.push(legacy);
+ }
}
- return path.join(CONFIGURATION_FILE_NAME);
}
- // Last resort: home directory
- let home = std::env::var("HOME")
- .or_else(|_| std::env::var("USERPROFILE"))
- .unwrap_or_default();
- PathBuf::from(home).join(CONFIGURATION_FILE_NAME)
+ paths
+}
+
+fn app_data_dir_with_fallback<R: Runtime>(app_handle: &tauri::AppHandle<R>) -> PathBuf {
+ let package_name = env!("CARGO_PKG_NAME");
+ app_handle.path().data_dir().unwrap_or_else(|err| {
+ log::error!("Failed to get data directory: {err}. Using home directory instead.");
+
+ let home_dir = std::env::var(if cfg!(target_os = "windows") {
+ "USERPROFILE"
+ } else {
+ "HOME"
+ })
+ .expect("Failed to determine the home directory");
+
+ PathBuf::from(home_dir)
+ })
+ .join(package_name)
+}
+
+/// Resolve the Jan config file path without an AppHandle (for CLI use).
+/// Canonical location is `%APPDATA%/Jan/settings.json` (or OS equivalent),
+/// with fallback recovery from bundle-id location when needed.
+pub fn resolve_config_file_path() -> PathBuf {
+ let app_data = resolve_human_readable_app_data_dir().unwrap_or_else(|| {
+ let package_name = env!("CARGO_PKG_NAME");
+ let home = std::env::var("HOME")
+ .or_else(|_| std::env::var("USERPROFILE"))
+ .unwrap_or_default();
+ PathBuf::from(home).join(package_name)
+ });
+
+ if let Err(err) = migrate_legacy_app_configuration(&app_data) {
+ log::warn!("Legacy app config migration (CLI) skipped: {err}");
+ }
+
+ app_data.join(CONFIGURATION_FILE_NAME)
}
/// Resolve the Jan data folder path without an AppHandle (for CLI use).
@@ -68,7 +141,12 @@ pub fn get_app_configurations<R: Runtime>(app_handle: tauri::AppHandle<R>) -> Ap
return app_default_configuration;
}
- let configuration_file = get_configuration_file_path(app_handle.clone());
+ let app_path = app_data_dir_with_fallback(&app_handle);
+ if let Err(err) = migrate_legacy_app_configuration(&app_path) {
+ log::warn!("Legacy app config migration skipped: {err}");
+ }
+
+ let configuration_file = app_path.join(CONFIGURATION_FILE_NAME);
let default_data_folder = default_data_folder_path(app_handle.clone());
@@ -157,44 +235,11 @@ pub fn get_jan_data_folder_path<R: Runtime>(app_handle: tauri::AppHandle<R>) ->
#[tauri::command]
pub fn get_configuration_file_path<R: Runtime>(app_handle: tauri::AppHandle<R>) -> PathBuf {
- let app_path = app_handle.path().app_data_dir().unwrap_or_else(|err| {
- log::error!("Failed to get app data directory: {err}. Using home directory instead.");
-
- let home_dir = std::env::var(if cfg!(target_os = "windows") {
- "USERPROFILE"
- } else {
- "HOME"
- })
- .expect("Failed to determine the home directory");
-
- PathBuf::from(home_dir)
- });
-
- let package_name = env!("CARGO_PKG_NAME");
- #[cfg(target_os = "linux")]
- let old_data_dir = {
- if let Some(config_path) = dirs::config_dir() {
- config_path.join(package_name)
- } else {
- log::debug!("Could not determine config directory");
- app_path
- .parent()
- .unwrap_or(&app_path.join("../"))
- .join(package_name)
- }
- };
-
- #[cfg(not(target_os = "linux"))]
- let old_data_dir = app_path
- .parent()
- .unwrap_or(&app_path.join("../"))
- .join(package_name);
-
- if old_data_dir.exists() {
- old_data_dir.join(CONFIGURATION_FILE_NAME)
- } else {
- app_path.join(CONFIGURATION_FILE_NAME)
+ let app_path = app_data_dir_with_fallback(&app_handle);
+ if let Err(err) = migrate_legacy_app_configuration(&app_path) {
+ log::warn!("Legacy app config migration skipped: {err}");
}
+ app_path.join(CONFIGURATION_FILE_NAME)
}
#[tauri::command]
@@ -276,3 +321,71 @@ pub fn change_app_data_folder<R: Runtime>(
pub fn app_token(state: State<'_, AppState>) -> Option<String> {
state.app_token.clone()
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::Value;
+ use tempfile::tempdir;
+
+ #[test]
+ fn migration_copies_legacy_when_canonical_missing() {
+ let tmp = tempdir().expect("temp dir");
+ let canonical_dir = tmp.path().join("Jan");
+ let canonical = canonical_dir.join(CONFIGURATION_FILE_NAME);
+ let legacy = tmp.path().join("jan.ai.app").join(CONFIGURATION_FILE_NAME);
+
+ fs::create_dir_all(legacy.parent().unwrap()).expect("create legacy dir");
+ fs::write(&legacy, r#"{"data_folder":"D:\\jan.ai"}"#).expect("write legacy config");
+
+ migrate_from_candidates(&canonical, vec![legacy.clone()]).expect("migration succeeds");
+
+ let recovered = fs::read_to_string(&canonical).expect("read canonical");
+ assert!(recovered.contains(r#""data_folder":"D:\\jan.ai""#));
+ assert!(legacy.exists(), "migration should be copy-only");
+ }
+
+ #[test]
+ fn migration_skips_when_canonical_exists() {
+ let tmp = tempdir().expect("temp dir");
+ let canonical_dir = tmp.path().join("Jan");
+ let canonical = canonical_dir.join(CONFIGURATION_FILE_NAME);
+ let legacy = tmp.path().join("jan.ai.app").join(CONFIGURATION_FILE_NAME);
+
+ fs::create_dir_all(canonical.parent().unwrap()).expect("create canonical dir");
+ fs::create_dir_all(legacy.parent().unwrap()).expect("create legacy dir");
+ fs::write(&canonical, r#"{"data_folder":"D:\\kept"}"#).expect("write canonical config");
+ fs::write(&legacy, r#"{"data_folder":"D:\\legacy"}"#).expect("write legacy config");
+
+ migrate_legacy_app_configuration(&canonical_dir).expect("migration succeeds");
+
+ let current = fs::read_to_string(&canonical).expect("read canonical");
+ assert!(current.contains(r#""data_folder":"D:\\kept""#));
+ }
+
+ #[test]
+ fn migration_handles_missing_legacy_files() {
+ let tmp = tempdir().expect("temp dir");
+ let canonical = tmp.path().join("Jan").join(CONFIGURATION_FILE_NAME);
+ let missing = tmp.path().join("missing").join(CONFIGURATION_FILE_NAME);
+
+ migrate_from_candidates(&canonical, vec![missing]).expect("migration succeeds");
+ assert!(!canonical.exists(), "canonical should remain absent");
+ }
+
+ #[test]
+ fn bundle_identifier_matches_tauri_conf() {
+ let conf_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tauri.conf.json");
+ let content = fs::read_to_string(conf_path).expect("read tauri.conf.json");
+ let json: Value = serde_json::from_str(&content).expect("parse tauri.conf.json");
+ let identifier = json
+ .get("identifier")
+ .and_then(|v| v.as_str())
+ .expect("identifier field exists");
+
+ assert_eq!(
+ identifier, TAURI_BUNDLE_IDENTIFIER,
+ "TAURI_BUNDLE_IDENTIFIER must stay synced with tauri.conf.json"
+ );
+ }
+}
diff --git a/src-tauri/src/core/app/constants.rs b/src-tauri/src/core/app/constants.rs
index 432ca3518..7b9502741 100644
--- a/src-tauri/src/core/app/constants.rs
+++ b/src-tauri/src/core/app/constants.rs
@@ -1,6 +1,10 @@
// App Configuration Constants
pub const CONFIGURATION_FILE_NAME: &str = "settings.json";
+/// Tauri bundle `identifier` from `tauri.conf.json`. Used only as a fallback
+/// source for legacy config recovery.
+pub const TAURI_BUNDLE_IDENTIFIER: &str = "jan.ai.app";
+
// Categorised lists of Jan data directories and files.
// The factory-reset logic selectively deletes by category based on user choices.
// Add new entries to the appropriate category so they are picked up automatically.