Skip to content

TypeScript: file-cycle false positive from ./index mentions inside comments #1193

Open

Description

@Vincentas

Summary

graphify 0.8.35 reports a 2-file cycle for our apps/web/lib/i18n/ module even though the TypeScript imports are acyclic.

- 2-file cycle: apps/web/lib/i18n/index.ts -> apps/web/lib/i18n/locale-routes.ts -> apps/web/lib/i18n/index.ts

What the code actually does

apps/web/lib/i18n/index.ts:

export { LOCALE_COOKIE, LOCALES, DEFAULT_LOCALE, isLocale } from './locale';
export type { Locale } from './locale';
import type { Locale } from './locale';

export {
  LOCALE_URL_PREFIXES,
  LOCALE_PREFIXED_ROUTE_SEGMENTS,
  matchLocalePrefix,
  localePath,
} from './locale-routes';

apps/web/lib/i18n/locale-routes.ts:

// Locale-URL routing primitives. Kept in a SEPARATE module from `index.ts`
// (which imports the en/zh/es bundles) so the Edge `proxy.ts` middleware can
// import the matcher without pulling every translation string into the Edge
// bundle. Imports the `Locale` type from the tiny ./locale module rather than
// from ./index so the dependency graph stays acyclic (graphify 2026-06-07).
// Re-exported from `index.ts` for non-Edge callers.
import type { Locale } from './locale';

The only index.ts mentions inside locale-routes.ts are inside // comments (backtick-quoted within the prose). There is no actual import ... from './index' statement.

Hypothesis

The TS extractor appears to count backtick-quoted module references inside line comments as import edges. Possibly the regex strips comment delimiters before matching import / from patterns, or matches on any backtick-quoted module-shaped string.

Repro

graphify 0.8.35
node 22
TypeScript 5.x project

Take any TS module B that has zero imports from a sibling module A but mentions A in a doc comment like // from \./A``. graphify reports a cycle.

Expected

graphify should parse TypeScript ESM imports only from import ... from "..." / import("...") / export ... from "..." statements, ignoring identifiers inside // and /* */ comments and inside string/template literals.

Workaround we used

Removed the backtick-quoted './index' mention from a code comment — that suppressed the false positive locally. The fix didn't survive code review (the comment was useful for human readers explaining what was moved where).

Why this matters

Cycles are one of the headline findings in GRAPH_REPORT.md and trigger refactor PRs. A false positive directs engineering time at a non-existent problem and erodes trust in the report.

Happy to test a fix on a representative repo if useful. Thanks for the great tool.

Activity

  1. Vincentas commented on Jun 8, 2026

    @Vincentas
    Author

    Follow-up investigation

    Cloned safishamsi/graphify to dig in. The reported edge has these attributes in our graphify-out/graph.json:

    {
      "relation": "imports_from",
      "context": "import",
      "confidence": "EXTRACTED",
      "source_file": "apps/web/lib/i18n/locale-routes.ts",
      "source_location": "L6",
      "weight": 1.0,
      "confidence_score": 1.0,
      "source": "i18n_locale_routes",
      "target": "i18n_index"
    }

    But tree-sitter-typescript on the SAME file reports only one import_statement, at row=6 (so L7 1-indexed), pointing to './locale':

    node.start_point=Point(row=6, column=0) end_point=Point(row=6, column=39)
      string child: "'./locale'" sp=Point(row=6, column=28)

    So:

    • tree-sitter sees no AST node at L6 (it's a // comment).
    • The resolver _resolve_js_import_target('./locale', ...) correctly returns ('apps_web_lib_i18n_locale_ts', .../locale.ts).
    • Yet graph.json carries a phantom edge from L6 to i18n_index.

    Repro is reliable: rm -rf graphify-out/cache && graphify update . produces the same phantom edge.

    I'm not able to identify the exact codepath that emits this edge — it doesn't come from _import_js (which would produce L7, target i18n_locale_ts), nor from _resolve_js_import_target directly, nor from symbol_resolution.py (that path is bash-source only).

    Possible angles for the maintainer:

    1. There may be a cross-file inference path that creates an edge to i18n_index based on apps/web/lib/i18n/index.ts re-exporting from apps/web/lib/i18n/locale-routes.ts (forward edge), with a downstream pass adding the inverse imports_from edge — but the edge is marked EXTRACTED not INFERRED, which argues against this.
    2. A regex pass over file content somewhere that I haven't located, matching from \index.ts`` literally inside the comment.
    3. A stale-cache issue not invalidated by rm -rf graphify-out/cache (the cache file stat-index.json had no entry for locale-routes.ts).

    Happy to send a draft patch if you can point at the codepath. Otherwise the cleanest fix I can see from outside is a post-pass in analyze.find_import_cycles that re-validates the source_location line against tree-sitter before counting it toward a cycle, but that feels like papering over the symptom.

    Reproducer repo on request.

  2. ayushcodes10 commented on Sep 16, 2026

    @ayushcodes10
    Contributor

    Already fixed on current v8 (or never reproduced against the AST-based TS extractor — see caveat below).

    Reproduced the repro shape: a locale-routes.ts file whose only mention of index.ts is inside a // comment, alongside a real import type { Locale } from './locale'. Extracting all three files and inspecting the imports edges:

    [('locale_routes_ts', 'locale_ts')]
    

    Only one imports edge exists, from locale-routes.ts to locale.ts. There is no edge from locale-routes.ts back to index.ts, so the edge set stays a DAG — no 2-file cycle between index.ts and locale-routes.ts can be reported from this edge set.

    Caveat: this confirms the import-edge extraction itself is correct for the exact shape in this issue (current TS extraction walks import_statement AST nodes, which are structurally separate from comment nodes, so a comment mentioning a filename can't produce a phantom edge by construction). I didn't separately trace the standalone cycle-report routine, and I don't know which historical change (if any) fixed this versus it never applying to the AST-based extractor. If you still have a repo where this reproduces, a concrete GRAPH_REPORT.md excerpt showing the false cycle would help re-open with a solid repro.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions