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/// Returns the path to the configuration directory used by Zed.
9pub fn config_dir() -> &'static PathBuf {
10 static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
11 CONFIG_DIR.get_or_init(|| {
12 if cfg!(target_os = "windows") {
13 return dirs::config_dir()
14 .expect("failed to determine RoamingAppData directory")
15 .join("Zed");
16 }
17
18 if cfg!(target_os = "linux") {
19 return if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
20 flatpak_xdg_config.into()
21 } else {
22 dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
23 }
24 .join("zed");
25 }
26
27 home_dir().join(".config").join("zed")
28 })
29}
30
31/// Returns the path to the support directory used by Zed.
32pub fn support_dir() -> &'static PathBuf {
33 static SUPPORT_DIR: OnceLock<PathBuf> = OnceLock::new();
34 SUPPORT_DIR.get_or_init(|| {
35 if cfg!(target_os = "macos") {
36 return home_dir().join("Library/Application Support/Zed");
37 }
38
39 if cfg!(target_os = "linux") {
40 return if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
41 flatpak_xdg_data.into()
42 } else {
43 dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
44 }
45 .join("zed");
46 }
47
48 if cfg!(target_os = "windows") {
49 return dirs::data_local_dir()
50 .expect("failed to determine LocalAppData directory")
51 .join("Zed");
52 }
53
54 config_dir().clone()
55 })
56}
57
58/// Returns the path to the temp directory used by Zed.
59pub fn temp_dir() -> &'static PathBuf {
60 static TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
61 TEMP_DIR.get_or_init(|| {
62 if cfg!(target_os = "macos") {
63 return dirs::cache_dir()
64 .expect("failed to determine cachesDirectory directory")
65 .join("Zed");
66 }
67
68 if cfg!(target_os = "windows") {
69 return dirs::cache_dir()
70 .expect("failed to determine LocalAppData directory")
71 .join("Zed");
72 }
73
74 if cfg!(target_os = "linux") {
75 return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
76 flatpak_xdg_cache.into()
77 } else {
78 dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
79 }
80 .join("zed");
81 }
82
83 home_dir().join(".cache").join("zed")
84 })
85}
86
87/// Returns the path to the logs directory.
88pub fn logs_dir() -> &'static PathBuf {
89 static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
90 LOGS_DIR.get_or_init(|| {
91 if cfg!(target_os = "macos") {
92 home_dir().join("Library/Logs/Zed")
93 } else {
94 support_dir().join("logs")
95 }
96 })
97}
98
99/// Returns the path to the `Zed.log` file.
100pub fn log_file() -> &'static PathBuf {
101 static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
102 LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
103}
104
105/// Returns the path to the `Zed.log.old` file.
106pub fn old_log_file() -> &'static PathBuf {
107 static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
108 OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
109}
110
111/// Returns the path to the database directory.
112pub fn database_dir() -> &'static PathBuf {
113 static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
114 DATABASE_DIR.get_or_init(|| support_dir().join("db"))
115}
116
117/// Returns the path to the crashes directory, if it exists for the current platform.
118pub fn crashes_dir() -> &'static Option<PathBuf> {
119 static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
120 CRASHES_DIR.get_or_init(|| {
121 cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
122 })
123}
124
125/// Returns the path to the retired crashes directory, if it exists for the current platform.
126pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
127 static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
128 CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
129}
130
131/// Returns the path to the `settings.json` file.
132pub fn settings_file() -> &'static PathBuf {
133 static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
134 SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
135}
136
137/// Returns the path to the `keymap.json` file.
138pub fn keymap_file() -> &'static PathBuf {
139 static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
140 KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
141}
142
143/// Returns the path to the `tasks.json` file.
144pub fn tasks_file() -> &'static PathBuf {
145 static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
146 TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
147}
148
149/// Returns the path to the extensions directory.
150///
151/// This is where installed extensions are stored.
152pub fn extensions_dir() -> &'static PathBuf {
153 static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
154 EXTENSIONS_DIR.get_or_init(|| support_dir().join("extensions"))
155}
156
157/// Returns the path to the themes directory.
158///
159/// This is where themes that are not provided by extensions are stored.
160pub fn themes_dir() -> &'static PathBuf {
161 static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
162 THEMES_DIR.get_or_init(|| config_dir().join("themes"))
163}
164
165/// Returns the path to the contexts directory.
166///
167/// This is where the saved contexts from the Assistant are stored.
168pub fn contexts_dir() -> &'static PathBuf {
169 static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
170 CONTEXTS_DIR.get_or_init(|| {
171 if cfg!(target_os = "macos") {
172 config_dir().join("conversations")
173 } else {
174 support_dir().join("conversations")
175 }
176 })
177}
178
179/// Returns the path to the contexts directory.
180///
181/// This is where the prompts for use with the Assistant are stored.
182pub fn prompts_dir() -> &'static PathBuf {
183 static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
184 PROMPTS_DIR.get_or_init(|| {
185 if cfg!(target_os = "macos") {
186 config_dir().join("prompts")
187 } else {
188 support_dir().join("prompts")
189 }
190 })
191}
192
193/// Returns the path to the prompt templates directory.
194///
195/// This is where the prompt templates for core features can be overridden with templates.
196///
197/// # Arguments
198///
199/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
200pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
201 if let Some(path) = repo_path {
202 let dev_path = path.join("assets").join("prompts");
203 if dev_path.exists() {
204 return dev_path;
205 }
206 }
207
208 static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
209 PROMPT_TEMPLATES_DIR
210 .get_or_init(|| {
211 if cfg!(target_os = "macos") {
212 config_dir().join("prompt_overrides")
213 } else {
214 support_dir().join("prompt_overrides")
215 }
216 })
217 .clone()
218}
219
220/// Returns the path to the semantic search's embeddings directory.
221///
222/// This is where the embeddings used to power semantic search are stored.
223pub fn embeddings_dir() -> &'static PathBuf {
224 static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
225 EMBEDDINGS_DIR.get_or_init(|| {
226 if cfg!(target_os = "macos") {
227 config_dir().join("embeddings")
228 } else {
229 support_dir().join("embeddings")
230 }
231 })
232}
233
234/// Returns the path to the languages directory.
235///
236/// This is where language servers are downloaded to for languages built-in to Zed.
237pub fn languages_dir() -> &'static PathBuf {
238 static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
239 LANGUAGES_DIR.get_or_init(|| support_dir().join("languages"))
240}
241
242/// Returns the path to the Copilot directory.
243pub fn copilot_dir() -> &'static PathBuf {
244 static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
245 COPILOT_DIR.get_or_init(|| support_dir().join("copilot"))
246}
247
248/// Returns the path to the Supermaven directory.
249pub fn supermaven_dir() -> &'static PathBuf {
250 static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
251 SUPERMAVEN_DIR.get_or_init(|| support_dir().join("supermaven"))
252}
253
254/// Returns the path to the default Prettier directory.
255pub fn default_prettier_dir() -> &'static PathBuf {
256 static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
257 DEFAULT_PRETTIER_DIR.get_or_init(|| support_dir().join("prettier"))
258}
259
260/// Returns the path to the remote server binaries directory.
261pub fn remote_servers_dir() -> &'static PathBuf {
262 static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
263 REMOTE_SERVERS_DIR.get_or_init(|| support_dir().join("remote_servers"))
264}
265
266/// Returns the relative path to a `.zed` folder within a project.
267pub fn local_settings_folder_relative_path() -> &'static Path {
268 Path::new(".zed")
269}
270
271/// Returns the relative path to a `settings.json` file within a project.
272pub fn local_settings_file_relative_path() -> &'static Path {
273 Path::new(".zed/settings.json")
274}
275
276/// Returns the relative path to a `tasks.json` file within a project.
277pub fn local_tasks_file_relative_path() -> &'static Path {
278 Path::new(".zed/tasks.json")
279}
280
281/// Returns the relative path to a `.vscode/tasks.json` file within a project.
282pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
283 Path::new(".vscode/tasks.json")
284}