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

Include file permissions in cache key #3104

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 14 additions & 26 deletions crates/ruff_cli/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@ use path_absolutize::Absolutize;
use ruff::message::Message;
use ruff::settings::{flags, AllSettings, Settings};
use serde::{Deserialize, Serialize};
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Serialize, Deserialize)]
struct CacheMetadata {
mtime: i64,
}

#[derive(Serialize)]
struct CheckResultRef<'a> {
metadata: &'a CacheMetadata,
messages: &'a [Message],
}

#[derive(Deserialize)]
struct CheckResult {
metadata: CacheMetadata,
messages: Vec<Message>,
}

Expand All @@ -38,6 +33,7 @@ fn content_dir() -> &'static Path {
fn cache_key<P: AsRef<Path>>(
path: P,
package: Option<&P>,
metadata: &fs::Metadata,
settings: &Settings,
autofix: flags::Autofix,
) -> u64 {
Expand All @@ -48,6 +44,9 @@ fn cache_key<P: AsRef<Path>>(
.as_ref()
.map(|path| path.as_ref().absolutize().unwrap())
.hash(&mut hasher);
FileTime::from_last_modification_time(metadata).hash(&mut hasher);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now include the timestamp in the cache key. This should be more performant, since we don't have to deserialize from the cache to know that it's a miss.

#[cfg(unix)]
metadata.permissions().mode().hash(&mut hasher);
settings.hash(&mut hasher);
autofix.hash(&mut hasher);
hasher.finish()
Expand Down Expand Up @@ -99,23 +98,16 @@ pub fn get<P: AsRef<Path>>(
) -> Option<Vec<Message>> {
let encoded = read_sync(
&settings.cli.cache_dir,
cache_key(path, package, &settings.lib, autofix),
cache_key(path, package, metadata, &settings.lib, autofix),
)
.ok()?;
let (mtime, messages) = match bincode::deserialize::<CheckResult>(&encoded[..]) {
Ok(CheckResult {
metadata: CacheMetadata { mtime },
messages,
}) => (mtime, messages),
match bincode::deserialize::<CheckResult>(&encoded[..]) {
Ok(CheckResult { messages }) => Some(messages),
Err(e) => {
error!("Failed to deserialize encoded cache entry: {e:?}");
return None;
None
}
};
if FileTime::from_last_modification_time(metadata).unix_seconds() != mtime {
return None;
}
Some(messages)
}

/// Set a value in the cache.
Expand All @@ -127,15 +119,10 @@ pub fn set<P: AsRef<Path>>(
autofix: flags::Autofix,
messages: &[Message],
) {
let check_result = CheckResultRef {
metadata: &CacheMetadata {
mtime: FileTime::from_last_modification_time(metadata).unix_seconds(),
},
messages,
};
let check_result = CheckResultRef { messages };
if let Err(e) = write_sync(
&settings.cli.cache_dir,
cache_key(path, package, &settings.lib, autofix),
cache_key(path, package, metadata, &settings.lib, autofix),
&bincode::serialize(&check_result).unwrap(),
) {
error!("Failed to write to cache: {e:?}");
Expand All @@ -146,11 +133,12 @@ pub fn set<P: AsRef<Path>>(
pub fn del<P: AsRef<Path>>(
path: P,
package: Option<&P>,
metadata: &fs::Metadata,
settings: &AllSettings,
autofix: flags::Autofix,
) {
drop(del_sync(
&settings.cli.cache_dir,
cache_key(path, package, &settings.lib, autofix),
cache_key(path, package, metadata, &settings.lib, autofix),
));
}
4 changes: 3 additions & 1 deletion crates/ruff_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ pub fn lint_path(
);

// Purge the cache.
cache::del(path, package.as_ref(), settings, autofix.into());
if let Some(metadata) = metadata {
cache::del(path, package.as_ref(), &metadata, settings, autofix.into());
}
} else {
// Re-populate the cache.
if let Some(metadata) = metadata {
Expand Down