From faf97b896a7373589ca8890a2735053d0ae91bba Mon Sep 17 00:00:00 2001 From: WillLillis Date: Sun, 22 Dec 2024 19:10:29 -0500 Subject: [PATCH] fix(cli): use xdg config directory on macOS fix: address feedback --- Cargo.lock | 54 +++++++++++-------------------------------- Cargo.toml | 2 +- cli/Cargo.toml | 1 - cli/config/Cargo.toml | 2 +- cli/config/src/lib.rs | 29 ++++++++++++++++++----- cli/loader/Cargo.toml | 2 +- cli/loader/src/lib.rs | 29 +++++++++++++++-------- 7 files changed, 59 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b4951378..8609f024 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -487,27 +487,6 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" -[[package]] -name = "dirs" -version = "5.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" -dependencies = [ - "dirs-sys", -] - -[[package]] -name = "dirs-sys" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" -dependencies = [ - "libc", - "option-ext", - "redox_users", - "windows-sys 0.48.0", -] - [[package]] name = "displaydoc" version = "0.2.5" @@ -568,6 +547,17 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "etcetera" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136d1b5283a1ab77bd9257427ffd09d8667ced0570b6f938942bc7568ed5b943" +dependencies = [ + "cfg-if", + "home", + "windows-sys 0.48.0", +] + [[package]] name = "fallible-iterator" version = "0.3.0" @@ -1202,12 +1192,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "option-ext" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" - [[package]] name = "paste" version = "1.0.15" @@ -1356,17 +1340,6 @@ dependencies = [ "bitflags", ] -[[package]] -name = "redox_users" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" -dependencies = [ - "getrandom", - "libredox", - "thiserror 1.0.69", -] - [[package]] name = "regalloc2" version = "0.10.2" @@ -1830,7 +1803,6 @@ dependencies = [ "ctor", "ctrlc", "dialoguer", - "dirs", "encoding_rs", "filetime", "glob", @@ -1875,7 +1847,7 @@ name = "tree-sitter-config" version = "0.25.0" dependencies = [ "anyhow", - "dirs", + "etcetera", "serde", "serde_json", ] @@ -1922,7 +1894,7 @@ version = "0.25.0" dependencies = [ "anyhow", "cc", - "dirs", + "etcetera", "fs4", "indoc", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index fc280cec..31ace6c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,7 +109,7 @@ clap_complete_nushell = "4.5.4" ctor = "0.2.9" ctrlc = { version = "3.4.5", features = ["termination"] } dialoguer = { version = "0.11.0", features = ["fuzzy-select"] } -dirs = "5.0.1" +etcetera = "0.8.0" filetime = "0.2.25" fs4 = "0.12.0" git2 = "0.19.0" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 31b23a27..64aa95be 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -37,7 +37,6 @@ clap_complete_nushell.workspace = true ctor.workspace = true ctrlc.workspace = true dialoguer.workspace = true -dirs.workspace = true filetime.workspace = true glob.workspace = true heck.workspace = true diff --git a/cli/config/Cargo.toml b/cli/config/Cargo.toml index 5ad7b88f..e332db77 100644 --- a/cli/config/Cargo.toml +++ b/cli/config/Cargo.toml @@ -17,6 +17,6 @@ workspace = true [dependencies] anyhow.workspace = true -dirs.workspace = true +etcetera.workspace = true serde.workspace = true serde_json.workspace = true diff --git a/cli/config/src/lib.rs b/cli/config/src/lib.rs index 6d240c52..38165f2c 100644 --- a/cli/config/src/lib.rs +++ b/cli/config/src/lib.rs @@ -2,7 +2,8 @@ use std::{env, fs, path::PathBuf}; -use anyhow::{anyhow, Context, Result}; +use anyhow::{Context, Result}; +use etcetera::BaseStrategy as _; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -38,8 +39,24 @@ impl Config { return Ok(Some(xdg_path)); } - let legacy_path = dirs::home_dir() - .ok_or_else(|| anyhow!("Cannot determine home directory"))? + if cfg!(target_os = "macos") { + let legacy_apple_path = etcetera::base_strategy::Apple::new()? + .data_dir() // `$HOME/Library/Application Support/` + .join("tree-sitter") + .join("config.json"); + if legacy_apple_path.is_file() { + fs::create_dir_all(xdg_path.parent().unwrap())?; + fs::rename(&legacy_apple_path, &xdg_path)?; + println!( + "Warning: your config.json file has been automatically migrated from \"{}\" to \"{}\"", + legacy_apple_path.display(), + xdg_path.display() + ); + return Ok(Some(xdg_path)); + } + } + + let legacy_path = etcetera::home_dir()? .join(".tree-sitter") .join("config.json"); if legacy_path.is_file() { @@ -50,8 +67,8 @@ impl Config { } fn xdg_config_file() -> Result { - let xdg_path = dirs::config_dir() - .ok_or_else(|| anyhow!("Cannot determine config directory"))? + let xdg_path = etcetera::choose_base_strategy()? + .config_dir() .join("tree-sitter") .join("config.json"); Ok(xdg_path) @@ -63,7 +80,7 @@ impl Config { /// - Location specified by the path parameter if provided /// - `$TREE_SITTER_DIR/config.json`, if the `TREE_SITTER_DIR` environment variable is set /// - `tree-sitter/config.json` in your default user configuration directory, as determined by - /// [`dirs::config_dir`](https://docs.rs/dirs/*/dirs/fn.config_dir.html) + /// [`etcetera::choose_base_strategy`](https://docs.rs/etcetera/*/etcetera/#basestrategy) /// - `$HOME/.tree-sitter/config.json` as a fallback from where tree-sitter _used_ to store /// its configuration pub fn load(path: Option) -> Result { diff --git a/cli/loader/Cargo.toml b/cli/loader/Cargo.toml index 5e524b04..fc534b9a 100644 --- a/cli/loader/Cargo.toml +++ b/cli/loader/Cargo.toml @@ -26,7 +26,7 @@ default = ["tree-sitter-highlight", "tree-sitter-tags"] [dependencies] anyhow.workspace = true cc.workspace = true -dirs.workspace = true +etcetera.workspace = true fs4.workspace = true indoc.workspace = true lazy_static.workspace = true diff --git a/cli/loader/src/lib.rs b/cli/loader/src/lib.rs index 4c484892..d88ca5cc 100644 --- a/cli/loader/src/lib.rs +++ b/cli/loader/src/lib.rs @@ -20,6 +20,7 @@ use std::{ #[cfg(any(feature = "tree-sitter-highlight", feature = "tree-sitter-tags"))] use anyhow::Error; use anyhow::{anyhow, Context, Result}; +use etcetera::BaseStrategy as _; use fs4::fs_std::FileExt; use indoc::indoc; use lazy_static::lazy_static; @@ -258,7 +259,7 @@ where D: Deserializer<'de>, { let paths = Vec::::deserialize(deserializer)?; - let Some(home) = dirs::home_dir() else { + let Ok(home) = etcetera::home_dir() else { return Ok(paths); }; let standardized = paths @@ -281,7 +282,7 @@ fn standardize_path(path: PathBuf, home: &Path) -> PathBuf { impl Config { #[must_use] pub fn initial() -> Self { - let home_dir = dirs::home_dir().expect("Cannot determine home directory"); + let home_dir = etcetera::home_dir().expect("Cannot determine home directory"); Self { parser_directories: vec![ home_dir.join("github"), @@ -377,12 +378,22 @@ unsafe impl Sync for Loader {} impl Loader { pub fn new() -> Result { - let parser_lib_path = match env::var("TREE_SITTER_LIBDIR") { - Ok(path) => PathBuf::from(path), - _ => dirs::cache_dir() - .ok_or_else(|| anyhow!("Cannot determine cache directory"))? + let parser_lib_path = if let Ok(path) = env::var("TREE_SITTER_LIBDIR") { + PathBuf::from(path) + } else { + if cfg!(target_os = "macos") { + let legacy_apple_path = etcetera::base_strategy::Apple::new()? + .cache_dir() // `$HOME/Library/Caches/` + .join("tree-sitter"); + if legacy_apple_path.exists() && legacy_apple_path.is_dir() { + std::fs::remove_dir_all(legacy_apple_path)?; + } + } + + etcetera::choose_base_strategy()? + .cache_dir() .join("tree-sitter") - .join("lib"), + .join("lib") }; Ok(Self::with_parser_lib_path(parser_lib_path)) } @@ -733,8 +744,8 @@ impl Loader { .join("lock") .join(format!("{}.lock", config.name)) } else { - dirs::cache_dir() - .ok_or_else(|| anyhow!("Cannot determine cache directory"))? + etcetera::choose_base_strategy()? + .cache_dir() .join("tree-sitter") .join("lock") .join(format!("{}.lock", config.name))