fix: narrow cache miss recovery behavior

This commit is contained in:
Rasyidan Akbar F. 2026-03-17 09:02:06 +07:00
commit 3533670d92
2 changed files with 12 additions and 8 deletions

View file

@ -166,12 +166,10 @@ impl Cache {
let raw = match fs::read_to_string(&path) {
Ok(s) => s,
Err(e) => {
eprintln!(
"warning: corrupted cache entry for {}/{}, treating as miss: {}",
data_type, symbol, e
);
let _ = fs::remove_file(&path);
return Ok(None);
if e.kind() == std::io::ErrorKind::NotFound {
return Ok(None);
}
return Err(IdxError::Io(e.to_string()));
}
};
let entry: CacheEntry<T> = match serde_json::from_str(&raw) {
@ -207,7 +205,10 @@ pub fn cache_dir() -> Result<PathBuf, IdxError> {
if let Ok(dir) = std::env::var("XDG_CACHE_HOME")
&& !dir.is_empty()
{
return Ok(PathBuf::from(dir).join("idx"));
let path = PathBuf::from(dir);
if path.is_absolute() {
return Ok(path.join("idx"));
}
}
ProjectDirs::from("", "", "idx")
.map(|d| d.cache_dir().to_path_buf())

View file

@ -200,7 +200,10 @@ pub fn config_path() -> Result<PathBuf, IdxError> {
if let Ok(dir) = std::env::var("XDG_CONFIG_HOME")
&& !dir.is_empty()
{
return Ok(PathBuf::from(dir).join("idx").join("config.toml"));
let path = PathBuf::from(dir);
if path.is_absolute() {
return Ok(path.join("idx").join("config.toml"));
}
}
ProjectDirs::from("", "", "idx")
.map(|d| d.config_dir().join("config.toml"))