Initialize caches for packages and standalone files (#5237)

## Summary

While fixing https://github.com/astral-sh/ruff/pull/5233, I noticed that
in FastAPI, 343 out of 823 files weren't hitting the cache. It turns out
these are standalone files in the documentation that lack a "package
root". Later, when looking up the cache entries, we fallback to the
package directory.

This PR ensures that we initialize the cache for both kinds of files:
those that are in a package, and those that aren't.

The total size of the FastAPI cache for me is now 388K. I also suspect
that this approach is much faster than as initially written, since
before, we were probably initializing one cache per _directory_.

## Test Plan

Ran `cargo run -p ruff_cli -- check ../fastapi --verbose`; verified
that, on second execution, there were no "Checking" entries in the logs.
This commit is contained in:
Charlie Marsh 2023-06-21 13:29:09 -04:00 committed by GitHub
parent c792c10eaa
commit bf1a94ee54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View file

@ -81,17 +81,18 @@ pub(crate) fn run(
// Load the caches.
let caches = bool::from(cache).then(|| {
package_roots
.values()
.flatten()
.dedup()
.map(|package_root| {
let settings = resolver.resolve_all(package_root, pyproject_config);
.iter()
.map(|(package, package_root)| package_root.unwrap_or(package))
.unique()
.par_bridge()
.map(|cache_root| {
let settings = resolver.resolve_all(cache_root, pyproject_config);
let cache = Cache::open(
&settings.cli.cache_dir,
package_root.to_path_buf(),
cache_root.to_path_buf(),
&settings.lib,
);
(&**package_root, cache)
(cache_root, cache)
})
.collect::<HashMap<&Path, Cache>>()
});
@ -109,8 +110,16 @@ pub(crate) fn run(
.and_then(|package| *package);
let settings = resolver.resolve_all(path, pyproject_config);
let package_root = package.unwrap_or_else(|| path.parent().unwrap_or(path));
let cache = caches.as_ref().and_then(|caches| caches.get(&package_root));
let cache_root = package.unwrap_or_else(|| path.parent().unwrap_or(path));
let cache = caches.as_ref().and_then(|caches| {
if let Some(cache) = caches.get(&cache_root) {
Some(cache)
} else {
debug!("No cache found for {}", cache_root.display());
None
}
});
lint_path(path, package, settings, cache, noqa, autofix).map_err(|e| {
(Some(path.to_owned()), {