From b965fc23e96fb8a56b5e8e4b601bbd76f28ad74f Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 6 Dec 2023 17:01:51 -0600 Subject: [PATCH 1/2] use a temp directory for the hot reloading pipe on desktop, fullstack and liveview --- packages/cli/src/server/desktop/mod.rs | 5 +++-- packages/hot-reload/src/file_watcher.rs | 7 ++++--- packages/hot-reload/src/lib.rs | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/server/desktop/mod.rs b/packages/cli/src/server/desktop/mod.rs index 98d6e9244..6373487b3 100644 --- a/packages/cli/src/server/desktop/mod.rs +++ b/packages/cli/src/server/desktop/mod.rs @@ -109,7 +109,8 @@ pub async fn serve(config: CrateConfig, hot_reload_state: Option } async fn start_desktop_hot_reload(hot_reload_state: HotReloadState) -> Result<()> { - match LocalSocketListener::bind("@dioxusin") { + let path = std::env::temp_dir().join("dioxusin"); + match LocalSocketListener::bind(path) { Ok(local_socket_stream) => { let aborted = Arc::new(Mutex::new(false)); // States @@ -188,7 +189,7 @@ fn clear_paths() { // We check if the file socket is already open from an old session and then delete it let paths = ["./dioxusin", "./@dioxusin"]; for path in paths { - let path = std::path::PathBuf::from(path); + let path = std::env::temp_dir().join(path); if path.exists() { let _ = std::fs::remove_file(path); } diff --git a/packages/hot-reload/src/file_watcher.rs b/packages/hot-reload/src/file_watcher.rs index 2509dd789..bbd0a7660 100644 --- a/packages/hot-reload/src/file_watcher.rs +++ b/packages/hot-reload/src/file_watcher.rs @@ -157,16 +157,17 @@ pub fn init(cfg: Config) { // On unix, if you force quit the application, it can leave the file socket open // This will cause the local socket listener to fail to open // We check if the file socket is already open from an old session and then delete it - let paths = ["./dioxusin", "./@dioxusin"]; + let paths = ["./dioxusin"]; for path in paths { - let path = PathBuf::from(path); + let path = std::env::temp_dir().join(path); if path.exists() { let _ = std::fs::remove_file(path); } } } - match LocalSocketListener::bind("@dioxusin") { + let path = std::env::temp_dir().join("dioxusin"); + match LocalSocketListener::bind(path) { Ok(local_socket_stream) => { let aborted = Arc::new(Mutex::new(false)); diff --git a/packages/hot-reload/src/lib.rs b/packages/hot-reload/src/lib.rs index 68692b72b..07bd70a8d 100644 --- a/packages/hot-reload/src/lib.rs +++ b/packages/hot-reload/src/lib.rs @@ -24,7 +24,8 @@ pub enum HotReloadMsg { /// Connect to the hot reloading listener. The callback provided will be called every time a template change is detected pub fn connect(mut f: impl FnMut(HotReloadMsg) + Send + 'static) { std::thread::spawn(move || { - if let Ok(socket) = LocalSocketStream::connect("@dioxusin") { + let path = std::env::temp_dir().join("dioxusin"); + if let Ok(socket) = LocalSocketStream::connect(path) { let mut buf_reader = BufReader::new(socket); loop { let mut buf = String::new(); From 9a13df2c737b4232bcf76946eff2cbac5d111896 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 7 Dec 2023 07:00:59 -0600 Subject: [PATCH 2/2] move hot reload socket into the target directory --- packages/cli/src/server/desktop/mod.rs | 21 +++++++++++---------- packages/hot-reload/src/file_watcher.rs | 14 ++++++-------- packages/hot-reload/src/lib.rs | 7 +++++-- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/cli/src/server/desktop/mod.rs b/packages/cli/src/server/desktop/mod.rs index 6373487b3..86f9d971a 100644 --- a/packages/cli/src/server/desktop/mod.rs +++ b/packages/cli/src/server/desktop/mod.rs @@ -43,8 +43,6 @@ pub async fn startup(config: CrateConfig) -> Result<()> { let hot_reload_tx = broadcast::channel(100).0; - clear_paths(); - Some(HotReloadState { messages: hot_reload_tx.clone(), file_map: file_map.clone(), @@ -109,7 +107,13 @@ pub async fn serve(config: CrateConfig, hot_reload_state: Option } async fn start_desktop_hot_reload(hot_reload_state: HotReloadState) -> Result<()> { - let path = std::env::temp_dir().join("dioxusin"); + let metadata = cargo_metadata::MetadataCommand::new() + .no_deps() + .exec() + .unwrap(); + let target_dir = metadata.target_directory.as_std_path(); + let path = target_dir.join("dioxusin"); + clear_paths(&path); match LocalSocketListener::bind(path) { Ok(local_socket_stream) => { let aborted = Arc::new(Mutex::new(false)); @@ -182,17 +186,14 @@ async fn start_desktop_hot_reload(hot_reload_state: HotReloadState) -> Result<() Ok(()) } -fn clear_paths() { +fn clear_paths(file_socket_path: &std::path::Path) { if cfg!(target_os = "macos") { // On unix, if you force quit the application, it can leave the file socket open // This will cause the local socket listener to fail to open // We check if the file socket is already open from an old session and then delete it - let paths = ["./dioxusin", "./@dioxusin"]; - for path in paths { - let path = std::env::temp_dir().join(path); - if path.exists() { - let _ = std::fs::remove_file(path); - } + + if file_socket_path.exists() { + let _ = std::fs::remove_file(file_socket_path); } } } diff --git a/packages/hot-reload/src/file_watcher.rs b/packages/hot-reload/src/file_watcher.rs index bbd0a7660..c83c62ad9 100644 --- a/packages/hot-reload/src/file_watcher.rs +++ b/packages/hot-reload/src/file_watcher.rs @@ -152,22 +152,20 @@ pub fn init(cfg: Config) { } let file_map = Arc::new(Mutex::new(file_map)); + let target_dir = crate_dir.join("target"); + let hot_reload_socket_path = target_dir.join("dioxusin"); + #[cfg(target_os = "macos")] { // On unix, if you force quit the application, it can leave the file socket open // This will cause the local socket listener to fail to open // We check if the file socket is already open from an old session and then delete it - let paths = ["./dioxusin"]; - for path in paths { - let path = std::env::temp_dir().join(path); - if path.exists() { - let _ = std::fs::remove_file(path); - } + if hot_reload_socket_path.exists() { + let _ = std::fs::remove_file(hot_reload_socket_path); } } - let path = std::env::temp_dir().join("dioxusin"); - match LocalSocketListener::bind(path) { + match LocalSocketListener::bind(hot_reload_socket_path) { Ok(local_socket_stream) => { let aborted = Arc::new(Mutex::new(false)); diff --git a/packages/hot-reload/src/lib.rs b/packages/hot-reload/src/lib.rs index 07bd70a8d..46efb8341 100644 --- a/packages/hot-reload/src/lib.rs +++ b/packages/hot-reload/src/lib.rs @@ -1,4 +1,7 @@ -use std::io::{BufRead, BufReader}; +use std::{ + io::{BufRead, BufReader}, + path::PathBuf, +}; use dioxus_core::Template; #[cfg(feature = "file_watcher")] @@ -24,7 +27,7 @@ pub enum HotReloadMsg { /// Connect to the hot reloading listener. The callback provided will be called every time a template change is detected pub fn connect(mut f: impl FnMut(HotReloadMsg) + Send + 'static) { std::thread::spawn(move || { - let path = std::env::temp_dir().join("dioxusin"); + let path = PathBuf::from("./").join("target").join("dioxusin"); if let Ok(socket) = LocalSocketStream::connect(path) { let mut buf_reader = BufReader::new(socket); loop {