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