DEV Community

VANSH ARORA
VANSH ARORA

Posted on

Import Edges Beyond JavaScript: Python, Go, Rust and Java in TokenCap

TokenCap builds a file graph of your repository: one node per source file, one edge per import that resolves to another file in the repository. Task packs, risk labels and the MCP dependency tool all read that graph. It used to be much smaller than most repositories.

What the graph used to leave out

Two limits, both in src/graph/:

  1. The scanner only added .ts, .tsx, .js, .jsx, .mjs and .cjs source files (plus some infrastructure files) to the graph. Python, Go, Rust, Java, Vue and Svelte files were not nodes, so no task pack could select them.
  2. The import parser only returned specifiers that started with ./ or ../. The resolver already understood tsconfig paths and baseUrl, but an import like @/lib/auth was discarded before it got there.

Now those languages are graph nodes, and their imports become edges.

How each language resolves

The specifiers are read with line-based patterns per language, then resolved against the list of files in the repository. Anything that does not resolve to a repository file is dropped, which is how standard library and third-party imports stay out.

I ran the parser and resolver from src/graph/ on a small set of paths to show real output:

backend/app/api/routes.py
  py:app.core.config  -> backend/app/core/config.py
  ..db                -> backend/app/db.py
  py:os               -> null
  py:requests         -> null

cmd/server/main.go
  fmt                             -> null
  example.com/svc/internal/store  -> internal/store/store.go

src/main.rs
  rs:mod::walk             -> src/walk.rs
  rs:mod::flags            -> src/flags/mod.rs
  rs:crate::walk::Walker   -> src/walk.rs
  rs:crate::flags::parse   -> src/flags/parse.rs

src/main/java/com/example/App.java
  java:com.example.Foo     -> src/main/java/com/example/Foo.java
  java:java.util.List      -> null

src/components/Login.vue
  @/lib/auth               -> src/lib/auth.ts
Enter fullscreen mode Exit fullscreen mode

The rules behind those lines:

  • Python. Relative imports count leading dots as levels up and resolve to mod.py or mod/__init__.py. Absolute imports are tried against import roots: the directory above the importer's top-level package, the repository root, src/, and any directory that holds a top-level package. That covers layouts like backend/app/.... from pkg import mod is also tried as a submodule.
  • Go. Module paths come from go.mod at the repository root and in workspace package directories. An import names a package directory, so it resolves to one non-test .go file in that directory, preferring <dir>.go, then doc.go. A nested sub-package is never picked.
  • Rust. mod x; declarations and use paths that start with crate::, super:: or self:: resolve to x.rs or x/mod.rs. Use trees like crate::{a, b::{self, c}} are expanded. External crates are not followed.
  • Java. import com.example.Foo; and static imports resolve to the file whose path ends in com/example/Foo.java.
  • Vue and Svelte. The JS import patterns run over the file, so <script> imports become edges, including imports of other .vue and .svelte files.
  • JS/TS. Every string specifier is kept now, not only relative ones. Bare specifiers go through tsconfig/jsconfig paths and baseUrl, package.json imports (#internal/*), and workspace package names. Dynamic import("./x") is matched too.

Why the graph matters for a task pack

Retrieval scores files by the words and identifiers in the task. The graph adds two things on top.

Proximity. The 25 best-scoring files each lend 15% of their score to the files they import and the files that import them. A neighbour never outranks the file it came from. In practice this pulls in the module a matched function calls into, or the route that uses a matched service, even when that file shares no words with the task. Each file in the pack records why it was chosen, and for these files the reason names the file that imports it or that it imports. tokencap ask "<task>" --json carries the full explanation.

Risk and impact. A file's importer count feeds its importance and risk level. Five or more importers make a file HIGH risk; a database file with five or more importers is CRITICAL. Those labels show up in the pack's Risk Areas section and act as small multipliers on files that already match the task. Without edges, a Python service imported by thirty modules looked exactly like a script nobody used.

Seeing the edges

To build the graph and open it in a browser:

tokencap make --open-graph
Enter fullscreen mode Exit fullscreen mode

From an agent, the MCP tool tokencap_dependencies walks the same graph from a file: what it imports, what imports it, up to five hops. It is available once your host runs tokencap serve --mcp.

Limits

  • Import parsing uses line-based patterns, not the Tree-sitter parser. An import built at runtime from a string is invisible.
  • Java wildcard imports (import com.example.util.*;) are not followed.
  • A one-word Python import like import config that is not found under an import root is not matched to a same-named file elsewhere in the repository. It is far more likely to be the standard library or a third-party package. Dotted imports still fall back to a path match.
  • A Go import becomes one edge to one file of the package, not to every file in it.
  • Edges are file to file. Call-level resolution is a separate graph, used by tokencap ask impact.

On the benchmarks/fair/ set (100 real commits across JavaScript, TypeScript, Vue, Python, Go, Rust and Java), the task pack shows 89% of the needed files as code and holds 74% of the edited lines, at a median 18.4x fewer tokens than a keyword grep. That figure covers the whole retrieval and packing pipeline; the benchmark does not isolate import edges.

More: tokencap.vansharora.app

Top comments (0)