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 `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 extensions directory.
169///
170/// This is where installed extensions are stored on a remote.
171pub fn remote_extensions_dir() -> &'static PathBuf {
172    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
173    EXTENSIONS_DIR.get_or_init(|| support_dir().join("remote_extensions"))
174}
175
176/// Returns the path to the extensions directory.
177///
178/// This is where installed extensions are stored on a remote.
179pub fn remote_extensions_uploads_dir() -> &'static PathBuf {
180    static UPLOAD_DIR: OnceLock<PathBuf> = OnceLock::new();
181    UPLOAD_DIR.get_or_init(|| remote_extensions_dir().join("uploads"))
182}
183
184/// Returns the path to the themes directory.
185///
186/// This is where themes that are not provided by extensions are stored.
187pub fn themes_dir() -> &'static PathBuf {
188    static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
189    THEMES_DIR.get_or_init(|| config_dir().join("themes"))
190}
191
192/// Returns the path to the snippets directory.
193pub fn snippets_dir() -> &'static PathBuf {
194    static SNIPPETS_DIR: OnceLock<PathBuf> = OnceLock::new();
195    SNIPPETS_DIR.get_or_init(|| config_dir().join("snippets"))
196}
197
198/// Returns the path to the contexts directory.
199///
200/// This is where the saved contexts from the Assistant are stored.
201pub fn contexts_dir() -> &'static PathBuf {
202    static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
203    CONTEXTS_DIR.get_or_init(|| {
204        if cfg!(target_os = "macos") {
205            config_dir().join("conversations")
206        } else {
207            support_dir().join("conversations")
208        }
209    })
210}
211
212/// Returns the path to the contexts directory.
213///
214/// This is where the prompts for use with the Assistant are stored.
215pub fn prompts_dir() -> &'static PathBuf {
216    static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
217    PROMPTS_DIR.get_or_init(|| {
218        if cfg!(target_os = "macos") {
219            config_dir().join("prompts")
220        } else {
221            support_dir().join("prompts")
222        }
223    })
224}
225
226/// Returns the path to the prompt templates directory.
227///
228/// This is where the prompt templates for core features can be overridden with templates.
229///
230/// # Arguments
231///
232/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
233pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
234    if let Some(path) = repo_path {
235        let dev_path = path.join("assets").join("prompts");
236        if dev_path.exists() {
237            return dev_path;
238        }
239    }
240
241    static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
242    PROMPT_TEMPLATES_DIR
243        .get_or_init(|| {
244            if cfg!(target_os = "macos") {
245                config_dir().join("prompt_overrides")
246            } else {
247                support_dir().join("prompt_overrides")
248            }
249        })
250        .clone()
251}
252
253/// Returns the path to the semantic search's embeddings directory.
254///
255/// This is where the embeddings used to power semantic search are stored.
256pub fn embeddings_dir() -> &'static PathBuf {
257    static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
258    EMBEDDINGS_DIR.get_or_init(|| {
259        if cfg!(target_os = "macos") {
260            config_dir().join("embeddings")
261        } else {
262            support_dir().join("embeddings")
263        }
264    })
265}
266
267/// Returns the path to the languages directory.
268///
269/// This is where language servers are downloaded to for languages built-in to Zed.
270pub fn languages_dir() -> &'static PathBuf {
271    static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
272    LANGUAGES_DIR.get_or_init(|| support_dir().join("languages"))
273}
274
275/// Returns the path to the Copilot directory.
276pub fn copilot_dir() -> &'static PathBuf {
277    static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
278    COPILOT_DIR.get_or_init(|| support_dir().join("copilot"))
279}
280
281/// Returns the path to the Supermaven directory.
282pub fn supermaven_dir() -> &'static PathBuf {
283    static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
284    SUPERMAVEN_DIR.get_or_init(|| support_dir().join("supermaven"))
285}
286
287/// Returns the path to the default Prettier directory.
288pub fn default_prettier_dir() -> &'static PathBuf {
289    static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
290    DEFAULT_PRETTIER_DIR.get_or_init(|| support_dir().join("prettier"))
291}
292
293/// Returns the path to the remote server binaries directory.
294pub fn remote_servers_dir() -> &'static PathBuf {
295    static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
296    REMOTE_SERVERS_DIR.get_or_init(|| support_dir().join("remote_servers"))
297}
298
299/// Returns the relative path to a `.zed` folder within a project.
300pub fn local_settings_folder_relative_path() -> &'static Path {
301    Path::new(".zed")
302}
303
304/// Returns the relative path to a `settings.json` file within a project.
305pub fn local_settings_file_relative_path() -> &'static Path {
306    Path::new(".zed/settings.json")
307}
308
309/// Returns the relative path to a `tasks.json` file within a project.
310pub fn local_tasks_file_relative_path() -> &'static Path {
311    Path::new(".zed/tasks.json")
312}
313
314/// Returns the relative path to a `.vscode/tasks.json` file within a project.
315pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
316    Path::new(".vscode/tasks.json")
317}
318
319/// A default editorconfig file name to use when resolving project settings.
320pub const EDITORCONFIG_NAME: &str = ".editorconfig";