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