diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index d26f60350b5656b1730993ff76e07c31139c41da..4f2ba9423f69bfc374b072142dbc4508191c3dc2 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -5497,25 +5497,51 @@ impl Project { let key = (worktree_id, path); log::debug!("handle_create_file_for_peer: looking up key={:?}", key); - let mut files = downloading_files.lock(); - log::trace!( - "handle_create_file_for_peer: current downloading_files keys: {:?}", - files.keys().collect::>() - ); + let empty_file_destination: Option = { + let mut files = downloading_files.lock(); + log::trace!( + "handle_create_file_for_peer: current downloading_files keys: {:?}", + files.keys().collect::>() + ); + + if let Some(file_entry) = files.get_mut(&key) { + file_entry.total_size = state.content_size; + file_entry.file_id = Some(state.id); + log::debug!( + "handle_create_file_for_peer: updated file entry: total_size={}, file_id={}", + state.content_size, + state.id + ); + } else { + log::warn!( + "handle_create_file_for_peer: key={:?} not found in downloading_files", + key + ); + } + + if state.content_size == 0 { + // No chunks will arrive for an empty file; write it now. + files.remove(&key).map(|entry| entry.destination_path) + } else { + None + } + }; - if let Some(file_entry) = files.get_mut(&key) { - file_entry.total_size = state.content_size; - file_entry.file_id = Some(state.id); + if let Some(destination) = empty_file_destination { log::debug!( - "handle_create_file_for_peer: updated file entry: total_size={}, file_id={}", - state.content_size, - state.id - ); - } else { - log::warn!( - "handle_create_file_for_peer: key={:?} not found in downloading_files", - key + "handle_create_file_for_peer: writing empty file to {:?}", + destination ); + match smol::fs::write(&destination, &[] as &[u8]).await { + Ok(_) => log::info!( + "handle_create_file_for_peer: successfully wrote file to {:?}", + destination + ), + Err(e) => log::error!( + "handle_create_file_for_peer: failed to write empty file: {:?}", + e + ), + } } } else { log::warn!("handle_create_file_for_peer: State has no file field");