paths.rs

  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 hang traces directory.
159pub fn hang_traces_dir() -> &'static PathBuf {
160    static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
161    LOGS_DIR.get_or_init(|| data_dir().join("hang_traces"))
162}
163
164/// Returns the path to the logs directory.
165pub fn logs_dir() -> &'static PathBuf {
166    static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
167    LOGS_DIR.get_or_init(|| {
168        if cfg!(target_os = "macos") {
169            home_dir().join("Library/Logs/Zed")
170        } else {
171            data_dir().join("logs")
172        }
173    })
174}
175
176/// Returns the path to the Zed server directory on this SSH host.
177pub fn remote_server_state_dir() -> &'static PathBuf {
178    static REMOTE_SERVER_STATE: OnceLock<PathBuf> = OnceLock::new();
179    REMOTE_SERVER_STATE.get_or_init(|| data_dir().join("server_state"))
180}
181
182/// Returns the path to the `Zed.log` file.
183pub fn log_file() -> &'static PathBuf {
184    static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
185    LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
186}
187
188/// Returns the path to the `Zed.log.old` file.
189pub fn old_log_file() -> &'static PathBuf {
190    static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
191    OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
192}
193
194/// Returns the path to the database directory.
195pub fn database_dir() -> &'static PathBuf {
196    static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
197    DATABASE_DIR.get_or_init(|| data_dir().join("db"))
198}
199
200/// Returns the path to the crashes directory, if it exists for the current platform.
201pub fn crashes_dir() -> &'static Option<PathBuf> {
202    static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
203    CRASHES_DIR.get_or_init(|| {
204        cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
205    })
206}
207
208/// Returns the path to the retired crashes directory, if it exists for the current platform.
209pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
210    static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
211    CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
212}
213
214/// Returns the path to the `settings.json` file.
215pub fn settings_file() -> &'static PathBuf {
216    static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
217    SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
218}
219
220/// Returns the path to the global settings file.
221pub fn global_settings_file() -> &'static PathBuf {
222    static GLOBAL_SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
223    GLOBAL_SETTINGS_FILE.get_or_init(|| config_dir().join("global_settings.json"))
224}
225
226/// Returns the path to the `settings_backup.json` file.
227pub fn settings_backup_file() -> &'static PathBuf {
228    static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
229    SETTINGS_FILE.get_or_init(|| config_dir().join("settings_backup.json"))
230}
231
232/// Returns the path to the `keymap.json` file.
233pub fn keymap_file() -> &'static PathBuf {
234    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
235    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
236}
237
238/// Returns the path to the `keymap_backup.json` file.
239pub fn keymap_backup_file() -> &'static PathBuf {
240    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
241    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap_backup.json"))
242}
243
244/// Returns the path to the `tasks.json` file.
245pub fn tasks_file() -> &'static PathBuf {
246    static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
247    TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
248}
249
250/// Returns the path to the `debug.json` file.
251pub fn debug_scenarios_file() -> &'static PathBuf {
252    static DEBUG_SCENARIOS_FILE: OnceLock<PathBuf> = OnceLock::new();
253    DEBUG_SCENARIOS_FILE.get_or_init(|| config_dir().join("debug.json"))
254}
255
256/// Returns the path to the extensions directory.
257///
258/// This is where installed extensions are stored.
259pub fn extensions_dir() -> &'static PathBuf {
260    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
261    EXTENSIONS_DIR.get_or_init(|| data_dir().join("extensions"))
262}
263
264/// Returns the path to the extensions directory.
265///
266/// This is where installed extensions are stored on a remote.
267pub fn remote_extensions_dir() -> &'static PathBuf {
268    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
269    EXTENSIONS_DIR.get_or_init(|| data_dir().join("remote_extensions"))
270}
271
272/// Returns the path to the extensions directory.
273///
274/// This is where installed extensions are stored on a remote.
275pub fn remote_extensions_uploads_dir() -> &'static PathBuf {
276    static UPLOAD_DIR: OnceLock<PathBuf> = OnceLock::new();
277    UPLOAD_DIR.get_or_init(|| remote_extensions_dir().join("uploads"))
278}
279
280/// Returns the path to the themes directory.
281///
282/// This is where themes that are not provided by extensions are stored.
283pub fn themes_dir() -> &'static PathBuf {
284    static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
285    THEMES_DIR.get_or_init(|| config_dir().join("themes"))
286}
287
288/// Returns the path to the snippets directory.
289pub fn snippets_dir() -> &'static PathBuf {
290    static SNIPPETS_DIR: OnceLock<PathBuf> = OnceLock::new();
291    SNIPPETS_DIR.get_or_init(|| config_dir().join("snippets"))
292}
293
294/// Returns the path to the contexts directory.
295///
296/// This is where the saved contexts from the Assistant are stored.
297pub fn text_threads_dir() -> &'static PathBuf {
298    static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
299    CONTEXTS_DIR.get_or_init(|| {
300        if cfg!(target_os = "macos") {
301            config_dir().join("conversations")
302        } else {
303            data_dir().join("conversations")
304        }
305    })
306}
307
308/// Returns the path to the contexts directory.
309///
310/// This is where the prompts for use with the Assistant are stored.
311pub fn prompts_dir() -> &'static PathBuf {
312    static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
313    PROMPTS_DIR.get_or_init(|| {
314        if cfg!(target_os = "macos") {
315            config_dir().join("prompts")
316        } else {
317            data_dir().join("prompts")
318        }
319    })
320}
321
322/// Returns the path to the prompt templates directory.
323///
324/// This is where the prompt templates for core features can be overridden with templates.
325///
326/// # Arguments
327///
328/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
329pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
330    if let Some(path) = repo_path {
331        let dev_path = path.join("assets").join("prompts");
332        if dev_path.exists() {
333            return dev_path;
334        }
335    }
336
337    static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
338    PROMPT_TEMPLATES_DIR
339        .get_or_init(|| {
340            if cfg!(target_os = "macos") {
341                config_dir().join("prompt_overrides")
342            } else {
343                data_dir().join("prompt_overrides")
344            }
345        })
346        .clone()
347}
348
349/// Returns the path to the semantic search's embeddings directory.
350///
351/// This is where the embeddings used to power semantic search are stored.
352pub fn embeddings_dir() -> &'static PathBuf {
353    static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
354    EMBEDDINGS_DIR.get_or_init(|| {
355        if cfg!(target_os = "macos") {
356            config_dir().join("embeddings")
357        } else {
358            data_dir().join("embeddings")
359        }
360    })
361}
362
363/// Returns the path to the languages directory.
364///
365/// This is where language servers are downloaded to for languages built-in to Zed.
366pub fn languages_dir() -> &'static PathBuf {
367    static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
368    LANGUAGES_DIR.get_or_init(|| data_dir().join("languages"))
369}
370
371/// Returns the path to the debug adapters directory
372///
373/// This is where debug adapters are downloaded to for DAPs that are built-in to Zed.
374pub fn debug_adapters_dir() -> &'static PathBuf {
375    static DEBUG_ADAPTERS_DIR: OnceLock<PathBuf> = OnceLock::new();
376    DEBUG_ADAPTERS_DIR.get_or_init(|| data_dir().join("debug_adapters"))
377}
378
379/// Returns the path to the external agents directory
380///
381/// This is where agent servers are downloaded to
382pub fn external_agents_dir() -> &'static PathBuf {
383    static EXTERNAL_AGENTS_DIR: OnceLock<PathBuf> = OnceLock::new();
384    EXTERNAL_AGENTS_DIR.get_or_init(|| data_dir().join("external_agents"))
385}
386
387/// Returns the path to the Copilot directory.
388pub fn copilot_dir() -> &'static PathBuf {
389    static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
390    COPILOT_DIR.get_or_init(|| data_dir().join("copilot"))
391}
392
393/// Returns the path to the Supermaven directory.
394pub fn supermaven_dir() -> &'static PathBuf {
395    static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
396    SUPERMAVEN_DIR.get_or_init(|| data_dir().join("supermaven"))
397}
398
399/// Returns the path to the default Prettier directory.
400pub fn default_prettier_dir() -> &'static PathBuf {
401    static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
402    DEFAULT_PRETTIER_DIR.get_or_init(|| data_dir().join("prettier"))
403}
404
405/// Returns the path to the remote server binaries directory.
406pub fn remote_servers_dir() -> &'static PathBuf {
407    static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
408    REMOTE_SERVERS_DIR.get_or_init(|| data_dir().join("remote_servers"))
409}
410
411/// Returns the relative path to a `.zed` folder within a project.
412pub fn local_settings_folder_name() -> &'static str {
413    ".zed"
414}
415
416/// Returns the relative path to a `.vscode` folder within a project.
417pub fn local_vscode_folder_name() -> &'static str {
418    ".vscode"
419}
420
421/// Returns the relative path to a `settings.json` file within a project.
422pub fn local_settings_file_relative_path() -> &'static RelPath {
423    static CACHED: LazyLock<&'static RelPath> =
424        LazyLock::new(|| RelPath::unix(".zed/settings.json").unwrap());
425    *CACHED
426}
427
428/// Returns the relative path to a `tasks.json` file within a project.
429pub fn local_tasks_file_relative_path() -> &'static RelPath {
430    static CACHED: LazyLock<&'static RelPath> =
431        LazyLock::new(|| RelPath::unix(".zed/tasks.json").unwrap());
432    *CACHED
433}
434
435/// Returns the relative path to a `.vscode/tasks.json` file within a project.
436pub fn local_vscode_tasks_file_relative_path() -> &'static RelPath {
437    static CACHED: LazyLock<&'static RelPath> =
438        LazyLock::new(|| RelPath::unix(".vscode/tasks.json").unwrap());
439    *CACHED
440}
441
442pub fn debug_task_file_name() -> &'static str {
443    "debug.json"
444}
445
446pub fn task_file_name() -> &'static str {
447    "tasks.json"
448}
449
450/// Returns the relative path to a `debug.json` file within a project.
451/// .zed/debug.json
452pub fn local_debug_file_relative_path() -> &'static RelPath {
453    static CACHED: LazyLock<&'static RelPath> =
454        LazyLock::new(|| RelPath::unix(".zed/debug.json").unwrap());
455    *CACHED
456}
457
458/// Returns the relative path to a `.vscode/launch.json` file within a project.
459pub fn local_vscode_launch_file_relative_path() -> &'static RelPath {
460    static CACHED: LazyLock<&'static RelPath> =
461        LazyLock::new(|| RelPath::unix(".vscode/launch.json").unwrap());
462    *CACHED
463}
464
465pub fn user_ssh_config_file() -> PathBuf {
466    home_dir().join(".ssh/config")
467}
468
469pub fn global_ssh_config_file() -> Option<&'static Path> {
470    if cfg!(windows) {
471        None
472    } else {
473        Some(Path::new("/etc/ssh/ssh_config"))
474    }
475}
476
477/// Returns candidate paths for the vscode user settings file
478pub fn vscode_settings_file_paths() -> Vec<PathBuf> {
479    let mut paths = vscode_user_data_paths();
480    for path in paths.iter_mut() {
481        path.push("User/settings.json");
482    }
483    paths
484}
485
486/// Returns candidate paths for the cursor user settings file
487pub fn cursor_settings_file_paths() -> Vec<PathBuf> {
488    let mut paths = cursor_user_data_paths();
489    for path in paths.iter_mut() {
490        path.push("User/settings.json");
491    }
492    paths
493}
494
495fn vscode_user_data_paths() -> Vec<PathBuf> {
496    // https://github.com/microsoft/vscode/blob/23e7148cdb6d8a27f0109ff77e5b1e019f8da051/src/vs/platform/environment/node/userDataPath.ts#L45
497    const VSCODE_PRODUCT_NAMES: &[&str] = &[
498        "Code",
499        "Code - OSS",
500        "VSCodium",
501        "Code Dev",
502        "Code - OSS Dev",
503        "code-oss-dev",
504    ];
505    let mut paths = Vec::new();
506    if let Ok(portable_path) = env::var("VSCODE_PORTABLE") {
507        paths.push(Path::new(&portable_path).join("user-data"));
508    }
509    if let Ok(vscode_appdata) = env::var("VSCODE_APPDATA") {
510        for product_name in VSCODE_PRODUCT_NAMES {
511            paths.push(Path::new(&vscode_appdata).join(product_name));
512        }
513    }
514    for product_name in VSCODE_PRODUCT_NAMES {
515        add_vscode_user_data_paths(&mut paths, product_name);
516    }
517    paths
518}
519
520fn cursor_user_data_paths() -> Vec<PathBuf> {
521    let mut paths = Vec::new();
522    add_vscode_user_data_paths(&mut paths, "Cursor");
523    paths
524}
525
526fn add_vscode_user_data_paths(paths: &mut Vec<PathBuf>, product_name: &str) {
527    if cfg!(target_os = "macos") {
528        paths.push(
529            home_dir()
530                .join("Library/Application Support")
531                .join(product_name),
532        );
533    } else if cfg!(target_os = "windows") {
534        if let Some(data_local_dir) = dirs::data_local_dir() {
535            paths.push(data_local_dir.join(product_name));
536        }
537        if let Some(data_dir) = dirs::data_dir() {
538            paths.push(data_dir.join(product_name));
539        }
540    } else {
541        paths.push(
542            dirs::config_dir()
543                .unwrap_or(home_dir().join(".config"))
544                .join(product_name),
545        );
546    }
547}
548
549#[cfg(any(test, feature = "test-support"))]
550pub fn global_gitignore_path() -> Option<PathBuf> {
551    Some(home_dir().join(".config").join("git").join("ignore"))
552}
553
554#[cfg(not(any(test, feature = "test-support")))]
555pub fn global_gitignore_path() -> Option<PathBuf> {
556    static GLOBAL_GITIGNORE_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
557    GLOBAL_GITIGNORE_PATH
558        .get_or_init(::ignore::gitignore::gitconfig_excludes_path)
559        .clone()
560}