fix(watch): refresh JS/TS imports after resolver input changes
Refs #5017.
Co-Authored-By: OpenAI Codex <noreply@openai.com>
(cherry picked from commit 84ca430505)
This commit is contained in:
1 parent
c66ccae853
commit
4feadeda0d
2 files changed
+101
-16
No files matched your search
+27
-16
@@ -2350,6 +2350,7 @@ def _rebuild_code(
|
||||
classify_manifest_deletions,
|
||||
classify_manifest_files,
|
||||
detect,
|
||||
is_resolution_input,
|
||||
load_manifest,
|
||||
)
|
||||
from graphify.build import build_from_json, _is_ast_tier, _norm_source_file as _nsf
|
||||
@@ -2440,12 +2441,13 @@ def _rebuild_code(
|
||||
if incremental and changed_paths is None and not force and (out / "graph.json").is_file():
|
||||
manifest = load_manifest(str(out / "manifest.json"), root=watch_root)
|
||||
if manifest:
|
||||
resolution_inputs = detected.get("resolution_inputs") or []
|
||||
_changed_buckets, _unchanged_buckets = classify_manifest_files(
|
||||
detected["files"], manifest, kind="ast"
|
||||
{**detected["files"], "resolution_inputs": resolution_inputs},
|
||||
manifest,
|
||||
kind="ast",
|
||||
)
|
||||
derived = [
|
||||
Path(f) for bucket in _changed_buckets.values() for f in bucket
|
||||
]
|
||||
modified = [f for bucket in _changed_buckets.values() for f in bucket]
|
||||
# Rows that left the corpus belong in the change set too. A row
|
||||
# whose file is gone from disk must reach the deleted branch
|
||||
# below, or its nodes survive and the shrink guard refuses the
|
||||
@@ -2453,24 +2455,33 @@ def _rebuild_code(
|
||||
# A row that is still alive but out of scan is an ignore-rule
|
||||
# change, which the corpus sweep handles on positive evidence
|
||||
# alone (#2495), so it stays out of the change set.
|
||||
_deleted_rows, _excluded_rows = classify_manifest_deletions(
|
||||
deleted_rows, excluded_rows = classify_manifest_deletions(
|
||||
manifest,
|
||||
(f for bucket in detected["files"].values() for f in bucket),
|
||||
[
|
||||
*resolution_inputs,
|
||||
*(f for bucket in detected["files"].values() for f in bucket),
|
||||
],
|
||||
root=watch_root,
|
||||
)
|
||||
derived.extend(Path(f) for f in _deleted_rows)
|
||||
changed_paths = derived
|
||||
_scanned = sum(len(v) for v in detected["files"].values())
|
||||
if derived:
|
||||
if any(is_resolution_input(f) for f in (*modified, *deleted_rows, *excluded_rows)):
|
||||
# An unchanged importer may resolve differently when its
|
||||
# config or workspace layout changes. Re-resolve the corpus.
|
||||
print(
|
||||
f"[graphify watch] Incremental: re-extracting {len(derived)} "
|
||||
f"of {_scanned} scanned files that changed since the last build."
|
||||
"[graphify watch] JS/TS resolution inputs changed; rebuilding the full code graph."
|
||||
)
|
||||
else:
|
||||
print(
|
||||
f"[graphify watch] Incremental: none of the {_scanned} scanned "
|
||||
"files differ from the last build."
|
||||
)
|
||||
changed_paths = [Path(f) for f in (*modified, *deleted_rows)]
|
||||
scanned = sum(len(v) for v in detected["files"].values())
|
||||
if changed_paths:
|
||||
print(
|
||||
f"[graphify watch] Incremental: re-extracting {len(changed_paths)} "
|
||||
f"of {scanned} scanned files that changed since the last build."
|
||||
)
|
||||
else:
|
||||
print(
|
||||
f"[graphify watch] Incremental: none of the {scanned} scanned "
|
||||
"files differ from the last build."
|
||||
)
|
||||
|
||||
# #2495: hand reconcile the same ignore decisions the detect() call
|
||||
# above made, so a newly-ignored file that still exists on disk is
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
"""A resolver input can change imports in a file whose bytes did not change."""
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from graphify import extract as extract_mod
|
||||
from graphify.watch import _rebuild_code
|
||||
|
||||
|
||||
@pytest.mark.parametrize("change", ["edit", "remove", "add"])
|
||||
def test_update_refreshes_unchanged_typescript_importers(tmp_path, change):
|
||||
source = tmp_path / "main.ts"
|
||||
source.write_text('import { target } from "@app/target";\nexport function run() { return target(); }\n',
|
||||
encoding="utf-8")
|
||||
for name in ("old", "new"):
|
||||
directory = tmp_path / name
|
||||
directory.mkdir()
|
||||
(directory / "target.ts").write_text(f'export function target() {{ return "{name}"; }}\n',
|
||||
encoding="utf-8")
|
||||
config = tmp_path / "tsconfig.json"
|
||||
|
||||
def write_config(target):
|
||||
config.write_text(json.dumps({"compilerOptions": {"baseUrl": ".", "paths": {
|
||||
"@app/*": [f"./{target}/*"],
|
||||
}}}), encoding="utf-8")
|
||||
|
||||
graph = tmp_path / "graphify-out" / "graph.json"
|
||||
|
||||
def local_imports():
|
||||
data = json.loads(graph.read_text(encoding="utf-8"))
|
||||
nodes = {node["id"]: node for node in data["nodes"]}
|
||||
return {
|
||||
nodes[link["target"]].get("source_file", "").replace("\\", "/")
|
||||
for link in data["links"]
|
||||
if link["relation"] == "imports_from"
|
||||
and nodes[link["source"]].get("source_file") == "main.ts"
|
||||
} & {"old/target.ts", "new/target.ts"}
|
||||
|
||||
if change != "add":
|
||||
write_config("old")
|
||||
assert _rebuild_code(tmp_path, no_cluster=True)
|
||||
assert local_imports() == (set() if change == "add" else {"old/target.ts"})
|
||||
|
||||
if change == "remove":
|
||||
config.unlink()
|
||||
else:
|
||||
write_config("new")
|
||||
assert _rebuild_code(tmp_path, incremental=True, no_cluster=True)
|
||||
|
||||
assert local_imports() == (set() if change == "remove" else {"new/target.ts"})
|
||||
|
||||
|
||||
def test_update_rechecks_importers_after_lockfile_only_change(tmp_path, monkeypatch):
|
||||
source = tmp_path / "main.ts"
|
||||
source.write_text('import "external";\nexport function run() {}\n', encoding="utf-8")
|
||||
lockfile = tmp_path / "pnpm-lock.yaml"
|
||||
lockfile.write_text("lockfileVersion: '9.0'\n", encoding="utf-8")
|
||||
assert _rebuild_code(tmp_path, no_cluster=True)
|
||||
|
||||
actual_extract = extract_mod.extract
|
||||
batches = []
|
||||
|
||||
def record_extract(paths, **kwargs):
|
||||
batches.append(list(paths))
|
||||
return actual_extract(paths, **kwargs)
|
||||
|
||||
monkeypatch.setattr(extract_mod, "extract", record_extract)
|
||||
lockfile.write_text("lockfileVersion: '9.0'\nimporters: {}\n", encoding="utf-8")
|
||||
assert _rebuild_code(tmp_path, incremental=True, no_cluster=True)
|
||||
assert batches == [[source]]
|
||||
|
||||
assert _rebuild_code(tmp_path, incremental=True, no_cluster=True)
|
||||
assert batches == [[source]]
|
||||
Reference in new issue
Block a user