Skip to content

Commit a72b281

Browse files
authored
Merge pull request #101 from CoreTrace/fix/plugin-cache-fingerprint
fix: fingerprint every file of a plugin directory for the cache key
2 parents 1e0a383 + d9e13d5 commit a72b281

4 files changed

Lines changed: 34 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Two plugins may declare the same symbol: an identical model is registered once and sinks merge their kinds and positions; a real conflict names both plugins instead of failing the run on the first duplicate (#92).
88
- A plugin's entrypoint may be a package, `<module>/__init__.py`, so a plugin can split its tables across files and import them relatively (#93).
9+
- The cache key covers every file of a plugin directory, not only `.py` and `.toml`, so rules or advisories shipped as data invalidate cached results when they change (#94).
910

1011
## 0.2.0 (2026-09-06)
1112

‎docs/usage.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,9 @@ coretrace-python-analyzer --check src/ --sbom sbom.json --policy security/policy
283283
## Large projects
284284

285285
`--cache DIR` keeps the results of a directory check on disk, one entry per module,
286-
keyed by the module's source, the tool and plugin versions, the security models, the
287-
advisories, the dependency graph and the modules it imports. On the next run an unchanged
286+
keyed by the module's source, the tool version, every file of every plugin directory
287+
(code, manifest and data alike), the security models, the advisories, the dependency
288+
graph and the modules it imports. On the next run an unchanged
288289
module is served from the cache, so editing one file re-analyses that file and its
289290
importers only. Entries are plain data; an unreadable entry is recomputed.
290291

‎src/coretrace_python/cache.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import hashlib
1515
import json
1616
import os
17-
from collections.abc import Iterable, Mapping
17+
from collections.abc import Mapping
1818
from dataclasses import dataclass
1919
from pathlib import Path
2020
from types import MappingProxyType
@@ -64,15 +64,18 @@ def fingerprint(*parts: str) -> str:
6464
return digest.hexdigest()
6565

6666

67-
def directory_fingerprint(directory: Path, suffixes: Iterable[str] = (".py", ".toml")) -> str:
68-
"""A digest of the source files under ``directory``, so edited plugin code misses."""
67+
def directory_fingerprint(directory: Path) -> str:
68+
"""A digest of every file under ``directory``, so an edited plugin misses: its code,
69+
its manifest and the rule or advisory data it ships in any format. Bytecode and
70+
hidden files are not part of what the plugin does."""
6971

70-
wanted = tuple(suffixes)
7172
parts: list[str] = []
7273
for path in sorted(directory.rglob("*")):
73-
if path.is_file() and path.suffix in wanted:
74-
parts.append(str(path.relative_to(directory)))
75-
parts.append(path.read_text(encoding="utf-8", errors="replace"))
74+
relative = path.relative_to(directory)
75+
if not path.is_file() or any(p == "__pycache__" or p.startswith(".") for p in relative.parts):
76+
continue
77+
parts.append(str(relative))
78+
parts.append(path.read_bytes().hex())
7679
return fingerprint(*parts)
7780

7881

‎tests/test_cache.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from __future__ import annotations
1414

1515
import json
16+
import shutil
1617
from pathlib import Path
1718

1819
import pytest
@@ -119,6 +120,25 @@ def test_keys_depend_on_dependency_files(tmp_path: Path) -> None:
119120
assert all(after[name] != before[name] for name in before)
120121

121122

123+
def test_keys_depend_on_every_plugin_file(tmp_path: Path) -> None:
124+
root = standard(tmp_path)
125+
plugins = tmp_path / "plugins"
126+
shutil.copytree(
127+
PLUGINS / "syntax" / "dangerous_eval",
128+
plugins / "dangerous_eval",
129+
ignore=shutil.ignore_patterns("__pycache__"),
130+
)
131+
before = engine.analyze_project(root, [plugins]).keys
132+
(plugins / "dangerous_eval" / "rules.json").write_text('{"v": 1}', encoding="utf-8")
133+
after = engine.analyze_project(root, [plugins]).keys
134+
(plugins / "dangerous_eval" / "__pycache__").mkdir(exist_ok=True)
135+
(plugins / "dangerous_eval" / "__pycache__" / "x.pyc").write_bytes(b"\0")
136+
with_bytecode = engine.analyze_project(root, [plugins]).keys
137+
138+
assert all(after[name] != before[name] for name in before)
139+
assert with_bytecode == after
140+
141+
122142
# --------------------------------------------------------------------------- reuse
123143

124144

0 commit comments

Comments
 (0)