paths.rs

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