From f92824f82b85ba269dd586b229605c4d577cbc53 Mon Sep 17 00:00:00 2001 From: Yangshun Date: Sat, 21 Mar 2026 09:44:13 +0800 Subject: [PATCH] refactor: extract shared `hash_content` into `execute/hash.rs` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both `fingerprint.rs` and `glob_inputs.rs` implemented identical 8 KiB buffered xxHash3_64 hashing for cache invalidation. Extract into a shared `hash.rs` module to ensure consistent hashes across code paths — if either diverges, cache lookups would silently break. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../src/session/execute/fingerprint.rs | 19 ++--------------- .../src/session/execute/glob_inputs.rs | 21 ++----------------- crates/vite_task/src/session/execute/hash.rs | 15 +++++++++++++ crates/vite_task/src/session/execute/mod.rs | 1 + 4 files changed, 20 insertions(+), 36 deletions(-) create mode 100644 crates/vite_task/src/session/execute/hash.rs diff --git a/crates/vite_task/src/session/execute/fingerprint.rs b/crates/vite_task/src/session/execute/fingerprint.rs index 1c4994a1..34a7c3da 100644 --- a/crates/vite_task/src/session/execute/fingerprint.rs +++ b/crates/vite_task/src/session/execute/fingerprint.rs @@ -6,8 +6,7 @@ use std::{ collections::BTreeMap, fs::File, - hash::Hasher as _, - io::{self, BufRead, Read}, + io::{self, BufRead}, sync::Arc, }; @@ -182,20 +181,6 @@ fn determine_folder_change_kind<'a>( } } -/// Hash file content using `xxHash3_64` -fn hash_content(mut stream: impl Read) -> io::Result { - let mut hasher = twox_hash::XxHash3_64::default(); - let mut buf = [0u8; 8192]; - loop { - let n = stream.read(&mut buf)?; - if n == 0 { - break; - } - hasher.write(&buf[..n]); - } - Ok(hasher.finish()) -} - /// Check if a directory entry should be ignored in fingerprinting fn should_ignore_entry(name: &[u8]) -> bool { matches!(name, b"." | b".." | b".DS_Store") || name.eq_ignore_ascii_case(b"dist") @@ -251,7 +236,7 @@ pub fn fingerprint_path( return process_directory(std_path, path_read); } } - Ok(PathFingerprint::FileContentHash(hash_content(reader)?)) + Ok(PathFingerprint::FileContentHash(super::hash::hash_content(reader)?)) } /// Process a directory on Windows using `std::fs::read_dir` diff --git a/crates/vite_task/src/session/execute/glob_inputs.rs b/crates/vite_task/src/session/execute/glob_inputs.rs index c68f3bd0..a9bb1b20 100644 --- a/crates/vite_task/src/session/execute/glob_inputs.rs +++ b/crates/vite_task/src/session/execute/glob_inputs.rs @@ -5,12 +5,7 @@ //! //! All glob patterns are workspace-root-relative (resolved at task graph stage). -use std::{ - collections::BTreeMap, - fs::File, - hash::Hasher as _, - io::{self, Read}, -}; +use std::{collections::BTreeMap, fs::File, io}; #[cfg(test)] use vite_path::AbsolutePathBuf; @@ -114,21 +109,9 @@ pub fn compute_globbed_inputs( Ok(result) } -/// Hash file content using `xxHash3_64`. #[expect(clippy::disallowed_types, reason = "receives std::path::Path from wax glob walker")] fn hash_file_content(path: &std::path::Path) -> io::Result { - let file = File::open(path)?; - let mut reader = io::BufReader::new(file); - let mut hasher = twox_hash::XxHash3_64::default(); - let mut buf = [0u8; 8192]; - loop { - let n = reader.read(&mut buf)?; - if n == 0 { - break; - } - hasher.write(&buf[..n]); - } - Ok(hasher.finish()) + super::hash::hash_content(io::BufReader::new(File::open(path)?)) } #[cfg(test)] diff --git a/crates/vite_task/src/session/execute/hash.rs b/crates/vite_task/src/session/execute/hash.rs new file mode 100644 index 00000000..0bbfb0fa --- /dev/null +++ b/crates/vite_task/src/session/execute/hash.rs @@ -0,0 +1,15 @@ +use std::{hash::Hasher as _, io}; + +/// Hash content using 8 KiB buffered `xxHash3_64`. +pub(super) fn hash_content(mut stream: impl io::Read) -> io::Result { + let mut hasher = twox_hash::XxHash3_64::default(); + let mut buf = [0u8; 8192]; + loop { + let n = stream.read(&mut buf)?; + if n == 0 { + break; + } + hasher.write(&buf[..n]); + } + Ok(hasher.finish()) +} diff --git a/crates/vite_task/src/session/execute/mod.rs b/crates/vite_task/src/session/execute/mod.rs index f520ed35..f3076fa0 100644 --- a/crates/vite_task/src/session/execute/mod.rs +++ b/crates/vite_task/src/session/execute/mod.rs @@ -1,5 +1,6 @@ pub mod fingerprint; pub mod glob_inputs; +mod hash; pub mod spawn; use std::{collections::BTreeMap, io::Write as _, process::Stdio, sync::Arc};