Skip to content

Conversation

@fank
Copy link

@fank fank commented Oct 6, 2025

Summary

Fixes duplicate edges when using package resolution mode. Previously, the tool created one edge per import statement, resulting in multiple duplicate edges between the same package pairs.

Problem

When using --resolution package, if package A had multiple files importing package B, the graph would contain multiple identical edges from A to B (one for each import statement). For large codebases, this resulted in thousands of duplicate edges, making the graph cluttered and misleading.

For example:

  • Package internal had 67 files importing package cacheable
  • This created 67 identical edges in the output DOT file
  • Graph became unreadable with 1815 edges (should have been ~200)

Solution

Added deduplication logic in writeRelationshipsForPackageResolution():

  • Tracks unique package-to-package edges using a map
  • Each package pair now has exactly one edge
  • Prioritizes cycle edges over normal edges when both exist

Results

Testing on a large codebase:

  • Before: 1815 edges (with ~1600 duplicates)
  • After: 199 edges (all unique)
  • Improvement: ~91% reduction in duplicate edges

Testing

Tested on the internal-api codebase with 306 packages:

go run ./cmd/goimportcycle -path ../internal-api/ -dot imports.dot -resolution package

Verified:

  • ✅ No duplicate edges in output
  • ✅ All package relationships correctly represented
  • ✅ Cycle detection still works correctly
  • ✅ File resolution mode unaffected

When using package resolution, the tool was creating one edge per import
statement rather than one edge per package dependency. This resulted in
duplicate edges when multiple files in a package imported the same target
package.

For example, if package A had 67 files that imported package B, it would
create 67 identical edges from A to B in the graph.

This fix deduplicates edges at the package level by tracking unique
package-to-package relationships. It also prioritizes cycle edges over
normal edges when both exist for the same package pair.

Before:
- Large codebases could have thousands of duplicate edges
- Graph visualization was cluttered and misleading

After:
- Each package pair has exactly one edge
- Cleaner, more accurate package dependency graphs
@fank
Copy link
Author

fank commented Oct 6, 2025

-resolution package shows file links:
image

@samlitowitz
Copy link
Owner

Resolved by #14

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