Skip to content

Commit faf97b8

Browse files
WillLillisamaanq
authored andcommitted
fix(cli): use xdg config directory on macOS
fix: address feedback
1 parent b747261 commit faf97b8

File tree

7 files changed

+59
-60
lines changed

7 files changed

+59
-60
lines changed

Cargo.lock

Lines changed: 13 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ clap_complete_nushell = "4.5.4"
109109
ctor = "0.2.9"
110110
ctrlc = { version = "3.4.5", features = ["termination"] }
111111
dialoguer = { version = "0.11.0", features = ["fuzzy-select"] }
112-
dirs = "5.0.1"
112+
etcetera = "0.8.0"
113113
filetime = "0.2.25"
114114
fs4 = "0.12.0"
115115
git2 = "0.19.0"

cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ clap_complete_nushell.workspace = true
3737
ctor.workspace = true
3838
ctrlc.workspace = true
3939
dialoguer.workspace = true
40-
dirs.workspace = true
4140
filetime.workspace = true
4241
glob.workspace = true
4342
heck.workspace = true

cli/config/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ workspace = true
1717

1818
[dependencies]
1919
anyhow.workspace = true
20-
dirs.workspace = true
20+
etcetera.workspace = true
2121
serde.workspace = true
2222
serde_json.workspace = true

cli/config/src/lib.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
use std::{env, fs, path::PathBuf};
44

5-
use anyhow::{anyhow, Context, Result};
5+
use anyhow::{Context, Result};
6+
use etcetera::BaseStrategy as _;
67
use serde::{Deserialize, Serialize};
78
use serde_json::Value;
89

@@ -38,8 +39,24 @@ impl Config {
3839
return Ok(Some(xdg_path));
3940
}
4041

41-
let legacy_path = dirs::home_dir()
42-
.ok_or_else(|| anyhow!("Cannot determine home directory"))?
42+
if cfg!(target_os = "macos") {
43+
let legacy_apple_path = etcetera::base_strategy::Apple::new()?
44+
.data_dir() // `$HOME/Library/Application Support/`
45+
.join("tree-sitter")
46+
.join("config.json");
47+
if legacy_apple_path.is_file() {
48+
fs::create_dir_all(xdg_path.parent().unwrap())?;
49+
fs::rename(&legacy_apple_path, &xdg_path)?;
50+
println!(
51+
"Warning: your config.json file has been automatically migrated from \"{}\" to \"{}\"",
52+
legacy_apple_path.display(),
53+
xdg_path.display()
54+
);
55+
return Ok(Some(xdg_path));
56+
}
57+
}
58+
59+
let legacy_path = etcetera::home_dir()?
4360
.join(".tree-sitter")
4461
.join("config.json");
4562
if legacy_path.is_file() {
@@ -50,8 +67,8 @@ impl Config {
5067
}
5168

5269
fn xdg_config_file() -> Result<PathBuf> {
53-
let xdg_path = dirs::config_dir()
54-
.ok_or_else(|| anyhow!("Cannot determine config directory"))?
70+
let xdg_path = etcetera::choose_base_strategy()?
71+
.config_dir()
5572
.join("tree-sitter")
5673
.join("config.json");
5774
Ok(xdg_path)
@@ -63,7 +80,7 @@ impl Config {
6380
/// - Location specified by the path parameter if provided
6481
/// - `$TREE_SITTER_DIR/config.json`, if the `TREE_SITTER_DIR` environment variable is set
6582
/// - `tree-sitter/config.json` in your default user configuration directory, as determined by
66-
/// [`dirs::config_dir`](https://docs.rs/dirs/*/dirs/fn.config_dir.html)
83+
/// [`etcetera::choose_base_strategy`](https://docs.rs/etcetera/*/etcetera/#basestrategy)
6784
/// - `$HOME/.tree-sitter/config.json` as a fallback from where tree-sitter _used_ to store
6885
/// its configuration
6986
pub fn load(path: Option<PathBuf>) -> Result<Self> {

cli/loader/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ default = ["tree-sitter-highlight", "tree-sitter-tags"]
2626
[dependencies]
2727
anyhow.workspace = true
2828
cc.workspace = true
29-
dirs.workspace = true
29+
etcetera.workspace = true
3030
fs4.workspace = true
3131
indoc.workspace = true
3232
lazy_static.workspace = true

cli/loader/src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::{
2020
#[cfg(any(feature = "tree-sitter-highlight", feature = "tree-sitter-tags"))]
2121
use anyhow::Error;
2222
use anyhow::{anyhow, Context, Result};
23+
use etcetera::BaseStrategy as _;
2324
use fs4::fs_std::FileExt;
2425
use indoc::indoc;
2526
use lazy_static::lazy_static;
@@ -258,7 +259,7 @@ where
258259
D: Deserializer<'de>,
259260
{
260261
let paths = Vec::<PathBuf>::deserialize(deserializer)?;
261-
let Some(home) = dirs::home_dir() else {
262+
let Ok(home) = etcetera::home_dir() else {
262263
return Ok(paths);
263264
};
264265
let standardized = paths
@@ -281,7 +282,7 @@ fn standardize_path(path: PathBuf, home: &Path) -> PathBuf {
281282
impl Config {
282283
#[must_use]
283284
pub fn initial() -> Self {
284-
let home_dir = dirs::home_dir().expect("Cannot determine home directory");
285+
let home_dir = etcetera::home_dir().expect("Cannot determine home directory");
285286
Self {
286287
parser_directories: vec![
287288
home_dir.join("github"),
@@ -377,12 +378,22 @@ unsafe impl Sync for Loader {}
377378

378379
impl Loader {
379380
pub fn new() -> Result<Self> {
380-
let parser_lib_path = match env::var("TREE_SITTER_LIBDIR") {
381-
Ok(path) => PathBuf::from(path),
382-
_ => dirs::cache_dir()
383-
.ok_or_else(|| anyhow!("Cannot determine cache directory"))?
381+
let parser_lib_path = if let Ok(path) = env::var("TREE_SITTER_LIBDIR") {
382+
PathBuf::from(path)
383+
} else {
384+
if cfg!(target_os = "macos") {
385+
let legacy_apple_path = etcetera::base_strategy::Apple::new()?
386+
.cache_dir() // `$HOME/Library/Caches/`
387+
.join("tree-sitter");
388+
if legacy_apple_path.exists() && legacy_apple_path.is_dir() {
389+
std::fs::remove_dir_all(legacy_apple_path)?;
390+
}
391+
}
392+
393+
etcetera::choose_base_strategy()?
394+
.cache_dir()
384395
.join("tree-sitter")
385-
.join("lib"),
396+
.join("lib")
386397
};
387398
Ok(Self::with_parser_lib_path(parser_lib_path))
388399
}
@@ -733,8 +744,8 @@ impl Loader {
733744
.join("lock")
734745
.join(format!("{}.lock", config.name))
735746
} else {
736-
dirs::cache_dir()
737-
.ok_or_else(|| anyhow!("Cannot determine cache directory"))?
747+
etcetera::choose_base_strategy()?
748+
.cache_dir()
738749
.join("tree-sitter")
739750
.join("lock")
740751
.join(format!("{}.lock", config.name))

0 commit comments

Comments
 (0)