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