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()
 78                    .expect("failed to determine XDG_CONFIG_HOME directory")
 79                    .join("zed")
 80            }
 81        } else {
 82            home_dir().join(".config").join("zed")
 83        }
 84    })
 85}
 86
 87/// Returns the path to the data directory used by Zed.
 88pub fn data_dir() -> &'static PathBuf {
 89    CURRENT_DATA_DIR.get_or_init(|| {
 90        if let Some(custom_dir) = CUSTOM_DATA_DIR.get() {
 91            custom_dir.clone()
 92        } else if cfg!(target_os = "macos") {
 93            home_dir().join("Library/Application Support/Zed")
 94        } else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
 95            if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
 96                flatpak_xdg_data.into()
 97            } else {
 98                dirs::data_local_dir()
 99                    .expect("failed to determine XDG_DATA_HOME directory")
100                    .join("zed")
101            }
102        } else if cfg!(target_os = "windows") {
103            dirs::data_local_dir()
104                .expect("failed to determine LocalAppData directory")
105                .join("Zed")
106        } else {
107            config_dir().clone() // Fallback
108        }
109    })
110}
111/// Returns the path to the temp directory used by Zed.
112pub fn temp_dir() -> &'static PathBuf {
113    static TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
114    TEMP_DIR.get_or_init(|| {
115        if cfg!(target_os = "macos") {
116            return dirs::cache_dir()
117                .expect("failed to determine cachesDirectory directory")
118                .join("Zed");
119        }
120
121        if cfg!(target_os = "windows") {
122            return dirs::cache_dir()
123                .expect("failed to determine LocalAppData directory")
124                .join("Zed");
125        }
126
127        if cfg!(any(target_os = "linux", target_os = "freebsd")) {
128            return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
129                flatpak_xdg_cache.into()
130            } else {
131                dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
132            }
133            .join("zed");
134        }
135
136        home_dir().join(".cache").join("zed")
137    })
138}
139
140/// Returns the path to the logs directory.
141pub fn logs_dir() -> &'static PathBuf {
142    static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
143    LOGS_DIR.get_or_init(|| {
144        if cfg!(target_os = "macos") {
145            home_dir().join("Library/Logs/Zed")
146        } else {
147            data_dir().join("logs")
148        }
149    })
150}
151
152/// Returns the path to the Zed server directory on this SSH host.
153pub fn remote_server_state_dir() -> &'static PathBuf {
154    static REMOTE_SERVER_STATE: OnceLock<PathBuf> = OnceLock::new();
155    REMOTE_SERVER_STATE.get_or_init(|| data_dir().join("server_state"))
156}
157
158/// Returns the path to the `Zed.log` file.
159pub fn log_file() -> &'static PathBuf {
160    static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
161    LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
162}
163
164/// Returns the path to the `Zed.log.old` file.
165pub fn old_log_file() -> &'static PathBuf {
166    static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
167    OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
168}
169
170/// Returns the path to the database directory.
171pub fn database_dir() -> &'static PathBuf {
172    static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
173    DATABASE_DIR.get_or_init(|| data_dir().join("db"))
174}
175
176/// Returns the path to the crashes directory, if it exists for the current platform.
177pub fn crashes_dir() -> &'static Option<PathBuf> {
178    static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
179    CRASHES_DIR.get_or_init(|| {
180        cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
181    })
182}
183
184/// Returns the path to the retired crashes directory, if it exists for the current platform.
185pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
186    static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
187    CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
188}
189
190/// Returns the path to the `settings.json` file.
191pub fn settings_file() -> &'static PathBuf {
192    static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
193    SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
194}
195
196/// Returns the path to the `settings_backup.json` file.
197pub fn settings_backup_file() -> &'static PathBuf {
198    static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
199    SETTINGS_FILE.get_or_init(|| config_dir().join("settings_backup.json"))
200}
201
202/// Returns the path to the `keymap.json` file.
203pub fn keymap_file() -> &'static PathBuf {
204    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
205    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
206}
207
208/// Returns the path to the `keymap_backup.json` file.
209pub fn keymap_backup_file() -> &'static PathBuf {
210    static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
211    KEYMAP_FILE.get_or_init(|| config_dir().join("keymap_backup.json"))
212}
213
214/// Returns the path to the `tasks.json` file.
215pub fn tasks_file() -> &'static PathBuf {
216    static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
217    TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
218}
219
220/// Returns the path to the `debug.json` file.
221pub fn debug_tasks_file() -> &'static PathBuf {
222    static DEBUG_TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
223    DEBUG_TASKS_FILE.get_or_init(|| config_dir().join("debug.json"))
224}
225
226/// Returns the path to the extensions directory.
227///
228/// This is where installed extensions are stored.
229pub fn extensions_dir() -> &'static PathBuf {
230    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
231    EXTENSIONS_DIR.get_or_init(|| data_dir().join("extensions"))
232}
233
234/// Returns the path to the extensions directory.
235///
236/// This is where installed extensions are stored on a remote.
237pub fn remote_extensions_dir() -> &'static PathBuf {
238    static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
239    EXTENSIONS_DIR.get_or_init(|| data_dir().join("remote_extensions"))
240}
241
242/// Returns the path to the extensions directory.
243///
244/// This is where installed extensions are stored on a remote.
245pub fn remote_extensions_uploads_dir() -> &'static PathBuf {
246    static UPLOAD_DIR: OnceLock<PathBuf> = OnceLock::new();
247    UPLOAD_DIR.get_or_init(|| remote_extensions_dir().join("uploads"))
248}
249
250/// Returns the path to the themes directory.
251///
252/// This is where themes that are not provided by extensions are stored.
253pub fn themes_dir() -> &'static PathBuf {
254    static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
255    THEMES_DIR.get_or_init(|| config_dir().join("themes"))
256}
257
258/// Returns the path to the snippets directory.
259pub fn snippets_dir() -> &'static PathBuf {
260    static SNIPPETS_DIR: OnceLock<PathBuf> = OnceLock::new();
261    SNIPPETS_DIR.get_or_init(|| config_dir().join("snippets"))
262}
263
264/// Returns the path to the contexts directory.
265///
266/// This is where the saved contexts from the Assistant are stored.
267pub fn contexts_dir() -> &'static PathBuf {
268    static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
269    CONTEXTS_DIR.get_or_init(|| {
270        if cfg!(target_os = "macos") {
271            config_dir().join("conversations")
272        } else {
273            data_dir().join("conversations")
274        }
275    })
276}
277
278/// Returns the path to the contexts directory.
279///
280/// This is where the prompts for use with the Assistant are stored.
281pub fn prompts_dir() -> &'static PathBuf {
282    static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
283    PROMPTS_DIR.get_or_init(|| {
284        if cfg!(target_os = "macos") {
285            config_dir().join("prompts")
286        } else {
287            data_dir().join("prompts")
288        }
289    })
290}
291
292/// Returns the path to the prompt templates directory.
293///
294/// This is where the prompt templates for core features can be overridden with templates.
295///
296/// # Arguments
297///
298/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
299pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
300    if let Some(path) = repo_path {
301        let dev_path = path.join("assets").join("prompts");
302        if dev_path.exists() {
303            return dev_path;
304        }
305    }
306
307    static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
308    PROMPT_TEMPLATES_DIR
309        .get_or_init(|| {
310            if cfg!(target_os = "macos") {
311                config_dir().join("prompt_overrides")
312            } else {
313                data_dir().join("prompt_overrides")
314            }
315        })
316        .clone()
317}
318
319/// Returns the path to the semantic search's embeddings directory.
320///
321/// This is where the embeddings used to power semantic search are stored.
322pub fn embeddings_dir() -> &'static PathBuf {
323    static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
324    EMBEDDINGS_DIR.get_or_init(|| {
325        if cfg!(target_os = "macos") {
326            config_dir().join("embeddings")
327        } else {
328            data_dir().join("embeddings")
329        }
330    })
331}
332
333/// Returns the path to the languages directory.
334///
335/// This is where language servers are downloaded to for languages built-in to Zed.
336pub fn languages_dir() -> &'static PathBuf {
337    static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
338    LANGUAGES_DIR.get_or_init(|| data_dir().join("languages"))
339}
340
341/// Returns the path to the debug adapters directory
342///
343/// This is where debug adapters are downloaded to for DAPs that are built-in to Zed.
344pub fn debug_adapters_dir() -> &'static PathBuf {
345    static DEBUG_ADAPTERS_DIR: OnceLock<PathBuf> = OnceLock::new();
346    DEBUG_ADAPTERS_DIR.get_or_init(|| data_dir().join("debug_adapters"))
347}
348
349/// Returns the path to the Copilot directory.
350pub fn copilot_dir() -> &'static PathBuf {
351    static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
352    COPILOT_DIR.get_or_init(|| data_dir().join("copilot"))
353}
354
355/// Returns the path to the Supermaven directory.
356pub fn supermaven_dir() -> &'static PathBuf {
357    static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
358    SUPERMAVEN_DIR.get_or_init(|| data_dir().join("supermaven"))
359}
360
361/// Returns the path to the default Prettier directory.
362pub fn default_prettier_dir() -> &'static PathBuf {
363    static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
364    DEFAULT_PRETTIER_DIR.get_or_init(|| data_dir().join("prettier"))
365}
366
367/// Returns the path to the remote server binaries directory.
368pub fn remote_servers_dir() -> &'static PathBuf {
369    static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
370    REMOTE_SERVERS_DIR.get_or_init(|| data_dir().join("remote_servers"))
371}
372
373/// Returns the relative path to a `.zed` folder within a project.
374pub fn local_settings_folder_relative_path() -> &'static Path {
375    Path::new(".zed")
376}
377
378/// Returns the relative path to a `.vscode` folder within a project.
379pub fn local_vscode_folder_relative_path() -> &'static Path {
380    Path::new(".vscode")
381}
382
383/// Returns the relative path to a `settings.json` file within a project.
384pub fn local_settings_file_relative_path() -> &'static Path {
385    Path::new(".zed/settings.json")
386}
387
388/// Returns the relative path to a `tasks.json` file within a project.
389pub fn local_tasks_file_relative_path() -> &'static Path {
390    Path::new(".zed/tasks.json")
391}
392
393/// Returns the relative path to a `.vscode/tasks.json` file within a project.
394pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
395    Path::new(".vscode/tasks.json")
396}
397
398pub fn debug_task_file_name() -> &'static str {
399    "debug.json"
400}
401
402pub fn task_file_name() -> &'static str {
403    "tasks.json"
404}
405
406/// Returns the relative path to a `launch.json` file within a project.
407pub fn local_debug_file_relative_path() -> &'static Path {
408    Path::new(".zed/debug.json")
409}
410
411/// Returns the relative path to a `.vscode/launch.json` file within a project.
412pub fn local_vscode_launch_file_relative_path() -> &'static Path {
413    Path::new(".vscode/launch.json")
414}