fix(exclude): persist --exclude so rebuilds re-apply it (#1886)

--exclude was honored only by the initial extract scan; update/watch/hook
rebuilds re-ran detect() without it and silently re-indexed excluded
paths. extract now writes the patterns to a graphify-out/.graphify_build
.json sidecar, and _rebuild_code reads and re-applies them on every
rebuild (layered after the ignore files, so they still win). Pre-existing
graphs without the sidecar behave as before. Adds a regression test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
safishamsi
2026-07-14 14:03:20 +01:00
co-authored by Claude Opus 4.8
parent fd54f0ef0b
commit 2795a18bda
4 changed files with 78 additions and 1 deletions
+2
View File
@@ -4,6 +4,8 @@ Full release notes with details on each version: [GitHub Releases](https://githu
## 0.9.16 (unreleased)
- Fix: `--exclude` patterns now survive into `update`/`watch`/git-hook rebuilds instead of applying only to the initial scan (#1886). The patterns were never persisted, so the first rebuild re-ran `detect()` without them and silently re-indexed the excluded paths. `extract` now records them in a `graphify-out/.graphify_build.json` sidecar and `_rebuild_code` re-applies them on every rebuild (they still layer after `.gitignore`/`.graphifyignore`/`.git/info/exclude`, so they keep winning). Graphs built before this release simply have no sidecar and behave as before.
- Fix: the dedup ID-collision warning now reports in proportion to what is actually lost, and an ID collision resolves to a deterministic survivor (#1851 / #1852, thanks @bchan84x). Two nodes can mint the same `<path>_<entity>` ID when a document merely references an entity defined elsewhere; the old code kept whichever arrived first and always printed a scary "two files with the same name" warning that overstated the loss (edges rewire to the survivor, so a reference collapse loses nothing). Now the node whose `source_file` actually defines the ID wins, a harmless reference collapse is silent, a same-file relabel is a quiet note, and only a genuine cross-file collision warns. The survivor is chosen by a total order (definer first, then the shorter/canonical label, then lexically) so it no longer depends on chunk arrival order, including when several same-file nodes co-define one ID.
- Fix (regression from 0.9.15): a nested `.gitignore`/`.graphifyignore` no longer applies its patterns outside its own directory (#1873 / #1887 / #1885, thanks @Alwyn93). When 0.9.15 started reading nested ignore files, a non-anchored pattern was still matched against the path relative to the scan root, so a nested bare `*` (a common "ignore this scratch dir" idiom, e.g. what the hypothesis library writes into `.hypothesis/`) matched every file and `detect()` returned zero files, silently producing an empty graph. Patterns are now matched relative to the directory whose ignore file defined them and only apply within that subtree; the anchor directory itself is exempt.
+4
View File
@@ -2071,6 +2071,10 @@ def dispatch_command(cmd: str) -> None:
out_root = (out_dir.resolve() if out_dir else target)
graphify_out = out_root / _GRAPHIFY_OUT
graphify_out.mkdir(parents=True, exist_ok=True)
# Persist --exclude so later update/watch/hook rebuilds re-apply it
# instead of silently re-including the excluded paths (#1886).
from graphify.watch import _write_build_config as _write_build_cfg
_write_build_cfg(graphify_out, excludes=cli_excludes or None)
stages = _StageTimer(cli_timing)
+46 -1
View File
@@ -69,6 +69,44 @@ def _drain_pending(out_dir: Path) -> list[Path]:
return out
# Build options that must survive into later rebuilds. The initial `extract`
# scan honours `--exclude`, but `update`/`watch`/hook rebuilds re-run detect()
# and would silently re-include excluded paths unless the patterns are persisted
# (#1886). We store them beside the graph so any rebuild driver can re-apply them.
_BUILD_CONFIG_FILENAME = ".graphify_build.json"
def _write_build_config(out_dir: Path, *, excludes: "list[str] | None") -> None:
"""Persist build options (currently ``--exclude`` patterns) under ``out_dir``.
Best-effort and non-clobbering: with no excludes it leaves any existing file
untouched, so a plain rebuild never erases patterns a prior extract recorded.
"""
if not excludes:
return
try:
out_dir.mkdir(parents=True, exist_ok=True)
(out_dir / _BUILD_CONFIG_FILENAME).write_text(
json.dumps({"excludes": list(excludes)}), encoding="utf-8"
)
except OSError:
pass
def _read_build_excludes(out_dir: Path) -> list[str]:
"""Return the persisted ``--exclude`` patterns for this graph, or []."""
try:
path = out_dir / _BUILD_CONFIG_FILENAME
if path.is_file():
cfg = json.loads(path.read_text(encoding="utf-8"))
ex = cfg.get("excludes") if isinstance(cfg, dict) else None
if isinstance(ex, list):
return [str(x) for x in ex if isinstance(x, str) and x]
except (OSError, json.JSONDecodeError):
pass
return []
def _merge_changed_paths(*sources: "list[Path] | None") -> list[Path]:
"""Concatenate path lists, preserving order and dropping duplicates.
@@ -827,7 +865,14 @@ def _rebuild_code(
from graphify.export import to_json, to_html
from graphify.security import check_graph_file_size_cap
detected = detect(watch_path, follow_symlinks=follow_symlinks)
# Re-apply the excludes the initial extract recorded, so an update/watch/
# hook rebuild does not silently re-include deliberately excluded paths
# (#1886).
_persisted_excludes = _read_build_excludes(out)
detected = detect(
watch_path, follow_symlinks=follow_symlinks,
extra_excludes=_persisted_excludes or None,
)
code_files = [Path(f) for f in detected['files']['code']]
# Include document files that have AST extractors (e.g. .md, .mdx, .qmd)
+26
View File
@@ -219,6 +219,32 @@ def test_update_rebuilds_with_nested_star_gitignore(tmp_path):
assert not any("scratch/junk.py" in s for s in sources)
def test_rebuild_honors_persisted_excludes(tmp_path):
"""#1886: `--exclude` recorded at extract time must survive into update/watch/
hook rebuilds. Before the fix only the initial scan applied the excludes, so
the first rebuild silently re-indexed the excluded paths. _rebuild_code now
reads the persisted build config and re-applies them."""
import json
from graphify.watch import _rebuild_code, _write_build_config
corpus = tmp_path / "corpus"
(corpus / "src").mkdir(parents=True)
(corpus / "vendor").mkdir()
(corpus / "src" / "app.py").write_text("def keep(): return 1\n", encoding="utf-8")
(corpus / "main.py").write_text("def top(): return 2\n", encoding="utf-8")
(corpus / "vendor" / "lib.py").write_text("def vendored(): pass\n", encoding="utf-8")
_write_build_config(corpus / "graphify-out", excludes=["vendor"])
assert _rebuild_code(corpus, no_cluster=True, acquire_lock=False) is True
graph = json.loads((corpus / "graphify-out" / "graph.json").read_text(encoding="utf-8"))
sources = {n.get("source_file", "") for n in graph["nodes"]}
assert any("src/app.py" in s for s in sources) and any("main.py" in s for s in sources)
assert not any("vendor/lib.py" in s for s in sources), (
"rebuild silently re-included an excluded path (#1886)"
)
def test_graphify_root_preserves_absolute_when_user_supplied(tmp_path):
"""When the caller supplies an absolute path, ``.graphify_root`` stores
that absolute form verbatim — preserving explicit-absolute intent."""