Merge pull request #1653 from ealmloff/fix-hot-reload-missing-items

Fix missing items in hot reloading diffing
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2023-12-06 21:38:36 -08:00
commit edebe5be4a
6 files changed
+90 -24

No files matched your search

+13 -1
View File
@@ -28,7 +28,19 @@ pub fn set_up_logging() {
message = message,
));
})
.level(log::LevelFilter::Info)
.level(match std::env::var("DIOXUS_LOG") {
Ok(level) => match level.to_lowercase().as_str() {
"error" => log::LevelFilter::Error,
"warn" => log::LevelFilter::Warn,
"info" => log::LevelFilter::Info,
"debug" => log::LevelFilter::Debug,
"trace" => log::LevelFilter::Trace,
_ => {
panic!("Invalid log level: {}", level)
}
},
Err(_) => log::LevelFilter::Info,
})
.chain(std::io::stdout())
.apply()
.unwrap();
+3
View File
@@ -73,6 +73,7 @@ pub async fn serve(config: CrateConfig, hot_reload_state: Option<HotReloadState>
move || {
let mut current_child = currently_running_child.write().unwrap();
log::trace!("Killing old process");
current_child.kill()?;
let (child, result) = start_desktop(&config)?;
*current_child = child;
@@ -212,6 +213,7 @@ fn send_msg(msg: HotReloadMsg, channel: &mut impl std::io::Write) -> bool {
pub fn start_desktop(config: &CrateConfig) -> Result<(Child, BuildResult)> {
// Run the desktop application
log::trace!("Building application");
let result = crate::builder::build_desktop(config, true)?;
match &config.executable {
@@ -222,6 +224,7 @@ pub fn start_desktop(config: &CrateConfig) -> Result<(Child, BuildResult)> {
if cfg!(windows) {
file.set_extension("exe");
}
log::trace!("Running application from {:?}", file);
let child = Command::new(file.to_str().unwrap()).spawn()?;
Ok((child, result))
+14 -11
View File
@@ -22,17 +22,20 @@ pub fn print_console_info(
options: PrettierOptions,
web_info: Option<WebServerInfo>,
) {
if let Ok(native_clearseq) = Command::new(if cfg!(target_os = "windows") {
"cls"
} else {
"clear"
})
.output()
{
print!("{}", String::from_utf8_lossy(&native_clearseq.stdout));
} else {
// Try ANSI-Escape characters
print!("\x1b[2J\x1b[H");
// Don't clear the screen if the user has set the DIOXUS_LOG environment variable to "trace" so that we can see the logs
if Some("trace") != std::env::var("DIOXUS_LOG").ok().as_deref() {
if let Ok(native_clearseq) = Command::new(if cfg!(target_os = "windows") {
"cls"
} else {
"clear"
})
.output()
{
print!("{}", String::from_utf8_lossy(&native_clearseq.stdout));
} else {
// Try ANSI-Escape characters
print!("\x1b[2J\x1b[H");
}
}
let mut profile = if config.release { "Release" } else { "Debug" }.to_string();
+1
View File
@@ -20,6 +20,7 @@ quote = { version = "1.0" }
serde = { version = "1.0", features = ["derive"], optional = true }
internment = { version = "0.7.0", optional = true }
krates = { version = "0.12.6", optional = true }
tracing.workspace = true
[features]
hot_reload = ["krates", "internment"]
+39 -5
View File
@@ -1,4 +1,5 @@
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{File, Macro};
pub enum DiffResult {
@@ -10,13 +11,30 @@ pub enum DiffResult {
pub fn find_rsx(new: &File, old: &File) -> DiffResult {
let mut rsx_calls = Vec::new();
if new.items.len() != old.items.len() {
tracing::trace!(
"found not hot reload-able change {:#?} != {:#?}",
new.items
.iter()
.map(|i| i.to_token_stream().to_string())
.collect::<Vec<_>>(),
old.items
.iter()
.map(|i| i.to_token_stream().to_string())
.collect::<Vec<_>>()
);
return DiffResult::CodeChanged;
}
for (new, old) in new.items.iter().zip(old.items.iter()) {
if find_rsx_item(new, old, &mut rsx_calls) {
tracing::trace!(
"found not hot reload-able change {:#?} != {:#?}",
new.to_token_stream().to_string(),
old.to_token_stream().to_string()
);
return DiffResult::CodeChanged;
}
}
tracing::trace!("found hot reload-able changes {:#?}", rsx_calls);
DiffResult::RsxChanged(rsx_calls)
}
@@ -94,6 +112,9 @@ fn find_rsx_item(
(syn::ImplItem::Macro(new_item), syn::ImplItem::Macro(old_item)) => {
old_item != new_item
}
(syn::ImplItem::Verbatim(stream), syn::ImplItem::Verbatim(stream2)) => {
stream.to_string() != stream2.to_string()
}
_ => true,
} {
return true;
@@ -186,10 +207,12 @@ fn find_rsx_trait(
}
}
(syn::TraitItem::Fn(new_item), syn::TraitItem::Fn(old_item)) => {
if let (Some(new_block), Some(old_block)) = (&new_item.default, &old_item.default) {
find_rsx_block(new_block, old_block, rsx_calls)
} else {
true
match (&new_item.default, &old_item.default) {
(Some(new_block), Some(old_block)) => {
find_rsx_block(new_block, old_block, rsx_calls)
}
(None, None) => false,
_ => true,
}
}
(syn::TraitItem::Type(new_item), syn::TraitItem::Type(old_item)) => {
@@ -198,6 +221,9 @@ fn find_rsx_trait(
(syn::TraitItem::Macro(new_item), syn::TraitItem::Macro(old_item)) => {
old_item != new_item
}
(syn::TraitItem::Verbatim(stream), syn::TraitItem::Verbatim(stream2)) => {
stream.to_string() != stream2.to_string()
}
_ => true,
} {
return true;
@@ -355,6 +381,11 @@ fn find_rsx_expr(
|| new_expr.or2_token != old_expr.or2_token
|| new_expr.output != old_expr.output
}
(syn::Expr::Const(new_expr), syn::Expr::Const(old_expr)) => {
find_rsx_block(&new_expr.block, &old_expr.block, rsx_calls)
|| new_expr.attrs != old_expr.attrs
|| new_expr.const_token != old_expr.const_token
}
(syn::Expr::Continue(new_expr), syn::Expr::Continue(old_expr)) => old_expr != new_expr,
(syn::Expr::Field(new_expr), syn::Expr::Field(old_expr)) => {
find_rsx_expr(&new_expr.base, &old_expr.base, rsx_calls)
@@ -402,6 +433,7 @@ fn find_rsx_expr(
|| new_expr.attrs != old_expr.attrs
|| new_expr.bracket_token != old_expr.bracket_token
}
(syn::Expr::Infer(new_expr), syn::Expr::Infer(old_expr)) => new_expr != old_expr,
(syn::Expr::Let(new_expr), syn::Expr::Let(old_expr)) => {
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|| new_expr.attrs != old_expr.attrs
@@ -589,7 +621,9 @@ fn find_rsx_expr(
_ => true,
}
}
(syn::Expr::Verbatim(_), syn::Expr::Verbatim(_)) => false,
(syn::Expr::Verbatim(stream), syn::Expr::Verbatim(stream2)) => {
stream.to_string() != stream2.to_string()
}
_ => true,
}
}
@@ -51,17 +51,24 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
fn find_rs_files(
root: PathBuf,
filter: &mut impl FnMut(&Path) -> bool,
) -> io::Result<FileMapSearchResult> {
) -> FileMapSearchResult {
let mut files = HashMap::new();
let mut errors = Vec::new();
if root.is_dir() {
for entry in (fs::read_dir(root)?).flatten() {
let read_dir = match fs::read_dir(root) {
Ok(read_dir) => read_dir,
Err(err) => {
errors.push(err);
return FileMapSearchResult { map: files, errors };
}
};
for entry in read_dir.flatten() {
let path = entry.path();
if !filter(&path) {
let FileMapSearchResult {
map,
errors: child_errors,
} = find_rs_files(path, filter)?;
} = find_rs_files(path, filter);
errors.extend(child_errors);
files.extend(map);
}
@@ -69,14 +76,20 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
} else if root.extension().and_then(|s| s.to_str()) == Some("rs") {
if let Ok(mut file) = File::open(root.clone()) {
let mut src = String::new();
file.read_to_string(&mut src)?;
files.insert(root, (src, None));
match file.read_to_string(&mut src) {
Ok(_) => {
files.insert(root, (src, None));
}
Err(err) => {
errors.push(err);
}
}
}
}
Ok(FileMapSearchResult { map: files, errors })
FileMapSearchResult { map: files, errors }
}
let FileMapSearchResult { map, errors } = find_rs_files(path, &mut filter)?;
let FileMapSearchResult { map, errors } = find_rs_files(path, &mut filter);
let result = Self {
map,
in_workspace: HashMap::new(),