Skip to content

fix(watch): re-extract the importers of a changed JS/TS barrel on incremental runs - #5636

Closed
0xkaushik-ai wants to merge 5 commits into
Graphify-Labs:v8from
0xkaushik-ai:fix/incremental-ts-barrel-retarget
Closed

0xkaushik-ai wants to merge 5 commits into
Graphify-Labs:v8from
0xkaushik-ai:fix/incremental-ts-barrel-retarget

Conversation

@0xkaushik-ai

@0xkaushik-ai 0xkaushik-ai commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5632.

An incremental update that edits or deletes a JS/TS barrel only re-extracted the barrel. The resolved imports / calls edges of the files importing through it belong to those files, so they were kept on the module the barrel used to re-export. That module still exists, so nothing made them dangle, and they stayed as EXTRACTED edges a full build of the same tree doesn't have.

Change

  • graphify/watch.py, _js_barrel_importers: a target is a barrel when it had a re_exports edge in the stored graph. Its importers are the JS/TS files with a stored imports / imports_from / re_exports edge onto one of its nodes. The walk continues through importers that are barrels themselves (api.ts re-exporting ./lib).
  • _incremental_partners combines it with _id_collision_partners. Both incremental paths call it: _rebuild_code (update / hook / watch) and graphify extract on an existing graph (graphify/cli.py).
  • A changed module that re-exports for the first time (no stored re_exports edge yet) counts as a barrel when its new source has an export ... from statement (comments between the tokens allowed), so its importers are re-extracted too.
  • graphify/cli.py: on extract, a barrel found missing only by reconciling graph.json against the scan (_stale_graph_sources, not listed in the manifest) counts as deleted too, so its importers are re-extracted before it is pruned.
  • A change to a file with no re-exports widens nothing. Restricted to JS/TS: the Python equivalent already matches a full build.

Invariant: an incremental update gives the same edges as a full build of the same tree.

Tests

tests/test_js_incremental_barrel_retarget.py covers update and extract, with a direct and a nested barrel, a module that becomes a barrel in the edit (including with comments inside the re-export), a barrel that stops re-exporting (export *, export { x }, export * as ns), and a deleted barrel that only the stored graph still names.

  • On v8 all 19 cases fail; with the fix all 19 pass.
  • Re-export / incremental / watch / update suites: 372 passed, 3 skipped.
  • Full suite: 12465 passed, 0 failed.
  • ruff check clean; pyright output on watch.py and cli.py is unchanged vs v8.

Cost: editing a barrel re-extracts every JS/TS file that imports it, which can be many files for a large index.ts.

Overlap: #5635 (fixes #5631 and #5633) also wraps the _id_collision_partners call in _rebuild_code, so whichever lands second needs a small rebase: one wrapper calling both passes. I've checked that combination locally: #5634 and both PRs merged give 12477 passed, 0 failed.

🤖 Generated with Claude Code

…remental runs

app.ts imports x from ./lib, and lib/index.ts re-exports it from ./x. The
resolved imports and calls edges land on lib/x.ts but belong to app.ts. An
update that re-pointed the barrel at ./z re-extracted only the barrel, so
app.ts kept EXTRACTED edges to lib/x.ts that a full build of the same tree
never has; lib/x.ts still existed, so nothing made them dangle.

When a target (or deleted file) had a re_exports edge in the stored graph,
also re-extract the JS/TS files with a stored imports/imports_from/re_exports
edge onto it, following importers that are barrels themselves. Both
incremental paths, _rebuild_code and extract on an existing graph, get it
through one helper next to the id-collision partners.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @0xkaushik-ai. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 29, 2026

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Graphify review View in Graphify

Nothing blocking · 5 possible issues to check · Merge confidence 5/5

5 possible issues to check · High impact

Incremental rebuilds now include ID-collision partners and JS/TS importers affected by changed or removed barrels.

5 possible issues we couldn't confirm
  • JS/TS barrel detection depends on a constant named for cache bypass, which may miss some JS/TS suffixes · graphify/watch.py:360
  • Files that import a re-exported name through a barrel may be missed when the barrel changes, leaving stale edges · graphify/watch.py:387
  • Incremental graphify update may skip re-extracting importers of changed JS/TS barrels because _incremental_partners now receives the who · graphify/cli.py:5323
  • +2 more in Graphify

They don't change the score.

Code graph · 1042 functions depend on this change
  • 104 of 659 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 87494d2 · 7 earlier findings not re-checked

Comment thread graphify/watch.py
Comment on lines +359 to +362
relation = edge.get("relation")
if not owner or not _is_js(owner) or relation not in _BARREL_IMPORT_RELATIONS:
continue
if relation == "re_exports":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  Importers stay stale when a module first becomes a barrel

When a changed JS/TS module gains its first re-export, unchanged files importing through it are not re-extracted, so their graph can disagree with a fresh build.

Start _js_barrel_importers traversal from changed JS/TS modules even if they are not stored barrels.

Evidence · checked against the code

Expected: The unchanged importers are re-extracted and their import and call edges reflect the new exports.
Actually: The importer walk starts only from changed paths already identified as barrels in the stored graph, so it skips this module and does not widen extraction to its importers.

  • Independent source review: Checked the added retargeting test: its initial tree already gives lib/index.ts a re-export, so it does not counter the first-re-export case. No guard for that case appears in the inspected helper or caller.
  • Path history: Read changes to graphify/watch.py: ca62ef4, 2ee82ff, 809666e, 707becd.
  • File history may be incomplete: shallow clones and renames can hide earlier changes.
Prompt to fix with AI
In graphify/watch.py, _js_barrel_importers (lines 359-362):
Problem: Importers stay stale when a module first becomes a barrel
Expected: The unchanged importers are re-extracted and their import and call edges reflect the new exports.
Actually: The importer walk starts only from changed paths already identified as barrels in the stored graph, so it skips this module and does not widen extraction to its importers.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 7af3471. A changed JS/TS module whose new source has an export ... from statement now counts as a barrel even without a stored re_exports edge, so its importers are re-extracted. Added test_module_that_first_becomes_a_barrel_matches_full_build (update and extract), which fails without the change.

…his edit

The barrel walk started only from targets that had a `re_exports` edge in the
stored graph. A module that defined `x` itself and now does
`export * from './z'` has none yet, so the files importing `x` from it were
not re-extracted: `graphify update` dropped their edges and drew none to
lib/z.ts, and a repeated `graphify extract` kept EXTRACTED edges to the
removed lib_index_x.

A changed JS/TS target whose new source has an `export ... from` statement is
now treated as a barrel too. Over-matching only widens the re-extraction.

Reported by the review bot on Graphify-Labs#5636.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread graphify/watch.py
Comment on lines +375 to +378
relation = edge.get("relation")
if not owner or not _is_js(owner) or relation not in _BARREL_IMPORT_RELATIONS:
continue
if relation == "re_exports":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  Importers stay stale when a barrel stops re-exporting

When an edited barrel stops re-exporting, unchanged files that import through it are not re-extracted, so their graph edges can still point at symbols the barrel no longer exports.

Treat stored export-star imports_from edges as barrel seeds in _js_barrel_importers.

Evidence · checked against the code

Expected: The unchanged importers are re-extracted so their graph edges reflect the barrel’s current exports.
Actually: The stored export-star edge is imports_from, not re_exports, and the new source no longer matches the re-export regex. The changed file is therefore not a barrel seed, so its unchanged importers are omitted from the incremental extraction set and can retain stale edges.

This was already on the base branch; the change keeps it.

  • Independent source review: Checked the CLI and watch callers and the extractor’s re-export handling. Both incremental paths rely on _incremental_partners; the extractor code read emits imports_from for the module edge and only emits symbol-level re_exports edges inside the export-clause/specifier loop. No guard or alternate route found that widens targets for a stored export-star barrel that stops re-exporting.
Prompt to fix with AI
In graphify/watch.py, _js_barrel_importers (lines 375-378):
Problem: Importers stay stale when a barrel stops re-exporting
Expected: The unchanged importers are re-extracted so their graph edges reflect the barrel’s current exports.
Actually: The stored export-star edge is imports_from, not re_exports, and the new source no longer matches the re-export regex. The changed file is therefore not a barrel seed, so its unchanged importers are omitted from the incremental extraction set and can retain stale edges.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think this one holds: for export * from './x', export { x } from './x' and export * as ns from './x' the stored graph has a re_exports edge from lib/index.ts (on both the update and the extract path), so _js_barrel_importers already seeds it, and when the barrel stops re-exporting app.ts is re-extracted and matches a full build. Added test_barrel_that_stops_reexporting_matches_full_build for all three forms on both paths in 84fd431: the 6 cases fail on v8 and pass here.

lib/index.ts re-exported x from ./x (`export *`, `export { x }`,
`export * as ns`) and now defines its own x. Its stored re_exports edge
already marks it as a barrel, so app.ts is re-extracted and keeps no edge to
lib_x_x. All 6 cases fail on v8 and pass with this branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread graphify/cli.py
Comment on lines +5309 to +5312
# unchanged importers of a changed JS/TS barrel re-resolve
# through it only when re-extracted.
from graphify.watch import _incremental_partners
code_files = list(code_files) + _incremental_partners(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  Importers are not refreshed when a stale barrel disappears

If an old graph contains a barrel that is no longer in the scan, its unchanged importers can keep edges resolved through that barrel after the barrel is pruned.

Include stale barrel identities when seeding _incremental_partners.

Evidence · checked against the code

Expected: The barrel's unchanged importers are re-extracted before stale graph sources are pruned.
Actually: The stale barrel is not included in _incremental_partners' deleted identities or changed targets, so its importers can retain edges resolved through it while the barrel's graph contribution is pruned.

This was already on the base branch; the change keeps it.

  • Independent source review: For barrels in deleted_files or excluded_files, _incremental_partners receives their identities and the traversal can select importers. That does not cover a stale source found only in _stale_graph_sources. The existing test inspected checks pruning a deleted semantic file, not re-resolution of barrel importers.
  • The case depends on a missing or excluded barrel being detected through _stale_graph_sources without also appearing in the explicit deleted or excluded lists.
Prompt to fix with AI
In graphify/cli.py, _incremental_partners (lines 5309-5312):
Problem: Importers are not refreshed when a stale barrel disappears
Expected: The barrel's unchanged importers are re-extracted before stale graph sources are pruned.
Actually: The stale barrel is not included in `_incremental_partners`' deleted identities or changed targets, so its importers can retain edges resolved through it while the barrel's graph contribution is pruned.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed and fixed in d92a7bf. Reproduced on extract: after dropping lib/index.ts from the manifest and deleting the file, the barrel only surfaced through _stale_graph_sources, so app.ts kept imports/calls edges to lib_x_x that a full build doesn't have. The stale sources are now passed to _incremental_partners with the deleted and excluded files, the same way _cpp_reconciliation_paths already gets them a few lines up.

Added test_barrel_only_the_graph_still_names_matches_full_build: it fails without the change and passes with it (the file is now 13 passing).

An incremental extract seeded the barrel-importer walk with the manifest's
deleted and excluded files only. A barrel found missing solely by reconciling
graph.json against the scan (_stale_graph_sources: absent from the manifest)
was pruned while its unchanged importers kept EXTRACTED edges resolved through
it. Feed those stale sources into _incremental_partners as deleted too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread graphify/watch.py Outdated
Comment on lines +315 to +318
# `export type { T } from`. Over-matching (e.g. inside a comment) only widens
# the re-extraction; it never adds an edge.
_JS_REEXPORT_RE = re.compile(
r"\bexport\s+(?:type\s+)?(?:\*(?:\s*as\s+[\w$]+)?|\{[^}]*\})\s*from\s*['\"]"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  Commented re-exports can leave importers pointing at old symbols

When a changed JavaScript or TypeScript file first becomes a barrel using comments between export and the re-export clause, unchanged importers may not be re-extracted and can retain edges to the old symbols.

Evidence · checked against the code

Expected: Incremental extraction re-extracts those importers so their edges match a full build.
Actually: The regex misses the re-export, so the changed module is not recognized as a new barrel and its importers are not widened into extraction; their stored edges can remain stale.

  • Independent source review: Files already present in the stored graph with a re_exports edge are placed in barrels directly and do not depend on the regex. That does not cover the claimed newly re-exporting file, which has no stored re-export edge. The incremental rebuild and CLI both call _incremental_partners; no mitigating route for the claimed case was found.
  • The existing tests cover re-export forms without comments between the tokens.
  • Also reported as: Commented re-exports can leave importers pointing at the old symbol
Prompt to fix with AI
In graphify/watch.py, _has_js_reexport (lines 315-318):
Problem: Commented re-exports can leave importers pointing at old symbols
Expected: Incremental extraction re-extracts those importers so their edges match a full build.
Actually: The regex misses the re-export, so the changed module is not recognized as a new barrel and its importers are not widened into extraction; their stored edges can remain stale.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed and fixed in 87494d2. Reproduced with export /* c */ * from './z', a line comment after export, export * /* c */ from and export { x } /* c */ from: a full build draws the re_exports edge, but the regex didn't match, so app.ts kept its edges to lib_index_x. _JS_REEXPORT_RE now allows block and line comments wherever it allowed whitespace.

test_module_that_first_becomes_a_barrel_matches_full_build is now parametrized over a plain, block-comment, line-comment and comment-before-from form; the three comment forms fail without the change and pass with it (the file is now 19 passing).

A module that first becomes a barrel is recognised from its new source, and
the regex required plain whitespace between `export`, `*`/`{...}` and `from`.
`export /* c */ * from './z'` was missed, so its unchanged importers were not
re-extracted and kept edges to the symbols it used to define. Allow block and
line comments wherever whitespace is allowed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
safishamsi pushed a commit that referenced this pull request Nov 30, 2026
safishamsi added a commit that referenced this pull request Nov 30, 2026
Extractor accuracy (Swift #5641, Apex #5627, Python #5647, TypeScript
#5604/#5614/#5612, Bash caller-var #5602), GitHub Actions run-step
extraction (#5603), incremental correctness (#5634/#5638/#5636), symlink
and semantic-cache fixes (#5617/#5618), export/report hardening and perf
(#5643/#5644/#5645/#5646/#5619/#5620/#5621), and CLI/docs/skill/install
work (#5629/#5628/#5626/#5624/#5623/#5625/#5642/#5583). AST cache schema
59 to 60; Swift/Apex/Python/Bash/GHA inputs re-extract on first build.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.124 (just released to PyPI), cherry-picked with your authorship preserved so it shows under your name in the history. Thanks @0xkaushik-ai for re-extracting importers of a changed JS/TS barrel — verified green in the full suite and, where applicable, smoke-tested against the published package. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

2 participants