fix(cli): use xdg config directory on macOS

fix: address feedback
This commit is contained in:
WillLillis 2024-12-22 19:10:29 -05:00 committed by Amaan Qureshi
parent b747261929
commit faf97b896a
7 changed files with 59 additions and 60 deletions

54
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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

View file

@ -17,6 +17,6 @@ workspace = true
[dependencies]
anyhow.workspace = true
dirs.workspace = true
etcetera.workspace = true
serde.workspace = true
serde_json.workspace = true

View file

@ -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<PathBuf> {
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<PathBuf>) -> Result<Self> {

View file

@ -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

View file

@ -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::<PathBuf>::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<Self> {
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))