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 relative path to the zed_server directory on the ssh host.
9pub fn remote_server_dir_relative() -> &'static Path {
10 Path::new(".zed_server")
11}
12
13/// Returns the path to the configuration directory used by Zed.
14pub fn config_dir() -> &'static PathBuf {
15 static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
16 CONFIG_DIR.get_or_init(|| {
17 if cfg!(target_os = "windows") {
18 return dirs::config_dir()
19 .expect("failed to determine RoamingAppData directory")
20 .join("Zed");
21 }
22
23 if cfg!(target_os = "linux") {
24 return if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
25 flatpak_xdg_config.into()
26 } else {
27 dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
28 }
29 .join("zed");
30 }
31
32 home_dir().join(".config").join("zed")
33 })
34}
35
36/// Returns the path to the support directory used by Zed.
37pub fn support_dir() -> &'static PathBuf {
38 static SUPPORT_DIR: OnceLock<PathBuf> = OnceLock::new();
39 SUPPORT_DIR.get_or_init(|| {
40 if cfg!(target_os = "macos") {
41 return home_dir().join("Library/Application Support/Zed");
42 }
43
44 if cfg!(target_os = "linux") {
45 return if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
46 flatpak_xdg_data.into()
47 } else {
48 dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
49 }
50 .join("zed");
51 }
52
53 if cfg!(target_os = "windows") {
54 return dirs::data_local_dir()
55 .expect("failed to determine LocalAppData directory")
56 .join("Zed");
57 }
58
59 config_dir().clone()
60 })
61}
62
63/// Returns the path to the temp directory used by Zed.
64pub fn temp_dir() -> &'static PathBuf {
65 static TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
66 TEMP_DIR.get_or_init(|| {
67 if cfg!(target_os = "macos") {
68 return dirs::cache_dir()
69 .expect("failed to determine cachesDirectory directory")
70 .join("Zed");
71 }
72
73 if cfg!(target_os = "windows") {
74 return dirs::cache_dir()
75 .expect("failed to determine LocalAppData directory")
76 .join("Zed");
77 }
78
79 if cfg!(target_os = "linux") {
80 return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
81 flatpak_xdg_cache.into()
82 } else {
83 dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
84 }
85 .join("zed");
86 }
87
88 home_dir().join(".cache").join("zed")
89 })
90}
91
92/// Returns the path to the logs directory.
93pub fn logs_dir() -> &'static PathBuf {
94 static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
95 LOGS_DIR.get_or_init(|| {
96 if cfg!(target_os = "macos") {
97 home_dir().join("Library/Logs/Zed")
98 } else {
99 support_dir().join("logs")
100 }
101 })
102}
103
104/// Returns the path to the Zed server directory on this SSH host.
105pub fn remote_server_state_dir() -> &'static PathBuf {
106 static REMOTE_SERVER_STATE: OnceLock<PathBuf> = OnceLock::new();
107 REMOTE_SERVER_STATE.get_or_init(|| support_dir().join("server_state"))
108}
109
110/// Returns the path to the `Zed.log` file.
111pub fn log_file() -> &'static PathBuf {
112 static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
113 LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
114}
115
116/// Returns the path to the `Zed.log.old` file.
117pub fn old_log_file() -> &'static PathBuf {
118 static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
119 OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
120}
121
122/// Returns the path to the database directory.
123pub fn database_dir() -> &'static PathBuf {
124 static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
125 DATABASE_DIR.get_or_init(|| support_dir().join("db"))
126}
127
128/// Returns the path to the crashes directory, if it exists for the current platform.
129pub fn crashes_dir() -> &'static Option<PathBuf> {
130 static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
131 CRASHES_DIR.get_or_init(|| {
132 cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
133 })
134}
135
136/// Returns the path to the retired crashes directory, if it exists for the current platform.
137pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
138 static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
139 CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
140}
141
142/// Returns the path to the `settings.json` file.
143pub fn settings_file() -> &'static PathBuf {
144 static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
145 SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
146}
147
148/// Returns the path to the `keymap.json` file.
149pub fn keymap_file() -> &'static PathBuf {
150 static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
151 KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
152}
153
154/// Returns the path to the `tasks.json` file.
155pub fn tasks_file() -> &'static PathBuf {
156 static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
157 TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
158}
159
160/// Returns the path to the extensions directory.
161///
162/// This is where installed extensions are stored.
163pub fn extensions_dir() -> &'static PathBuf {
164 static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
165 EXTENSIONS_DIR.get_or_init(|| support_dir().join("extensions"))
166}
167
168/// Returns the path to the themes directory.
169///
170/// This is where themes that are not provided by extensions are stored.
171pub fn themes_dir() -> &'static PathBuf {
172 static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
173 THEMES_DIR.get_or_init(|| config_dir().join("themes"))
174}
175
176/// Returns the path to the contexts directory.
177///
178/// This is where the saved contexts from the Assistant are stored.
179pub fn contexts_dir() -> &'static PathBuf {
180 static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
181 CONTEXTS_DIR.get_or_init(|| {
182 if cfg!(target_os = "macos") {
183 config_dir().join("conversations")
184 } else {
185 support_dir().join("conversations")
186 }
187 })
188}
189
190/// Returns the path to the contexts directory.
191///
192/// This is where the prompts for use with the Assistant are stored.
193pub fn prompts_dir() -> &'static PathBuf {
194 static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
195 PROMPTS_DIR.get_or_init(|| {
196 if cfg!(target_os = "macos") {
197 config_dir().join("prompts")
198 } else {
199 support_dir().join("prompts")
200 }
201 })
202}
203
204/// Returns the path to the prompt templates directory.
205///
206/// This is where the prompt templates for core features can be overridden with templates.
207///
208/// # Arguments
209///
210/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
211pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
212 if let Some(path) = repo_path {
213 let dev_path = path.join("assets").join("prompts");
214 if dev_path.exists() {
215 return dev_path;
216 }
217 }
218
219 static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
220 PROMPT_TEMPLATES_DIR
221 .get_or_init(|| {
222 if cfg!(target_os = "macos") {
223 config_dir().join("prompt_overrides")
224 } else {
225 support_dir().join("prompt_overrides")
226 }
227 })
228 .clone()
229}
230
231/// Returns the path to the semantic search's embeddings directory.
232///
233/// This is where the embeddings used to power semantic search are stored.
234pub fn embeddings_dir() -> &'static PathBuf {
235 static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
236 EMBEDDINGS_DIR.get_or_init(|| {
237 if cfg!(target_os = "macos") {
238 config_dir().join("embeddings")
239 } else {
240 support_dir().join("embeddings")
241 }
242 })
243}
244
245/// Returns the path to the languages directory.
246///
247/// This is where language servers are downloaded to for languages built-in to Zed.
248pub fn languages_dir() -> &'static PathBuf {
249 static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
250 LANGUAGES_DIR.get_or_init(|| support_dir().join("languages"))
251}
252
253/// Returns the path to the Copilot directory.
254pub fn copilot_dir() -> &'static PathBuf {
255 static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
256 COPILOT_DIR.get_or_init(|| support_dir().join("copilot"))
257}
258
259/// Returns the path to the Supermaven directory.
260pub fn supermaven_dir() -> &'static PathBuf {
261 static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
262 SUPERMAVEN_DIR.get_or_init(|| support_dir().join("supermaven"))
263}
264
265/// Returns the path to the default Prettier directory.
266pub fn default_prettier_dir() -> &'static PathBuf {
267 static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
268 DEFAULT_PRETTIER_DIR.get_or_init(|| support_dir().join("prettier"))
269}
270
271/// Returns the path to the remote server binaries directory.
272pub fn remote_servers_dir() -> &'static PathBuf {
273 static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
274 REMOTE_SERVERS_DIR.get_or_init(|| support_dir().join("remote_servers"))
275}
276
277/// Returns the relative path to a `.zed` folder within a project.
278pub fn local_settings_folder_relative_path() -> &'static Path {
279 Path::new(".zed")
280}
281
282/// Returns the relative path to a `settings.json` file within a project.
283pub fn local_settings_file_relative_path() -> &'static Path {
284 Path::new(".zed/settings.json")
285}
286
287/// Returns the relative path to a `tasks.json` file within a project.
288pub fn local_tasks_file_relative_path() -> &'static Path {
289 Path::new(".zed/tasks.json")
290}
291
292/// Returns the relative path to a `.vscode/tasks.json` file within a project.
293pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
294 Path::new(".vscode/tasks.json")
295}
296
297/// A default editorconfig file name to use when resolving project settings.
298pub const EDITORCONFIG_NAME: &str = ".editorconfig";