Skip to content

fix(resolution): bind a duplicated type to the caller's own copy, and report duplicates (#269) - #279

Open
sinha108 wants to merge 4 commits into
mainfrom
fix/269-caller-local-resolution
Open

sinha108 wants to merge 4 commits into
mainfrom
fix/269-caller-local-resolution

Conversation

@sinha108

@sinha108 sinha108 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #269.

When one analysis covers several source roots that each declare the same qualified type name (vendored copies of a shared library across services), every caller bound to one arbitrary copy. Nothing reported it. This PR does both of the issue's fixes.

1. Caller-local resolution

  • L1: each source root parses with its own resolution order: the JDK, then its own root, then the other roots nearest it first (the most leading path segments shared with its own root, ties in path order), then dependency jars. That matches L2's rule below, so the two levels pick the same copy when the caller's own root has none. The per-root solvers are built once and shared across orders, behind thin wrappers, since a TypeSolver takes a single parent. That keeps their parse caches from multiplying.
  • Root order: source roots are now sorted on their /-separated relative path, the same on every OS. JavaParser's ProjectRoot lists them in hash order of their absolute paths, so on main the copy that won changed with the checkout location.
  • L2: the binary-name index keeps every copy of a name. A call binds to the copy nearest the calling callable: the one sharing the most leading id segments, so its own source root first. If the nearest copy doesn't declare the called signature, the next copy that does wins. Ties go by id order, so the choice is deterministic.
  • RTA: an edge's source has no caller to be near (WALA loads one class per name), so it takes the first copy in id order that declares the signature, and its destination is chosen near that source.
  • Neo4j projector: extends, implements and import edges resolve the same way, through the shared schema.FqnIndex.

2. Detection
After L1, if any top-level qualified name is declared in more than one file, one warning goes to stderr on every run, with or without -v. It gives the count and one example with its files. This is not a degradation: the output keeps every copy, so --strict is unaffected. A consumer that keys types by qualified name can find the duplicates in the symbol table itself; python-sdk does so in codellm-devkit/python-sdk#451.

Known limitation
Only the caller's reference picks its own copy. A found copy's own declarations (its supertypes, its methods' return and parameter types) resolve in the application-wide order, so a chained call such as copy.helper().tag() can reach another root's Helper. Lifting that needs a parse cache per source root, which multiplies memory on large monorepos. A test pins the current behavior.

Verification

  • DuplicateTypeResolutionTest:
    • Duplicate FQNs across source roots bind to the last root, silently #269's reproduction: three services, each vendoring com.shared.RunStatsDataBean. CallerA binds to svc-a's copy and CallerB to svc-b's.
    • Copies with diverged signatures, so a call resolves only through the caller's own root. This fails if every root uses one root's order, which the first test alone does not catch.
    • The same application checked out at eight locations, with two copies equally near the caller: the fallback is always the first of them in path order. This fails on main's root order.
    • No copy in the caller's root: both levels bind to the nearest copy (a sibling of the caller's service before a shared lib), so a chained call does not reach one copy at L2 and another at L1.
    • The pinned limitation above.
    • The warning appears on stderr without -v, --strict passes, and an application without duplicates prints nothing.
  • FqnIndexTest covers nearest-copy selection, ties, lookups with no referrer, and read-only results.
  • DuplicateTypeProjectionTest: extends and import edges in Neo4j reach the referring service's own copy.
  • ./gradlew test: 664 tests, 0 failures.
  • On daytrader8, which has no duplicates, analysis.json at -a 1 is byte-identical to main's.
  • On StreamPark (four duplicated names, across its Flink shim modules), -a 1 --strict at the head commit exits 0 with the warning, in 17.5 s at a 1.5 GB peak.
  • On keycloak/keycloak (6,504 main Java files), -a 1 with per-root orders ran in 420 s at a 11.1 GB peak, measured on this PR's first revision. That run included fix(output): stream analysis.json and stdout instead of building one String (#277) #278's streaming fix, without which the output limit fails first. The later revisions change only the order of roots, which costs a sort of the roots per root.

With #278 (#277): the two touch separate parts of CodeAnalyzer.java and separate test classes, so they merge cleanly in either order.

@sinha108

sinha108 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

Two more checks, added after opening (rerun at 2d7bead):

The Neo4j projector. DuplicateTypeProjectionTest projects two services, each declaring com.shared.Base with a subclass and an importing class. Both J_EXTENDS and J_IMPORTS now reach the referring service's own Base. Run against main's projector, both assertions fail: svc-a's edges reach svc-b's copy.

Level 3 with RTA. I made the issue's reproduction a buildable Maven aggregator (svc-a, svc-b and svc-c, with a main in each caller so RTA has entry points) and ran -a 3:

3.4.0
   svc-a:CallerA.go()  -> svc-c:RunStatsDataBean.origin()   declared+rta
   svc-b:CallerB.go()  -> svc-c:RunStatsDataBean.origin()   declared+rta
this PR
   svc-a:CallerA.go()  -> svc-a:RunStatsDataBean.origin()   declared+rta
   svc-b:CallerB.go()  -> svc-b:RunStatsDataBean.origin()   declared+rta

The constructor edges move the same way. Declared and RTA edges agree in both builds, and an RTA destination now resolves near its source. The run prints the duplicate warning on stderr and exits 0.

Unrelated to this PR: on a GraalVM Enterprise 21 runtime, the RTA overlay fails with ClassHierarchyException: factory.getLoader failed, with or without duplicates. On Temurin 21 (the jdk4py runtime) it works.

@sinha108
sinha108 marked this pull request as draft November 23, 2026 14:37
…g --strict (#269)

- Source roots are sorted by path. JavaParser lists them in hash order of
  their absolute paths, so the fallback copy changed with the checkout
  location.
- Duplicate names are warned about on stderr on every run, and are no
  longer a degradation: the output keeps every copy, so --strict passes.
- Tests: copies with diverged signatures (fail if L1 resolves through
  root 0's order), the checkout-location case, and a pinned limitation:
  a found copy's own references resolve in the application-wide order.
- FqnIndex.ordered returns a read-only list.
With no copy in the caller's own root, L1 fell back in path order while L2
chose the copy nearest the caller, so their edges could contradict each
other. L1 now ranks the other roots by leading path segments shared with
the caller's root, then path order. Roots are sorted on the /-separated
relative path, the same on every OS.
@sinha108
sinha108 marked this pull request as ready for review November 23, 2026 15:35
@sinha108
sinha108 requested a review from rahlk November 23, 2026 15:44

This branch has not been deployed

No deployments
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.

Duplicate FQNs across source roots bind to the last root, silently

1 participant