1//! Paths to locations used by Zed.
  2
  3use std::env;
  4use std::path::{Path, PathBuf};
  5use std::sync::{LazyLock, OnceLock};
  6
  7pub use util::paths::home_dir;
  8use util::rel_path::RelPath;
  9
 10/// A default editorconfig file name to use when resolving project settings.
 11pub const EDITORCONFIG_NAME: &str = ".editorconfig";
 12
 13/// A custom data directory override, set only by `set_custom_data_dir`.
 14/// This is used to override the default data directory location.
 15/// The directory will be created if it doesn't exist when set.
 16static CUSTOM_DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
 17
 18/// The resolved data directory, combining custom override or platform defaults.
 19/// This is set once and cached for subsequent calls.
 20/// On macOS, this is `~/Library/Application Support/Zed`.
 21/// On Linux/FreeBSD, this is `$XDG_DATA_HOME/zed`.
 22/// On Windows, this is `%LOCALAPPDATA%\Zed`.
 23static CURRENT_DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
 24
 25/// The resolved config directory, combining custom override or platform defaults.
 26/// This is set once and cached for subsequent calls.
 27/// On macOS, this is `~/.config/zed`.
 28/// On Linux/FreeBSD, this is `$XDG_CONFIG_HOME/zed`.
 29/// On Windows, this is `%APPDATA%\Zed`.
 30static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
 31
 32/// Returns the relative path to the zed_server directory on the ssh host.
 33pub fn remote_server_dir_relative() -> &'static RelPath {
 34    static CACHED: LazyLock<&'static RelPath> =
 35        LazyLock::new(|| RelPath::unix(".zed_server").unwrap());
 36    *CACHED
 37}
 38
 39/// Returns the relative path to the zed_wsl_server directory on the wsl host.
 40pub fn remote_wsl_server_dir_relative() -> &'static RelPath {
 41    static CACHED: LazyLock<&'static RelPath> =
 42        LazyLock::new(|| RelPath::unix(".zed_wsl_server").unwrap());
 43    *CACHED
 44}
 45
 46/// Sets a custom directory for all user data, overriding the default data directory.
 47/// This function must be called before any other path operations that depend on the data directory.
 48/// The directory's path will be canonicalized to an absolute path by a blocking FS operation.
 49/// The directory will be created if it doesn't exist.
 50///
 51/// # Arguments
 52///
 53/// * `dir` - The path to use as the custom data directory. This will be used as the base
 54///   directory for all user data, including databases, extensions, and logs.
 55///
 56/// # Returns
 57///
 58/// A reference to the static `PathBuf` containing the custom data directory path.
 59///
 60/// # Panics
 61///
 62/// Panics if:
 63/// * Called after the data directory has been initialized (e.g., via `data_dir` or `config_dir`)
 64/// * The directory's path cannot be canonicalized to an absolute path
 65/// * The directory cannot be created
 66pub fn set_custom_data_dir(dir: &str) -> &'static PathBuf {
 67    if CURRENT_DATA_DIR.get().is_some() || CONFIG_DIR.get().is_some() {
 68        panic!("set_custom_data_dir called after data_dir or config_dir was initialized");
 69    }
 70    CUSTOM_DATA_DIR.get_or_init(|| {
 71        let mut path = PathBuf::from(dir);
 72        if path.is_relative() && path.exists() {
 73            let abs_path = path
 74                .canonicalize()
 75                .expect("failed to canonicalize custom data directory's path to an absolute path");
 76            path = util::paths::SanitizedPath::new(&abs_path).into()
 77        }
 78        std::fs::create_dir_all(&path).expect("failed to create custom data directory");
 79        path
 80    })
 81}
 82
 83/// Returns the path to the configuration directory used by Zed.
 84pub fn config_dir() -> &'static PathBuf {
 85    CONFIG_DIR.get_or_init(|| {
 86        if let Some(custom_dir) = CUSTOM_DATA_DIR.get() {
 87            custom_dir.join("config")
 88        } else if cfg!(target_os = "windows") {
 89            dirs::config_dir()
 90                .expect("failed to determine RoamingAppData directory")
 91                .join("Zed")
 92        } else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
 93            if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
 94                flatpak_xdg_config.into()
 95            } else {
 96                dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
 97            }
 98            .join("zed")
 99        } else {
100            home_dir().join(".config").join("zed")
101        }
102    })
103}
104
105/// Returns the path to the data directory used by Zed.
106pub fn data_dir() -> &'static PathBuf {
107    CURRENT_DATA_DIR.get_or_init(|| {
108        if let Some(custom_dir) = CUSTOM_DATA_DIR.get() {
109            custom_dir.clone()
110        } else if cfg!(target_os = "macos") {
111            home_dir().join("Library/Application Support/Zed")
112        } else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
113            if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
114                flatpak_xdg_data.into()
115            } else {
116                dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
117            }
118            .join("zed")
119        } else if cfg!(target_os = "windows") {
120            dirs::data_local_dir()
121                .expect("failed to determine LocalAppData directory")
122                .join("Zed")
123        } else {
124            config_dir().clone() // Fallback
125        }
126    })
127}
128
129/// Returns the path to the temp directory used by Zed.
130pub fn temp_dir() -> &'static PathBuf {
131    static TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
132    TEMP_DIR.get_or_init(|| {
133        if cfg!(target_os = "macos") {
134            return dirs::cache_dir()
135                .expect("failed to determine cachesDirectory directory")
136                .join("Zed");
137        }
138
139        if cfg!(target_os = "windows") {
140            return dirs::cache_dir()
141                .expect("failed to determine LocalAppData directory")
142                .join("Zed");
143        }
144
145        if cfg!(any(target_os = "linux", target_os = "freebsd")) {
146            return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
147                flatpak_xdg_cache.into()
148            } else {
149                dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
150            }
151            .join("zed");
152        }
153
154        home_dir().join(".cache").join("zed")
155    })
156}
157
158/// Returns the path to the logs directory.
159pub fn logs_dir() -> &'static PathBuf {
160    static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
161    LOGS_DIR.get_or_init(|| {
162        if cfg!(target_os = "macos") {
163            home_dir().join("Library/Logs/Zed")
164        } else {
165            data_dir().join("logs")
166        }
167    })
168}
169
170/// Returns the path to the Zed server directory on this SSH host.
171pub fn remote_server_state_dir() -> &'static PathBuf {
172    static REMOTE_SERVER_STATE: OnceLock<PathBuf> = OnceLock::new();
173    REMOTE_SERVER_STATE.get_or_init(|| data_dir().join("server_state"))
174}
175
176/// Returns the path to the `Zed.log` file.
177pub fn log_file() -> &'static PathBuf {
178    static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
179    LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
180}
181
182/// Returns the path to the `Zed.log.old` file.
183pub fn old_log_file() -> &'static PathBuf {
184    static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
185    OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
186}
187
188/// Returns the path to the database directory.
189pub fn database_dir() -> &'static PathBuf {
190    static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
191    DATABASE_DIR.get_or_init(|| data_dir().join("db"))
192}
193
194/// Returns the path to the crashes directory, if it exists for the current platform.
195pub fn crashes_dir() -> &'static Option<PathBuf> {
196    static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
197    CRASHES_DIR.get_or_init(|| {
198        cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
199    })
200}
201
202/// Returns the path to the retired crashes directory, if it exists for the current platform.
203pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
204    static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
205    CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
206}
207
208/// Returns the path to the `settings.json` file.
209pub fn settings_file() -> &'static PathBuf {
210    static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
211    SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
212}
213
214/// Returns the path to the global settings file.
215pub fn global_settings_file() -> &'static PathBuf {
216    static GLOBAL_SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
217    GLOBAL_SETTINGS_FILE.get_or_init(|| config_dir().join("global_settings.json"))
218}
219
220/// Returns the path to the `settings_backup.json` file.
221pub fn settings_backup_file() -> &'static PathBuf {
222    static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
223    SETTINGS_FILE.get_or_init(|| config_dir().join("settings_backup.json"))
224}
225
226/// Returns the path to the `keymap.json` file.
227pub fn keymap_file() -> &'static PathBuf {
228    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
229    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
230}
231
232/// Returns the path to the `keymap_backup.json` file.
233pub fn keymap_backup_file() -> &'static PathBuf {
234    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
235    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap_backup.json"))
236}
237
238/// Returns the path to the `tasks.json` file.
239pub fn tasks_file() -> &'static PathBuf {
240    static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
241    TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
242}
243
244/// Returns the path to the `debug.json` file.
245pub fn debug_scenarios_file() -> &'static PathBuf {
246    static DEBUG_SCENARIOS_FILE: OnceLock<PathBuf> = OnceLock::new();
247    DEBUG_SCENARIOS_FILE.get_or_init(|| config_dir().join("debug.json"))
248}
249
250/// Returns the path to the extensions directory.
251///
252/// This is where installed extensions are stored.
253pub fn extensions_dir() -> &'static PathBuf {
254    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
255    EXTENSIONS_DIR.get_or_init(|| data_dir().join("extensions"))
256}
257
258/// Returns the path to the extensions directory.
259///
260/// This is where installed extensions are stored on a remote.
261pub fn remote_extensions_dir() -> &'static PathBuf {
262    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
263    EXTENSIONS_DIR.get_or_init(|| data_dir().join("remote_extensions"))
264}
265
266/// Returns the path to the extensions directory.
267///
268/// This is where installed extensions are stored on a remote.
269pub fn remote_extensions_uploads_dir() -> &'static PathBuf {
270    static UPLOAD_DIR: OnceLock<PathBuf> = OnceLock::new();
271    UPLOAD_DIR.get_or_init(|| remote_extensions_dir().join("uploads"))
272}
273
274/// Returns the path to the themes directory.
275///
276/// This is where themes that are not provided by extensions are stored.
277pub fn themes_dir() -> &'static PathBuf {
278    static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
279    THEMES_DIR.get_or_init(|| config_dir().join("themes"))
280}
281
282/// Returns the path to the snippets directory.
283pub fn snippets_dir() -> &'static PathBuf {
284    static SNIPPETS_DIR: OnceLock<PathBuf> = OnceLock::new();
285    SNIPPETS_DIR.get_or_init(|| config_dir().join("snippets"))
286}
287
288/// Returns the path to the contexts directory.
289///
290/// This is where the saved contexts from the Assistant are stored.
291pub fn text_threads_dir() -> &'static PathBuf {
292    static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
293    CONTEXTS_DIR.get_or_init(|| {
294        if cfg!(target_os = "macos") {
295            config_dir().join("conversations")
296        } else {
297            data_dir().join("conversations")
298        }
299    })
300}
301
302/// Returns the path to the contexts directory.
303///
304/// This is where the prompts for use with the Assistant are stored.
305pub fn prompts_dir() -> &'static PathBuf {
306    static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
307    PROMPTS_DIR.get_or_init(|| {
308        if cfg!(target_os = "macos") {
309            config_dir().join("prompts")
310        } else {
311            data_dir().join("prompts")
312        }
313    })
314}
315
316/// Returns the path to the prompt templates directory.
317///
318/// This is where the prompt templates for core features can be overridden with templates.
319///
320/// # Arguments
321///
322/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
323pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
324    if let Some(path) = repo_path {
325        let dev_path = path.join("assets").join("prompts");
326        if dev_path.exists() {
327            return dev_path;
328        }
329    }
330
331    static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
332    PROMPT_TEMPLATES_DIR
333        .get_or_init(|| {
334            if cfg!(target_os = "macos") {
335                config_dir().join("prompt_overrides")
336            } else {
337                data_dir().join("prompt_overrides")
338            }
339        })
340        .clone()
341}
342
343/// Returns the path to the semantic search's embeddings directory.
344///
345/// This is where the embeddings used to power semantic search are stored.
346pub fn embeddings_dir() -> &'static PathBuf {
347    static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
348    EMBEDDINGS_DIR.get_or_init(|| {
349        if cfg!(target_os = "macos") {
350            config_dir().join("embeddings")
351        } else {
352            data_dir().join("embeddings")
353        }
354    })
355}
356
357/// Returns the path to the languages directory.
358///
359/// This is where language servers are downloaded to for languages built-in to Zed.
360pub fn languages_dir() -> &'static PathBuf {
361    static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
362    LANGUAGES_DIR.get_or_init(|| data_dir().join("languages"))
363}
364
365/// Returns the path to the debug adapters directory
366///
367/// This is where debug adapters are downloaded to for DAPs that are built-in to Zed.
368pub fn debug_adapters_dir() -> &'static PathBuf {
369    static DEBUG_ADAPTERS_DIR: OnceLock<PathBuf> = OnceLock::new();
370    DEBUG_ADAPTERS_DIR.get_or_init(|| data_dir().join("debug_adapters"))
371}
372
373/// Returns the path to the external agents directory
374///
375/// This is where agent servers are downloaded to
376pub fn external_agents_dir() -> &'static PathBuf {
377    static EXTERNAL_AGENTS_DIR: OnceLock<PathBuf> = OnceLock::new();
378    EXTERNAL_AGENTS_DIR.get_or_init(|| data_dir().join("external_agents"))
379}
380
381/// Returns the path to the Copilot directory.
382pub fn copilot_dir() -> &'static PathBuf {
383    static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
384    COPILOT_DIR.get_or_init(|| data_dir().join("copilot"))
385}
386
387/// Returns the path to the Supermaven directory.
388pub fn supermaven_dir() -> &'static PathBuf {
389    static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
390    SUPERMAVEN_DIR.get_or_init(|| data_dir().join("supermaven"))
391}
392
393/// Returns the path to the default Prettier directory.
394pub fn default_prettier_dir() -> &'static PathBuf {
395    static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
396    DEFAULT_PRETTIER_DIR.get_or_init(|| data_dir().join("prettier"))
397}
398
399/// Returns the path to the remote server binaries directory.
400pub fn remote_servers_dir() -> &'static PathBuf {
401    static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
402    REMOTE_SERVERS_DIR.get_or_init(|| data_dir().join("remote_servers"))
403}
404
405/// Returns the relative path to a `.zed` folder within a project.
406pub fn local_settings_folder_name() -> &'static str {
407    ".zed"
408}
409
410/// Returns the relative path to a `.vscode` folder within a project.
411pub fn local_vscode_folder_name() -> &'static str {
412    ".vscode"
413}
414
415/// Returns the relative path to a `settings.json` file within a project.
416pub fn local_settings_file_relative_path() -> &'static RelPath {
417    static CACHED: LazyLock<&'static RelPath> =
418        LazyLock::new(|| RelPath::unix(".zed/settings.json").unwrap());
419    *CACHED
420}
421
422/// Returns the relative path to a `tasks.json` file within a project.
423pub fn local_tasks_file_relative_path() -> &'static RelPath {
424    static CACHED: LazyLock<&'static RelPath> =
425        LazyLock::new(|| RelPath::unix(".zed/tasks.json").unwrap());
426    *CACHED
427}
428
429/// Returns the relative path to a `.vscode/tasks.json` file within a project.
430pub fn local_vscode_tasks_file_relative_path() -> &'static RelPath {
431    static CACHED: LazyLock<&'static RelPath> =
432        LazyLock::new(|| RelPath::unix(".vscode/tasks.json").unwrap());
433    *CACHED
434}
435
436pub fn debug_task_file_name() -> &'static str {
437    "debug.json"
438}
439
440pub fn task_file_name() -> &'static str {
441    "tasks.json"
442}
443
444/// Returns the relative path to a `debug.json` file within a project.
445/// .zed/debug.json
446pub fn local_debug_file_relative_path() -> &'static RelPath {
447    static CACHED: LazyLock<&'static RelPath> =
448        LazyLock::new(|| RelPath::unix(".zed/debug.json").unwrap());
449    *CACHED
450}
451
452/// Returns the relative path to a `.vscode/launch.json` file within a project.
453pub fn local_vscode_launch_file_relative_path() -> &'static RelPath {
454    static CACHED: LazyLock<&'static RelPath> =
455        LazyLock::new(|| RelPath::unix(".vscode/launch.json").unwrap());
456    *CACHED
457}
458
459pub fn user_ssh_config_file() -> PathBuf {
460    home_dir().join(".ssh/config")
461}
462
463pub fn global_ssh_config_file() -> &'static Path {
464    Path::new("/etc/ssh/ssh_config")
465}
466
467/// Returns candidate paths for the vscode user settings file
468pub fn vscode_settings_file_paths() -> Vec<PathBuf> {
469    let mut paths = vscode_user_data_paths();
470    for path in paths.iter_mut() {
471        path.push("User/settings.json");
472    }
473    paths
474}
475
476/// Returns candidate paths for the cursor user settings file
477pub fn cursor_settings_file_paths() -> Vec<PathBuf> {
478    let mut paths = cursor_user_data_paths();
479    for path in paths.iter_mut() {
480        path.push("User/settings.json");
481    }
482    paths
483}
484
485fn vscode_user_data_paths() -> Vec<PathBuf> {
486    // https://github.com/microsoft/vscode/blob/23e7148cdb6d8a27f0109ff77e5b1e019f8da051/src/vs/platform/environment/node/userDataPath.ts#L45
487    const VSCODE_PRODUCT_NAMES: &[&str] = &[
488        "Code",
489        "Code - OSS",
490        "VSCodium",
491        "Code Dev",
492        "Code - OSS Dev",
493        "code-oss-dev",
494    ];
495    let mut paths = Vec::new();
496    if let Ok(portable_path) = env::var("VSCODE_PORTABLE") {
497        paths.push(Path::new(&portable_path).join("user-data"));
498    }
499    if let Ok(vscode_appdata) = env::var("VSCODE_APPDATA") {
500        for product_name in VSCODE_PRODUCT_NAMES {
501            paths.push(Path::new(&vscode_appdata).join(product_name));
502        }
503    }
504    for product_name in VSCODE_PRODUCT_NAMES {
505        add_vscode_user_data_paths(&mut paths, product_name);
506    }
507    paths
508}
509
510fn cursor_user_data_paths() -> Vec<PathBuf> {
511    let mut paths = Vec::new();
512    add_vscode_user_data_paths(&mut paths, "Cursor");
513    paths
514}
515
516fn add_vscode_user_data_paths(paths: &mut Vec<PathBuf>, product_name: &str) {
517    if cfg!(target_os = "macos") {
518        paths.push(
519            home_dir()
520                .join("Library/Application Support")
521                .join(product_name),
522        );
523    } else if cfg!(target_os = "windows") {
524        if let Some(data_local_dir) = dirs::data_local_dir() {
525            paths.push(data_local_dir.join(product_name));
526        }
527        if let Some(data_dir) = dirs::data_dir() {
528            paths.push(data_dir.join(product_name));
529        }
530    } else {
531        paths.push(
532            dirs::config_dir()
533                .unwrap_or(home_dir().join(".config"))
534                .join(product_name),
535        );
536    }
537}
538
539#[cfg(any(test, feature = "test-support"))]
540pub fn global_gitignore_path() -> Option<PathBuf> {
541    Some(home_dir().join(".config").join("git").join("ignore"))
542}
543
544#[cfg(not(any(test, feature = "test-support")))]
545pub fn global_gitignore_path() -> Option<PathBuf> {
546    static GLOBAL_GITIGNORE_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
547    GLOBAL_GITIGNORE_PATH
548        .get_or_init(::ignore::gitignore::gitconfig_excludes_path)
549        .clone()
550}