Skip to content

[gazelle] Support lazy indexing in the java gazelle plugin#397

Open
shs96c wants to merge 5 commits into
bazel-contrib:mainfrom
shs96c:gazelle-lazy-index
Open

[gazelle] Support lazy indexing in the java gazelle plugin#397
shs96c wants to merge 5 commits into
bazel-contrib:mainfrom
shs96c:gazelle-lazy-index

Conversation

@shs96c

@shs96c shs96c commented Dec 5, 2025

Copy link
Copy Markdown
Collaborator

No description provided.

@jeffmace

Copy link
Copy Markdown
Contributor

I took a look at this today and hit issues in our repository which uses gazelle:java_module_granularity module. The current lazy-indexing logic will only index directories that include BUILD.bazel files. This condition fails in our repository so the additional paths are not indexed.

https://github.com/bazel-contrib/bazel-gazelle/blob/master/cmd/gazelle/fix-update.go#L338

Am I understanding the logic correctly? Do you think there is a way to handle this in the extension or do we need to suggest a reorganization of the upstream logic?

@jeffmace

Copy link
Copy Markdown
Contributor

The disconnect appears to be coming from the Resolver.Imports function. It is reading the cached package information from the rule. But that package information is populated by GenerateRules which hasn't been called.

https://github.com/shs96c/rules_jvm/blob/gazelle-lazy-index/java/gazelle/resolve.go#L61

If my understanding is right; I'm unsure how to proceed:

  • Update Imports to return a set of ImportSpec based on the pathnames for the files in the Rule if it matches the Maven layout
  • Breakout the java parsing logic from GenerateRules so it can be called from Imports if the package data isn't available
  • Work with the Gazelle maintainers to reorder the logic so GenerateRules is called before Imports

@shs96c shs96c force-pushed the gazelle-lazy-index branch from 8feac36 to 5e7ec8c Compare January 14, 2026 17:06
When using `gazelle:java_module_granularity module` with lazy indexing,
`Imports()` now correctly returns package information even when
`GenerateRules()` hasn't been called.

The implementation uses a two-phase approach:
1. Infer package from file path using configured search paths (fast)
2. Verify by quick-scanning the file for the actual package declaration

This respects all configured source roots including custom ones like
`src/integrationTest/java` and package prefixes from `java_search`.

Fixes the issue reported by @jeffmace where lazy indexing failed for
repositories using module granularity because `Resolver.Imports` was
reading cached package info that hadn't been populated.
@shs96c shs96c force-pushed the gazelle-lazy-index branch from 5e7ec8c to 8d0507b Compare January 20, 2026 13:42
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.

3 participants