paths.rs

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