paths: Replace lazy_static! with OnceLock (#13213)

This PR replaces the `lazy_static!` usages in the `paths` crate with
`OnceLock` from the standard library.

This allows us to drop the `lazy_static` dependency from this crate.

The paths are now exposed as accessor functions that reference a private
static value.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-06-18 12:22:37 -04:00 committed by GitHub
parent ba59e66314
commit 81475ac4cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 322 additions and 172 deletions

View file

@ -343,12 +343,12 @@ fn main() {
let user_settings_file_rx = watch_config_file(
&app.background_executor(),
fs.clone(),
paths::SETTINGS.clone(),
paths::settings_file().clone(),
);
let user_keymap_file_rx = watch_config_file(
&app.background_executor(),
fs.clone(),
paths::KEYMAP.clone(),
paths::keymap_file().clone(),
);
let login_shell_env_loaded = if stdout_is_a_pty() {
@ -399,7 +399,7 @@ fn main() {
cx.update_http_client(client.http_client().clone());
let mut languages =
LanguageRegistry::new(login_shell_env_loaded, cx.background_executor().clone());
languages.set_language_server_download_dir(paths::LANGUAGES_DIR.clone());
languages.set_language_server_download_dir(paths::languages_dir().clone());
let languages = Arc::new(languages);
let node_runtime = RealNodeRuntime::new(client.http_client());
@ -668,12 +668,12 @@ async fn restore_or_create_workspace(
fn init_paths() -> anyhow::Result<()> {
for path in [
&*paths::CONFIG_DIR,
&*paths::EXTENSIONS_DIR,
&*paths::LANGUAGES_DIR,
&*paths::DB_DIR,
&*paths::LOGS_DIR,
&*paths::TEMP_DIR,
paths::config_dir(),
paths::extensions_dir(),
paths::languages_dir(),
paths::database_dir(),
paths::logs_dir(),
paths::temp_dir(),
]
.iter()
{
@ -693,15 +693,16 @@ fn init_logger() {
const KIB: u64 = 1024;
const MIB: u64 = 1024 * KIB;
const MAX_LOG_BYTES: u64 = MIB;
if std::fs::metadata(&*paths::LOG).map_or(false, |metadata| metadata.len() > MAX_LOG_BYTES)
if std::fs::metadata(paths::log_file())
.map_or(false, |metadata| metadata.len() > MAX_LOG_BYTES)
{
let _ = std::fs::rename(&*paths::LOG, &*paths::OLD_LOG);
let _ = std::fs::rename(paths::log_file(), paths::old_log_file());
}
match OpenOptions::new()
.create(true)
.append(true)
.open(&*paths::LOG)
.open(paths::log_file())
{
Ok(log_file) => {
let config = ConfigBuilder::new()
@ -918,7 +919,7 @@ fn load_user_themes_in_background(fs: Arc<dyn fs::Fs>, cx: &mut AppContext) {
if let Some(theme_registry) =
cx.update(|cx| ThemeRegistry::global(cx).clone()).log_err()
{
let themes_dir = paths::THEMES_DIR.as_ref();
let themes_dir = paths::themes_dir().as_ref();
match fs
.metadata(themes_dir)
.await
@ -949,7 +950,7 @@ fn watch_themes(fs: Arc<dyn fs::Fs>, cx: &mut AppContext) {
use std::time::Duration;
cx.spawn(|cx| async move {
let (mut events, _) = fs
.watch(&paths::THEMES_DIR.clone(), Duration::from_millis(100))
.watch(paths::themes_dir(), Duration::from_millis(100))
.await;
while let Some(paths) = events.next().await {

View file

@ -8,7 +8,7 @@ use http::Method;
use isahc::config::Configurable;
use http::{self, HttpClient, HttpClientWithUrl};
use paths::{CRASHES_DIR, CRASHES_RETIRED_DIR};
use paths::{crashes_dir, crashes_retired_dir};
use release_channel::ReleaseChannel;
use release_channel::RELEASE_CHANNEL;
use settings::Settings;
@ -113,7 +113,7 @@ pub fn init_panic_hook(
if !is_pty {
if let Some(panic_data_json) = serde_json::to_string(&panic_data).log_err() {
let timestamp = chrono::Utc::now().format("%Y_%m_%d %H_%M_%S").to_string();
let panic_file_path = paths::LOGS_DIR.join(format!("zed-{}.panic", timestamp));
let panic_file_path = paths::logs_dir().join(format!("zed-{}.panic", timestamp));
let panic_file = std::fs::OpenOptions::new()
.append(true)
.create(true)
@ -368,7 +368,7 @@ async fn upload_previous_panics(
telemetry_settings: client::TelemetrySettings,
) -> Result<Option<(i64, String)>> {
let panic_report_url = http.build_zed_api_url("/telemetry/panics", &[])?;
let mut children = smol::fs::read_dir(&*paths::LOGS_DIR).await?;
let mut children = smol::fs::read_dir(paths::logs_dir()).await?;
let mut most_recent_panic = None;
@ -460,8 +460,8 @@ async fn upload_previous_crashes(
let crash_report_url = http.build_zed_api_url("/telemetry/crashes", &[])?;
// crash directories are only set on MacOS
for dir in [&*CRASHES_DIR, &*CRASHES_RETIRED_DIR]
// Crash directories are only set on macOS.
for dir in [crashes_dir(), crashes_retired_dir()]
.iter()
.filter_map(|d| d.as_deref())
{

View file

@ -36,7 +36,7 @@ use task::static_source::{StaticSource, TrackedFile};
use theme::ActiveTheme;
use workspace::notifications::NotificationId;
use paths::{LOCAL_SETTINGS_RELATIVE_PATH, LOCAL_TASKS_RELATIVE_PATH};
use paths::{local_settings_file_relative_path, local_tasks_file_relative_path};
use terminal_view::terminal_panel::{self, TerminalPanel};
use util::{asset_str, ResultExt};
use uuid::Uuid;
@ -178,11 +178,11 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
let fs = app_state.fs.clone();
project.task_inventory().update(cx, |inventory, cx| {
let tasks_file_rx =
watch_config_file(&cx.background_executor(), fs, paths::TASKS.clone());
watch_config_file(&cx.background_executor(), fs, paths::tasks_file().clone());
inventory.add_source(
TaskSourceKind::AbsPath {
id_base: "global_tasks".into(),
abs_path: paths::TASKS.clone(),
abs_path: paths::tasks_file().clone(),
},
|tx, cx| StaticSource::new(TrackedFile::new(tasks_file_rx, tx, cx)),
cx,
@ -341,13 +341,13 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
)
.register_action(
move |_: &mut Workspace, _: &OpenKeymap, cx: &mut ViewContext<Workspace>| {
open_settings_file(&paths::KEYMAP, Rope::default, cx);
open_settings_file(paths::keymap_file(), Rope::default, cx);
},
)
.register_action(
move |_: &mut Workspace, _: &OpenSettings, cx: &mut ViewContext<Workspace>| {
open_settings_file(
&paths::SETTINGS,
paths::settings_file(),
|| settings::initial_user_settings_content().as_ref().into(),
cx,
);
@ -356,7 +356,7 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
.register_action(
move |_: &mut Workspace, _: &OpenTasks, cx: &mut ViewContext<Workspace>| {
open_settings_file(
&paths::TASKS,
paths::tasks_file(),
|| settings::initial_tasks_content().as_ref().into(),
cx,
);
@ -566,7 +566,7 @@ fn open_log_file(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
let fs = workspace.app_state().fs.clone();
cx.spawn(|workspace, mut cx| async move {
let (old_log, new_log) =
futures::join!(fs.load(&paths::OLD_LOG), fs.load(&paths::LOG));
futures::join!(fs.load(paths::old_log_file()), fs.load(paths::log_file()));
let log = match (old_log, new_log) {
(Err(_), Err(_)) => None,
(old_log, new_log) => {
@ -602,7 +602,7 @@ fn open_log_file(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
cx.new_view(|_| {
MessageNotification::new(format!(
"Unable to access/open log file at path {:?}",
paths::LOG.as_path()
paths::log_file().as_path()
))
})
},
@ -715,7 +715,7 @@ fn open_local_settings_file(
) {
open_local_file(
workspace,
&LOCAL_SETTINGS_RELATIVE_PATH,
local_settings_file_relative_path(),
initial_local_settings_content(),
cx,
)
@ -728,7 +728,7 @@ fn open_local_tasks_file(
) {
open_local_file(
workspace,
&LOCAL_TASKS_RELATIVE_PATH,
local_tasks_file_relative_path(),
initial_tasks_content(),
cx,
)
@ -904,7 +904,7 @@ fn open_settings_file(
let worktree_creation_task = workspace.project().update(cx, |project, cx| {
// Set up a dedicated worktree for settings, since otherwise we're dropping and re-starting LSP servers for each file inside on every settings file close/open
// TODO: Do note that all other external files (e.g. drag and drop from OS) still have their worktrees released on file close, causing LSP servers' restarts.
project.find_or_create_local_worktree(paths::CONFIG_DIR.as_path(), false, cx)
project.find_or_create_local_worktree(paths::config_dir().as_path(), false, cx)
});
let settings_open_task = create_and_open_local_file(&abs_path, cx, default_content);
(worktree_creation_task, settings_open_task)

View file

@ -114,7 +114,7 @@ pub fn listen_for_cli_connections(opener: OpenListener) -> Result<()> {
use release_channel::RELEASE_CHANNEL_NAME;
use std::os::unix::net::UnixDatagram;
let sock_path = paths::SUPPORT_DIR.join(format!("zed-{}.sock", *RELEASE_CHANNEL_NAME));
let sock_path = paths::support_dir().join(format!("zed-{}.sock", *RELEASE_CHANNEL_NAME));
// remove the socket if the process listening on it has died
if let Err(e) = UnixDatagram::unbound()?.connect(&sock_path) {
if e.kind() == std::io::ErrorKind::ConnectionRefused {