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/// A default editorconfig file name to use when resolving project settings.
  9pub const EDITORCONFIG_NAME: &str = ".editorconfig";
 10
 11/// A custom data directory override, set only by `set_custom_data_dir`.
 12/// This is used to override the default data directory location.
 13/// The directory will be created if it doesn't exist when set.
 14static CUSTOM_DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
 15
 16/// The resolved data directory, combining custom override or platform defaults.
 17/// This is set once and cached for subsequent calls.
 18/// On macOS, this is `~/Library/Application Support/Zed`.
 19/// On Linux/FreeBSD, this is `$XDG_DATA_HOME/zed`.
 20/// On Windows, this is `%LOCALAPPDATA%\Zed`.
 21static CURRENT_DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
 22
 23/// The resolved config directory, combining custom override or platform defaults.
 24/// This is set once and cached for subsequent calls.
 25/// On macOS, this is `~/.config/zed`.
 26/// On Linux/FreeBSD, this is `$XDG_CONFIG_HOME/zed`.
 27/// On Windows, this is `%APPDATA%\Zed`.
 28static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
 29
 30/// Returns the relative path to the zed_server directory on the ssh host.
 31pub fn remote_server_dir_relative() -> &'static Path {
 32    Path::new(".zed_server")
 33}
 34
 35/// Sets a custom directory for all user data, overriding the default data directory.
 36/// This function must be called before any other path operations that depend on the data directory.
 37/// The directory will be created if it doesn't exist.
 38///
 39/// # Arguments
 40///
 41/// * `dir` - The path to use as the custom data directory. This will be used as the base
 42///           directory for all user data, including databases, extensions, and logs.
 43///
 44/// # Returns
 45///
 46/// A reference to the static `PathBuf` containing the custom data directory path.
 47///
 48/// # Panics
 49///
 50/// Panics if:
 51/// * Called after the data directory has been initialized (e.g., via `data_dir` or `config_dir`)
 52/// * The directory cannot be created
 53pub fn set_custom_data_dir(dir: &str) -> &'static PathBuf {
 54    if CURRENT_DATA_DIR.get().is_some() || CONFIG_DIR.get().is_some() {
 55        panic!("set_custom_data_dir called after data_dir or config_dir was initialized");
 56    }
 57    CUSTOM_DATA_DIR.get_or_init(|| {
 58        let path = PathBuf::from(dir);
 59        std::fs::create_dir_all(&path).expect("failed to create custom data directory");
 60        path
 61    })
 62}
 63
 64/// Returns the path to the configuration directory used by Zed.
 65pub fn config_dir() -> &'static PathBuf {
 66    CONFIG_DIR.get_or_init(|| {
 67        if let Some(custom_dir) = CUSTOM_DATA_DIR.get() {
 68            custom_dir.join("config")
 69        } else if cfg!(target_os = "windows") {
 70            dirs::config_dir()
 71                .expect("failed to determine RoamingAppData directory")
 72                .join("Zed")
 73        } else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
 74            if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
 75                flatpak_xdg_config.into()
 76            } else {
 77                dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
 78            }
 79            .join("zed")
 80        } else {
 81            home_dir().join(".config").join("zed")
 82        }
 83    })
 84}
 85
 86/// Returns the path to the data directory used by Zed.
 87pub fn data_dir() -> &'static PathBuf {
 88    CURRENT_DATA_DIR.get_or_init(|| {
 89        if let Some(custom_dir) = CUSTOM_DATA_DIR.get() {
 90            custom_dir.clone()
 91        } else if cfg!(target_os = "macos") {
 92            home_dir().join("Library/Application Support/Zed")
 93        } else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
 94            if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
 95                flatpak_xdg_data.into()
 96            } else {
 97                dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
 98            }
 99            .join("zed")
100        } else if cfg!(target_os = "windows") {
101            dirs::data_local_dir()
102                .expect("failed to determine LocalAppData directory")
103                .join("Zed")
104        } else {
105            config_dir().clone() // Fallback
106        }
107    })
108}
109/// Returns the path to the temp directory used by Zed.
110pub fn temp_dir() -> &'static PathBuf {
111    static TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
112    TEMP_DIR.get_or_init(|| {
113        if cfg!(target_os = "macos") {
114            return dirs::cache_dir()
115                .expect("failed to determine cachesDirectory directory")
116                .join("Zed");
117        }
118
119        if cfg!(target_os = "windows") {
120            return dirs::cache_dir()
121                .expect("failed to determine LocalAppData directory")
122                .join("Zed");
123        }
124
125        if cfg!(any(target_os = "linux", target_os = "freebsd")) {
126            return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
127                flatpak_xdg_cache.into()
128            } else {
129                dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
130            }
131            .join("zed");
132        }
133
134        home_dir().join(".cache").join("zed")
135    })
136}
137
138/// Returns the path to the logs directory.
139pub fn logs_dir() -> &'static PathBuf {
140    static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
141    LOGS_DIR.get_or_init(|| {
142        if cfg!(target_os = "macos") {
143            home_dir().join("Library/Logs/Zed")
144        } else {
145            data_dir().join("logs")
146        }
147    })
148}
149
150/// Returns the path to the Zed server directory on this SSH host.
151pub fn remote_server_state_dir() -> &'static PathBuf {
152    static REMOTE_SERVER_STATE: OnceLock<PathBuf> = OnceLock::new();
153    REMOTE_SERVER_STATE.get_or_init(|| data_dir().join("server_state"))
154}
155
156/// Returns the path to the `Zed.log` file.
157pub fn log_file() -> &'static PathBuf {
158    static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
159    LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
160}
161
162/// Returns the path to the `Zed.log.old` file.
163pub fn old_log_file() -> &'static PathBuf {
164    static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
165    OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
166}
167
168/// Returns the path to the database directory.
169pub fn database_dir() -> &'static PathBuf {
170    static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
171    DATABASE_DIR.get_or_init(|| data_dir().join("db"))
172}
173
174/// Returns the path to the crashes directory, if it exists for the current platform.
175pub fn crashes_dir() -> &'static Option<PathBuf> {
176    static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
177    CRASHES_DIR.get_or_init(|| {
178        cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
179    })
180}
181
182/// Returns the path to the retired crashes directory, if it exists for the current platform.
183pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
184    static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
185    CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
186}
187
188/// Returns the path to the `settings.json` file.
189pub fn settings_file() -> &'static PathBuf {
190    static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
191    SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
192}
193
194/// Returns the path to the `settings_backup.json` file.
195pub fn settings_backup_file() -> &'static PathBuf {
196    static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
197    SETTINGS_FILE.get_or_init(|| config_dir().join("settings_backup.json"))
198}
199
200/// Returns the path to the `keymap.json` file.
201pub fn keymap_file() -> &'static PathBuf {
202    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
203    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
204}
205
206/// Returns the path to the `keymap_backup.json` file.
207pub fn keymap_backup_file() -> &'static PathBuf {
208    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
209    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap_backup.json"))
210}
211
212/// Returns the path to the `tasks.json` file.
213pub fn tasks_file() -> &'static PathBuf {
214    static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
215    TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
216}
217
218/// Returns the path to the `debug.json` file.
219pub fn debug_scenarios_file() -> &'static PathBuf {
220    static DEBUG_SCENARIOS_FILE: OnceLock<PathBuf> = OnceLock::new();
221    DEBUG_SCENARIOS_FILE.get_or_init(|| config_dir().join("debug.json"))
222}
223
224/// Returns the path to the extensions directory.
225///
226/// This is where installed extensions are stored.
227pub fn extensions_dir() -> &'static PathBuf {
228    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
229    EXTENSIONS_DIR.get_or_init(|| data_dir().join("extensions"))
230}
231
232/// Returns the path to the extensions directory.
233///
234/// This is where installed extensions are stored on a remote.
235pub fn remote_extensions_dir() -> &'static PathBuf {
236    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
237    EXTENSIONS_DIR.get_or_init(|| data_dir().join("remote_extensions"))
238}
239
240/// Returns the path to the extensions directory.
241///
242/// This is where installed extensions are stored on a remote.
243pub fn remote_extensions_uploads_dir() -> &'static PathBuf {
244    static UPLOAD_DIR: OnceLock<PathBuf> = OnceLock::new();
245    UPLOAD_DIR.get_or_init(|| remote_extensions_dir().join("uploads"))
246}
247
248/// Returns the path to the themes directory.
249///
250/// This is where themes that are not provided by extensions are stored.
251pub fn themes_dir() -> &'static PathBuf {
252    static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
253    THEMES_DIR.get_or_init(|| config_dir().join("themes"))
254}
255
256/// Returns the path to the snippets directory.
257pub fn snippets_dir() -> &'static PathBuf {
258    static SNIPPETS_DIR: OnceLock<PathBuf> = OnceLock::new();
259    SNIPPETS_DIR.get_or_init(|| config_dir().join("snippets"))
260}
261
262/// Returns the path to the contexts directory.
263///
264/// This is where the saved contexts from the Assistant are stored.
265pub fn contexts_dir() -> &'static PathBuf {
266    static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
267    CONTEXTS_DIR.get_or_init(|| {
268        if cfg!(target_os = "macos") {
269            config_dir().join("conversations")
270        } else {
271            data_dir().join("conversations")
272        }
273    })
274}
275
276/// Returns the path to the contexts directory.
277///
278/// This is where the prompts for use with the Assistant are stored.
279pub fn prompts_dir() -> &'static PathBuf {
280    static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
281    PROMPTS_DIR.get_or_init(|| {
282        if cfg!(target_os = "macos") {
283            config_dir().join("prompts")
284        } else {
285            data_dir().join("prompts")
286        }
287    })
288}
289
290/// Returns the path to the prompt templates directory.
291///
292/// This is where the prompt templates for core features can be overridden with templates.
293///
294/// # Arguments
295///
296/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
297pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
298    if let Some(path) = repo_path {
299        let dev_path = path.join("assets").join("prompts");
300        if dev_path.exists() {
301            return dev_path;
302        }
303    }
304
305    static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
306    PROMPT_TEMPLATES_DIR
307        .get_or_init(|| {
308            if cfg!(target_os = "macos") {
309                config_dir().join("prompt_overrides")
310            } else {
311                data_dir().join("prompt_overrides")
312            }
313        })
314        .clone()
315}
316
317/// Returns the path to the semantic search's embeddings directory.
318///
319/// This is where the embeddings used to power semantic search are stored.
320pub fn embeddings_dir() -> &'static PathBuf {
321    static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
322    EMBEDDINGS_DIR.get_or_init(|| {
323        if cfg!(target_os = "macos") {
324            config_dir().join("embeddings")
325        } else {
326            data_dir().join("embeddings")
327        }
328    })
329}
330
331/// Returns the path to the languages directory.
332///
333/// This is where language servers are downloaded to for languages built-in to Zed.
334pub fn languages_dir() -> &'static PathBuf {
335    static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
336    LANGUAGES_DIR.get_or_init(|| data_dir().join("languages"))
337}
338
339/// Returns the path to the debug adapters directory
340///
341/// This is where debug adapters are downloaded to for DAPs that are built-in to Zed.
342pub fn debug_adapters_dir() -> &'static PathBuf {
343    static DEBUG_ADAPTERS_DIR: OnceLock<PathBuf> = OnceLock::new();
344    DEBUG_ADAPTERS_DIR.get_or_init(|| data_dir().join("debug_adapters"))
345}
346
347/// Returns the path to the Copilot directory.
348pub fn copilot_dir() -> &'static PathBuf {
349    static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
350    COPILOT_DIR.get_or_init(|| data_dir().join("copilot"))
351}
352
353/// Returns the path to the Supermaven directory.
354pub fn supermaven_dir() -> &'static PathBuf {
355    static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
356    SUPERMAVEN_DIR.get_or_init(|| data_dir().join("supermaven"))
357}
358
359/// Returns the path to the default Prettier directory.
360pub fn default_prettier_dir() -> &'static PathBuf {
361    static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
362    DEFAULT_PRETTIER_DIR.get_or_init(|| data_dir().join("prettier"))
363}
364
365/// Returns the path to the remote server binaries directory.
366pub fn remote_servers_dir() -> &'static PathBuf {
367    static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
368    REMOTE_SERVERS_DIR.get_or_init(|| data_dir().join("remote_servers"))
369}
370
371/// Returns the relative path to a `.zed` folder within a project.
372pub fn local_settings_folder_relative_path() -> &'static Path {
373    Path::new(".zed")
374}
375
376/// Returns the relative path to a `.vscode` folder within a project.
377pub fn local_vscode_folder_relative_path() -> &'static Path {
378    Path::new(".vscode")
379}
380
381/// Returns the relative path to a `settings.json` file within a project.
382pub fn local_settings_file_relative_path() -> &'static Path {
383    Path::new(".zed/settings.json")
384}
385
386/// Returns the relative path to a `tasks.json` file within a project.
387pub fn local_tasks_file_relative_path() -> &'static Path {
388    Path::new(".zed/tasks.json")
389}
390
391/// Returns the relative path to a `.vscode/tasks.json` file within a project.
392pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
393    Path::new(".vscode/tasks.json")
394}
395
396pub fn debug_task_file_name() -> &'static str {
397    "debug.json"
398}
399
400pub fn task_file_name() -> &'static str {
401    "tasks.json"
402}
403
404/// Returns the relative path to a `debug.json` file within a project.
405pub fn local_debug_file_relative_path() -> &'static Path {
406    Path::new(".zed/debug.json")
407}
408
409/// Returns the relative path to a `.vscode/launch.json` file within a project.
410pub fn local_vscode_launch_file_relative_path() -> &'static Path {
411    Path::new(".vscode/launch.json")
412}
413
414/// Returns the path to the vscode user settings file
415pub fn vscode_settings_file() -> &'static PathBuf {
416    static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
417    let rel_path = "Code/User/settings.json";
418    LOGS_DIR.get_or_init(|| {
419        if cfg!(target_os = "macos") {
420            home_dir()
421                .join("Library/Application Support")
422                .join(rel_path)
423        } else {
424            config_dir().join(rel_path)
425        }
426    })
427}