paths.rs

  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!(any(target_os = "linux", target_os = "freebsd")) {
 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!(any(target_os = "linux", target_os = "freebsd")) {
 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!(any(target_os = "linux", target_os = "freebsd")) {
 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 `settings_backup.json` file.
149pub fn settings_backup_file() -> &'static PathBuf {
150    static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
151    SETTINGS_FILE.get_or_init(|| config_dir().join("settings_backup.json"))
152}
153
154/// Returns the path to the `keymap.json` file.
155pub fn keymap_file() -> &'static PathBuf {
156    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
157    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
158}
159
160/// Returns the path to the `keymap_backup.json` file.
161pub fn keymap_backup_file() -> &'static PathBuf {
162    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
163    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap_backup.json"))
164}
165
166/// Returns the path to the `tasks.json` file.
167pub fn tasks_file() -> &'static PathBuf {
168    static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
169    TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
170}
171
172/// Returns the path to the `debug.json` file.
173pub fn debug_tasks_file() -> &'static PathBuf {
174    static DEBUG_TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
175    DEBUG_TASKS_FILE.get_or_init(|| config_dir().join("debug.json"))
176}
177
178/// Returns the path to the extensions directory.
179///
180/// This is where installed extensions are stored.
181pub fn extensions_dir() -> &'static PathBuf {
182    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
183    EXTENSIONS_DIR.get_or_init(|| support_dir().join("extensions"))
184}
185
186/// Returns the path to the extensions directory.
187///
188/// This is where installed extensions are stored on a remote.
189pub fn remote_extensions_dir() -> &'static PathBuf {
190    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
191    EXTENSIONS_DIR.get_or_init(|| support_dir().join("remote_extensions"))
192}
193
194/// Returns the path to the extensions directory.
195///
196/// This is where installed extensions are stored on a remote.
197pub fn remote_extensions_uploads_dir() -> &'static PathBuf {
198    static UPLOAD_DIR: OnceLock<PathBuf> = OnceLock::new();
199    UPLOAD_DIR.get_or_init(|| remote_extensions_dir().join("uploads"))
200}
201
202/// Returns the path to the themes directory.
203///
204/// This is where themes that are not provided by extensions are stored.
205pub fn themes_dir() -> &'static PathBuf {
206    static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
207    THEMES_DIR.get_or_init(|| config_dir().join("themes"))
208}
209
210/// Returns the path to the snippets directory.
211pub fn snippets_dir() -> &'static PathBuf {
212    static SNIPPETS_DIR: OnceLock<PathBuf> = OnceLock::new();
213    SNIPPETS_DIR.get_or_init(|| config_dir().join("snippets"))
214}
215
216/// Returns the path to the contexts directory.
217///
218/// This is where the saved contexts from the Assistant are stored.
219pub fn contexts_dir() -> &'static PathBuf {
220    static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
221    CONTEXTS_DIR.get_or_init(|| {
222        if cfg!(target_os = "macos") {
223            config_dir().join("conversations")
224        } else {
225            support_dir().join("conversations")
226        }
227    })
228}
229
230/// Returns the path to the contexts directory.
231///
232/// This is where the prompts for use with the Assistant are stored.
233pub fn prompts_dir() -> &'static PathBuf {
234    static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
235    PROMPTS_DIR.get_or_init(|| {
236        if cfg!(target_os = "macos") {
237            config_dir().join("prompts")
238        } else {
239            support_dir().join("prompts")
240        }
241    })
242}
243
244/// Returns the path to the prompt templates directory.
245///
246/// This is where the prompt templates for core features can be overridden with templates.
247///
248/// # Arguments
249///
250/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
251pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
252    if let Some(path) = repo_path {
253        let dev_path = path.join("assets").join("prompts");
254        if dev_path.exists() {
255            return dev_path;
256        }
257    }
258
259    static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
260    PROMPT_TEMPLATES_DIR
261        .get_or_init(|| {
262            if cfg!(target_os = "macos") {
263                config_dir().join("prompt_overrides")
264            } else {
265                support_dir().join("prompt_overrides")
266            }
267        })
268        .clone()
269}
270
271/// Returns the path to the semantic search's embeddings directory.
272///
273/// This is where the embeddings used to power semantic search are stored.
274pub fn embeddings_dir() -> &'static PathBuf {
275    static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
276    EMBEDDINGS_DIR.get_or_init(|| {
277        if cfg!(target_os = "macos") {
278            config_dir().join("embeddings")
279        } else {
280            support_dir().join("embeddings")
281        }
282    })
283}
284
285/// Returns the path to the languages directory.
286///
287/// This is where language servers are downloaded to for languages built-in to Zed.
288pub fn languages_dir() -> &'static PathBuf {
289    static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
290    LANGUAGES_DIR.get_or_init(|| support_dir().join("languages"))
291}
292
293/// Returns the path to the debug adapters directory
294///
295/// This is where debug adapters are downloaded to for DAPs that are built-in to Zed.
296pub fn debug_adapters_dir() -> &'static PathBuf {
297    static DEBUG_ADAPTERS_DIR: OnceLock<PathBuf> = OnceLock::new();
298    DEBUG_ADAPTERS_DIR.get_or_init(|| support_dir().join("debug_adapters"))
299}
300
301/// Returns the path to the Copilot directory.
302pub fn copilot_dir() -> &'static PathBuf {
303    static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
304    COPILOT_DIR.get_or_init(|| support_dir().join("copilot"))
305}
306
307/// Returns the path to the Supermaven directory.
308pub fn supermaven_dir() -> &'static PathBuf {
309    static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
310    SUPERMAVEN_DIR.get_or_init(|| support_dir().join("supermaven"))
311}
312
313/// Returns the path to the default Prettier directory.
314pub fn default_prettier_dir() -> &'static PathBuf {
315    static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
316    DEFAULT_PRETTIER_DIR.get_or_init(|| support_dir().join("prettier"))
317}
318
319/// Returns the path to the remote server binaries directory.
320pub fn remote_servers_dir() -> &'static PathBuf {
321    static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
322    REMOTE_SERVERS_DIR.get_or_init(|| support_dir().join("remote_servers"))
323}
324
325/// Returns the relative path to a `.zed` folder within a project.
326pub fn local_settings_folder_relative_path() -> &'static Path {
327    Path::new(".zed")
328}
329
330/// Returns the relative path to a `settings.json` file within a project.
331pub fn local_settings_file_relative_path() -> &'static Path {
332    Path::new(".zed/settings.json")
333}
334
335/// Returns the relative path to a `tasks.json` file within a project.
336pub fn local_tasks_file_relative_path() -> &'static Path {
337    Path::new(".zed/tasks.json")
338}
339
340/// Returns the relative path to a `.vscode/tasks.json` file within a project.
341pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
342    Path::new(".vscode/tasks.json")
343}
344
345pub fn debug_task_file_name() -> &'static str {
346    "debug.json"
347}
348
349pub fn task_file_name() -> &'static str {
350    "tasks.json"
351}
352
353/// Returns the relative path to a `launch.json` file within a project.
354pub fn local_debug_file_relative_path() -> &'static Path {
355    Path::new(".zed/debug.json")
356}
357
358/// Returns the relative path to a `.vscode/launch.json` file within a project.
359pub fn local_vscode_launch_file_relative_path() -> &'static Path {
360    Path::new(".vscode/launch.json")
361}
362
363/// A default editorconfig file name to use when resolving project settings.
364pub const EDITORCONFIG_NAME: &str = ".editorconfig";