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 = "windows") {
63 return dirs::cache_dir()
64 .expect("failed to determine LocalAppData directory")
65 .join("Zed");
66 }
67
68 if cfg!(target_os = "linux") {
69 return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
70 flatpak_xdg_cache.into()
71 } else {
72 dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
73 }
74 .join("zed");
75 }
76
77 home_dir().join(".cache").join("zed")
78 })
79}
80
81/// Returns the path to the logs directory.
82pub fn logs_dir() -> &'static PathBuf {
83 static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
84 LOGS_DIR.get_or_init(|| {
85 if cfg!(target_os = "macos") {
86 home_dir().join("Library/Logs/Zed")
87 } else {
88 support_dir().join("logs")
89 }
90 })
91}
92
93/// Returns the path to the `Zed.log` file.
94pub fn log_file() -> &'static PathBuf {
95 static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
96 LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
97}
98
99/// Returns the path to the `Zed.log.old` file.
100pub fn old_log_file() -> &'static PathBuf {
101 static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
102 OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
103}
104
105/// Returns the path to the database directory.
106pub fn database_dir() -> &'static PathBuf {
107 static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
108 DATABASE_DIR.get_or_init(|| support_dir().join("db"))
109}
110
111/// Returns the path to the crashes directory, if it exists for the current platform.
112pub fn crashes_dir() -> &'static Option<PathBuf> {
113 static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
114 CRASHES_DIR.get_or_init(|| {
115 cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
116 })
117}
118
119/// Returns the path to the retired crashes directory, if it exists for the current platform.
120pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
121 static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
122 CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
123}
124
125/// Returns the path to the `settings.json` file.
126pub fn settings_file() -> &'static PathBuf {
127 static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
128 SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
129}
130
131/// Returns the path to the `keymap.json` file.
132pub fn keymap_file() -> &'static PathBuf {
133 static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
134 KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
135}
136
137/// Returns the path to the `tasks.json` file.
138pub fn tasks_file() -> &'static PathBuf {
139 static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
140 TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
141}
142
143/// Returns the path to the extensions directory.
144///
145/// This is where installed extensions are stored.
146pub fn extensions_dir() -> &'static PathBuf {
147 static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
148 EXTENSIONS_DIR.get_or_init(|| support_dir().join("extensions"))
149}
150
151/// Returns the path to the themes directory.
152///
153/// This is where themes that are not provided by extensions are stored.
154pub fn themes_dir() -> &'static PathBuf {
155 static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
156 THEMES_DIR.get_or_init(|| config_dir().join("themes"))
157}
158
159/// Returns the path to the contexts directory.
160///
161/// This is where the saved contexts from the Assistant are stored.
162pub fn contexts_dir() -> &'static PathBuf {
163 static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
164 CONTEXTS_DIR.get_or_init(|| {
165 if cfg!(target_os = "macos") {
166 config_dir().join("conversations")
167 } else {
168 support_dir().join("conversations")
169 }
170 })
171}
172
173/// Returns the path within the contexts directory where images from contexts are stored.
174pub fn context_images_dir() -> &'static PathBuf {
175 static CONTEXT_IMAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
176 CONTEXT_IMAGES_DIR.get_or_init(|| contexts_dir().join("images"))
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}