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