Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): hash files properly by reading the whole file #20652

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions packages/nx/src/native/hasher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::io::BufRead;
use std::path::Path;

use tracing::trace;
use xxhash_rust::xxh3;

Expand All @@ -23,23 +23,17 @@ pub fn hash_file(file: String) -> Option<String> {
#[inline]
pub fn hash_file_path<P: AsRef<Path>>(path: P) -> Option<String> {
let path = path.as_ref();
let Ok(file) = File::open(path) else {
trace!("could not open file: {path:?}");
return None;
};

let mut buffer = BufReader::new(file);
let Ok(content) = buffer.fill_buf() else {
trace!("could not read file: {path:?}");
let Ok(content) = std::fs::read(path) else {
trace!("Failed to read file: {:?}", path);
return None;
};

Some(hash(content))
Some(hash(&content))
}

#[cfg(test)]
mod tests {
use super::*;
use crate::native::hasher::hash_file;
use assert_fs::prelude::*;
use assert_fs::TempDir;

Expand Down