Skip to content

fix: resolve JVM imports in multi-module Maven/Gradle projects (0 edges bug)#22

Merged
giancarloerra merged 1 commit intogiancarloerra:mainfrom
sb-giithub:fix/jvm-multi-module-resolution
Apr 11, 2026
Merged

fix: resolve JVM imports in multi-module Maven/Gradle projects (0 edges bug)#22
giancarloerra merged 1 commit intogiancarloerra:mainfrom
sb-giithub:fix/jvm-multi-module-resolution

Conversation

@sb-giithub
Copy link
Copy Markdown

Problem

Any Java/Kotlin/Scala project using a multi-module Maven or Gradle layout produces 0 dependency edges in codebase_graph_stats, making codebase_graph_query, codebase_graph_circular, and codebase_graph_visualize useless for the majority of real-world JVM codebases.

Tested on a 30-module Spring Boot monorepo (~29,000 Java files):

Total dependency edges: 0   ← before this fix

Root cause

resolveImport already had correct import parsing and FQN→path conversion for Java. The resolution failed silently because the fallback directory list was limited to three fixed prefixes:

src/main/java/
src/main/
src/

A multi-module project places sources at:

module-sso/module-sso-service/src/main/java/cn/sino/sso/UserService.java

None of the three prefixes match when resolved from the project root, so every cross-module import returned null.

Fix

Introduce buildJvmSuffixMap(fileSet) — a one-time O(n) scan that registers every JVM file by its class-path key (everything after src/main/<lang>/):

"cn/sino/sso/UserService.java" → "module-sso/module-sso-service/src/main/java/cn/sino/sso/UserService.java"

resolveImport accepts the map as an optional last argument and falls back to an O(1) map lookup when the existing prefix approach fails.

buildCodeGraph builds the map once per graph build, only when the project contains JVM files — zero overhead for non-JVM projects.

Changes

File Change
src/services/graph-resolution.ts Export buildJvmSuffixMap; add jvmSuffixMap? param to resolveImport; use it in Java/Kotlin/Scala case
src/services/code-graph.ts Build suffix map once before the import analysis loop; pass to resolveImport
tests/unit/graph-resolution.test.ts 7 new unit tests covering map construction, test-source exclusion, multi-module Java/Kotlin resolution, unresolvable class, stdlib guard

Compatibility

  • Fully backwards-compatible: jvmSuffixMap is optional; single-module projects continue to resolve via the existing prefix approach without reaching the suffix map.
  • Windows/POSIX safe: map keys are built and looked up using path.sep.
  • No new dependencies.

Testing

All 7 new unit tests pass. The 42 pre-existing test failures on Windows are unrelated path-separator issues in the test harness (/ vs \) that existed before this PR.


Relates to issue #21 — Java dependency graph support

🤖 Co-authored with Claude Code

Single-module resolution (src/main/java/…) already worked.
Multi-module layouts like:

  module-a/sub/src/main/java/com/example/Foo.java

were silently unresolved because the three fixed src-dir prefixes
never matched. The dependency graph therefore always produced 0
edges for any Java/Kotlin/Scala project that follows the standard
Maven/Gradle multi-module layout.

Fix: introduce `buildJvmSuffixMap()` which scans `fileSet` once
(O(n)) and registers every JVM source file by its class-path key
(i.e. everything after src/main/<lang>/). `resolveImport` accepts
the map as an optional last argument and falls back to it when the
existing prefix-based approach yields no result.

- `buildJvmSuffixMap` exported for reuse / testing.
- Map is built once per `buildCodeGraph` call, only when the
  project contains at least one JVM file — zero cost for other
  language projects.
- Lookup is O(1) per import, replacing a worst-case O(n) linear
  scan on every miss.
- Works on both Windows (backslash) and POSIX (forward-slash)
  because the map key is built with `path.sep`.
- 7 new unit tests covering: map construction, test-source
  exclusion, multi-module Java resolution, Kotlin resolution,
  unresolvable class, stdlib guard.

Fixes: enterprise Java/Spring Boot codebases (30+ Maven modules)
reporting 0 edges in codebase_graph_stats.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@giancarloerra giancarloerra self-assigned this Apr 9, 2026
@giancarloerra giancarloerra merged commit 2c2de5a into giancarloerra:main Apr 11, 2026
4 checks passed
@giancarloerra
Copy link
Copy Markdown
Owner

Thanks merged!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants