1//! Paths to locations used by Zed.
2
3use std::path::{Path, PathBuf};
4use std::sync::OnceLock;
5
6pub use util::paths::home_dir;
7
8/// Returns the path to the configuration directory used by Zed.
9pub fn config_dir() -> &'static PathBuf {
10 static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
11 CONFIG_DIR.get_or_init(|| {
12 if cfg!(target_os = "windows") {
13 return dirs::config_dir()
14 .expect("failed to determine RoamingAppData directory")
15 .join("Zed");
16 }
17
18 if cfg!(target_os = "linux") {
19 return if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
20 flatpak_xdg_config.into()
21 } else {
22 dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
23 }
24 .join("zed");
25 }
26
27 home_dir().join(".config").join("zed")
28 })
29}
30
31/// Returns the path to the support directory used by Zed.
32pub fn support_dir() -> &'static PathBuf {
33 static SUPPORT_DIR: OnceLock<PathBuf> = OnceLock::new();
34 SUPPORT_DIR.get_or_init(|| {
35 if cfg!(target_os = "macos") {
36 return home_dir().join("Library/Application Support/Zed");
37 }
38
39 if cfg!(target_os = "linux") {
40 return if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
41 flatpak_xdg_data.into()
42 } else {
43 dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
44 }
45 .join("zed");
46 }
47
48 if cfg!(target_os = "windows") {
49 return dirs::data_local_dir()
50 .expect("failed to determine LocalAppData directory")
51 .join("Zed");
52 }
53
54 config_dir().clone()
55 })
56}
57
58/// Returns the path to the temp directory used by Zed.
59pub fn temp_dir() -> &'static PathBuf {
60 static TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
61 TEMP_DIR.get_or_init(|| {
62 if cfg!(target_os = "windows") {
63 return dirs::cache_dir()
64 .expect("failed to determine LocalAppData directory")
65 .join("Zed");
66 }
67
68 if cfg!(target_os = "linux") {
69 return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
70 flatpak_xdg_cache.into()
71 } else {
72 dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
73 }
74 .join("zed");
75 }
76
77 home_dir().join(".cache").join("zed")
78 })
79}
80
81/// Returns the path to the logs directory.
82pub fn logs_dir() -> &'static PathBuf {
83 static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
84 LOGS_DIR.get_or_init(|| {
85 if cfg!(target_os = "macos") {
86 home_dir().join("Library/Logs/Zed")
87 } else {
88 support_dir().join("logs")
89 }
90 })
91}
92
93/// Returns the path to the `Zed.log` file.
94pub fn log_file() -> &'static PathBuf {
95 static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
96 LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
97}
98
99/// Returns the path to the `Zed.log.old` file.
100pub fn old_log_file() -> &'static PathBuf {
101 static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
102 OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
103}
104
105/// Returns the path to the database directory.
106pub fn database_dir() -> &'static PathBuf {
107 static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
108 DATABASE_DIR.get_or_init(|| support_dir().join("db"))
109}
110
111/// Returns the path to the crashes directory, if it exists for the current platform.
112pub fn crashes_dir() -> &'static Option<PathBuf> {
113 static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
114 CRASHES_DIR.get_or_init(|| {
115 cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
116 })
117}
118
119/// Returns the path to the retired crashes directory, if it exists for the current platform.
120pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
121 static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
122 CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
123}
124
125/// Returns the path to the `settings.json` file.
126pub fn settings_file() -> &'static PathBuf {
127 static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
128 SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
129}
130
131/// Returns the path to the `keymap.json` file.
132pub fn keymap_file() -> &'static PathBuf {
133 static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
134 KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
135}
136
137/// Returns the path to the `tasks.json` file.
138pub fn tasks_file() -> &'static PathBuf {
139 static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
140 TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
141}
142
143/// Returns the path to the extensions directory.
144///
145/// This is where installed extensions are stored.
146pub fn extensions_dir() -> &'static PathBuf {
147 static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
148 EXTENSIONS_DIR.get_or_init(|| support_dir().join("extensions"))
149}
150
151/// Returns the path to the themes directory.
152///
153/// This is where themes that are not provided by extensions are stored.
154pub fn themes_dir() -> &'static PathBuf {
155 static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
156 THEMES_DIR.get_or_init(|| config_dir().join("themes"))
157}
158
159/// Returns the path to the contexts directory.
160///
161/// This is where the saved contexts from the Assistant are stored.
162pub fn contexts_dir() -> &'static PathBuf {
163 static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
164 CONTEXTS_DIR.get_or_init(|| {
165 if cfg!(target_os = "macos") {
166 config_dir().join("conversations")
167 } else {
168 support_dir().join("conversations")
169 }
170 })
171}
172
173/// Returns the path to the contexts directory.
174///
175/// This is where the prompts for use with the Assistant are stored.
176pub fn prompts_dir() -> &'static PathBuf {
177 static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
178 PROMPTS_DIR.get_or_init(|| {
179 if cfg!(target_os = "macos") {
180 config_dir().join("prompts")
181 } else {
182 support_dir().join("prompts")
183 }
184 })
185}
186
187/// Returns the path to the semantic search's embeddings directory.
188///
189/// This is where the embeddings used to power semantic search are stored.
190pub fn embeddings_dir() -> &'static PathBuf {
191 static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
192 EMBEDDINGS_DIR.get_or_init(|| {
193 if cfg!(target_os = "macos") {
194 config_dir().join("embeddings")
195 } else {
196 support_dir().join("embeddings")
197 }
198 })
199}
200
201/// Returns the path to the languages directory.
202///
203/// This is where language servers are downloaded to for languages built-in to Zed.
204pub fn languages_dir() -> &'static PathBuf {
205 static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
206 LANGUAGES_DIR.get_or_init(|| support_dir().join("languages"))
207}
208
209/// Returns the path to the Copilot directory.
210pub fn copilot_dir() -> &'static PathBuf {
211 static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
212 COPILOT_DIR.get_or_init(|| support_dir().join("copilot"))
213}
214
215/// Returns the path to the Supermaven directory.
216pub fn supermaven_dir() -> &'static PathBuf {
217 static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
218 SUPERMAVEN_DIR.get_or_init(|| support_dir().join("supermaven"))
219}
220
221/// Returns the path to the default Prettier directory.
222pub fn default_prettier_dir() -> &'static PathBuf {
223 static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
224 DEFAULT_PRETTIER_DIR.get_or_init(|| support_dir().join("prettier"))
225}
226
227/// Returns the relative path to a `.zed` folder within a project.
228pub fn local_settings_folder_relative_path() -> &'static Path {
229 static LOCAL_SETTINGS_FOLDER_RELATIVE_PATH: OnceLock<&Path> = OnceLock::new();
230 LOCAL_SETTINGS_FOLDER_RELATIVE_PATH.get_or_init(|| Path::new(".zed"))
231}
232
233/// Returns the relative path to a `settings.json` file within a project.
234pub fn local_settings_file_relative_path() -> &'static Path {
235 static LOCAL_SETTINGS_FILE_RELATIVE_PATH: OnceLock<&Path> = OnceLock::new();
236 LOCAL_SETTINGS_FILE_RELATIVE_PATH.get_or_init(|| Path::new(".zed/settings.json"))
237}
238
239/// Returns the relative path to a `tasks.json` file within a project.
240pub fn local_tasks_file_relative_path() -> &'static Path {
241 static LOCAL_TASKS_FILE_RELATIVE_PATH: OnceLock<&Path> = OnceLock::new();
242 LOCAL_TASKS_FILE_RELATIVE_PATH.get_or_init(|| Path::new(".zed/tasks.json"))
243}
244
245/// Returns the relative path to a `.vscode/tasks.json` file within a project.
246pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
247 static LOCAL_VSCODE_TASKS_FILE_RELATIVE_PATH: OnceLock<&Path> = OnceLock::new();
248 LOCAL_VSCODE_TASKS_FILE_RELATIVE_PATH.get_or_init(|| Path::new(".vscode/tasks.json"))
249}