Skip to content

Commit 4f6a8fd

Browse files
committed
[gazelle] Use the index file generated by rules_jvm_external to better find third party deps
1 parent 071a9de commit 4f6a8fd

File tree

11 files changed

+115
-2
lines changed

11 files changed

+115
-2
lines changed

java/gazelle/configure.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Configurer struct {
2525
annotationToAttribute annotationToAttribute
2626
annotationToWrapper annotationToWrapper
2727
mavenInstallFile string
28+
javaIndexFile string
2829
}
2930

3031
func NewConfigurer(lang *javaLang) *Configurer {
@@ -39,6 +40,7 @@ func (jc *Configurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Conf
3940
fs.Var(&jc.annotationToAttribute, "java-annotation-to-attribute", "Mapping of annotations (on test classes) to attributes which should be set for that test rule. Examples: com.example.annotations.FlakyTest=flaky=True com.example.annotations.SlowTest=timeout=\"long\"")
4041
fs.Var(&jc.annotationToWrapper, "java-annotation-to-wrapper", "Mapping of annotations (on test classes) to wrapper rules which should be used around the test rule. Example: com.example.annotations.RequiresNetwork=@some//wrapper:file.bzl=requires_network")
4142
fs.StringVar(&jc.mavenInstallFile, "java-maven-install-file", "", "Path of the maven_install.json file. Defaults to \"maven_install.json\".")
43+
fs.StringVar(&jc.javaIndexFile, "java-index-file", "", "Path of the maven_index.json file. Defaults to \"maven_index.json\".")
4244
}
4345

4446
func (jc *Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error {
@@ -54,6 +56,9 @@ func (jc *Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error {
5456
if jc.mavenInstallFile != "" {
5557
cfgs[""].SetMavenInstallFile(jc.mavenInstallFile)
5658
}
59+
if jc.javaIndexFile != "" {
60+
cfgs[""].SetMavenIndexFile(jc.javaIndexFile)
61+
}
5762
return nil
5863
}
5964

@@ -62,6 +67,7 @@ func (jc *Configurer) KnownDirectives() []string {
6267
javaconfig.JavaExcludeArtifact,
6368
javaconfig.JavaExtensionDirective,
6469
javaconfig.JavaMavenInstallFile,
70+
javaconfig.JavaIndexFile,
6571
javaconfig.JavaModuleGranularityDirective,
6672
javaconfig.JavaTestFileSuffixes,
6773
javaconfig.JavaTestMode,
@@ -161,6 +167,9 @@ func (jc *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
161167
case javaconfig.JavaMavenInstallFile:
162168
cfg.SetMavenInstallFile(d.Value)
163169

170+
case javaconfig.JavaIndexFile:
171+
cfg.SetMavenIndexFile(d.Value)
172+
164173
case javaconfig.JavaModuleGranularityDirective:
165174
if err := cfg.SetModuleGranularity(d.Value); err != nil {
166175
jc.lang.logger.Fatal().Err(err).Msgf("invalid value for directive %q", javaconfig.JavaModuleGranularityDirective)
@@ -260,6 +269,7 @@ func (jc *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
260269
if jc.lang.mavenResolver == nil {
261270
resolver, err := maven.NewResolver(
262271
cfg.MavenInstallFile(),
272+
cfg.MavenIndexFile(),
263273
jc.lang.logger,
264274
)
265275
if err != nil {

java/gazelle/javaconfig/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ const (
2626
// Defaults to "maven_install.json".
2727
JavaMavenInstallFile = "java_maven_install_file"
2828

29+
// JavaIndexFile represents the directive that controls where the index
30+
// file generated by `rules_jvm_external` is located.
31+
// Defaults to "maven_index.json"
32+
JavaIndexFile = "java_index_file"
33+
2934
// JavaModuleGranularityDirective represents the directive that controls whether
3035
// this Java module has a module granularity (Gradle) or a package
3136
// granularity (bazel).
@@ -105,6 +110,7 @@ func (c *Config) NewChild() *Config {
105110
resolveToJavaExports: c.resolveToJavaExports,
106111
kotlinEnabled: c.kotlinEnabled,
107112
mavenInstallFile: c.mavenInstallFile,
113+
javaIndexFile: c.javaIndexFile,
108114
moduleGranularity: c.moduleGranularity,
109115
repoRoot: c.repoRoot,
110116
testMode: c.testMode,
@@ -138,6 +144,7 @@ type Config struct {
138144
resolveToJavaExports *types.LateInit[bool]
139145
kotlinEnabled bool
140146
mavenInstallFile string
147+
javaIndexFile string
141148
moduleGranularity string
142149
repoRoot string
143150
testMode string
@@ -166,6 +173,7 @@ func New(repoRoot string) *Config {
166173
resolveToJavaExports: types.NewLateInit[bool](true),
167174
kotlinEnabled: true,
168175
mavenInstallFile: "maven_install.json",
176+
javaIndexFile: "maven_index.json",
169177
moduleGranularity: "package",
170178
repoRoot: repoRoot,
171179
testMode: "suite",
@@ -234,6 +242,14 @@ func (c *Config) SetMavenInstallFile(filename string) {
234242
c.mavenInstallFile = filename
235243
}
236244

245+
func (c Config) MavenIndexFile() string {
246+
return filepath.Join(c.repoRoot, c.javaIndexFile)
247+
}
248+
249+
func (c *Config) SetMavenIndexFile(filename string) {
250+
c.javaIndexFile = filename
251+
}
252+
237253
func (c Config) ModuleGranularity() string {
238254
return c.moduleGranularity
239255
}

java/gazelle/private/maven/config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ type lockFileV2_Artifact struct {
103103
Version string `json:"version"`
104104
}
105105

106+
type IndexFile struct {
107+
Version int `json:"version"`
108+
Classes map[string]map[string][]string `json:"classes"`
109+
}
110+
111+
func loadIndex(filename string) (*IndexFile, error) {
112+
data, err := os.ReadFile(filename)
113+
if err != nil {
114+
return nil, err
115+
}
116+
var index IndexFile
117+
if err := json.Unmarshal(data, &index); err != nil {
118+
return nil, err
119+
}
120+
return &index, nil
121+
}
122+
106123
func loadConfiguration(filename string) (lockFile, error) {
107124
data, err := os.ReadFile(filename)
108125
if err != nil {

java/gazelle/private/maven/resolver.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package maven
22

33
import (
44
"fmt"
5+
"os"
56
"sort"
67

78
"github.com/bazel-contrib/rules_jvm/java/gazelle/private/bazel"
@@ -43,7 +44,7 @@ type resolver struct {
4344
logger zerolog.Logger
4445
}
4546

46-
func NewResolver(installFile string, logger zerolog.Logger) (Resolver, error) {
47+
func NewResolver(installFile string, indexFile string, logger zerolog.Logger) (Resolver, error) {
4748
r := resolver{
4849
data: multiset.NewStringMultiSet(),
4950
classIndex: make(map[string]string),
@@ -56,6 +57,17 @@ func NewResolver(installFile string, logger zerolog.Logger) (Resolver, error) {
5657
return &r, nil
5758
}
5859

60+
var index *IndexFile
61+
if indexFile != "" {
62+
var err error
63+
index, err = loadIndex(indexFile)
64+
if err != nil {
65+
if !os.IsNotExist(err) {
66+
r.logger.Warn().Err(err).Msg("failed to load index file")
67+
}
68+
}
69+
}
70+
5971
dependencies := c.ListDependencies()
6072

6173
r.logger.Debug().Int("count", len(dependencies)).Msg("Dependency count")
@@ -71,6 +83,19 @@ func NewResolver(installFile string, logger zerolog.Logger) (Resolver, error) {
7183
for _, class := range c.ListDependencyClasses(depName) {
7284
r.classIndex[class] = coords.ArtifactString()
7385
}
86+
if index != nil {
87+
if pkgMap, ok := index.Classes[depName]; ok {
88+
for pkg, classes := range pkgMap {
89+
for _, cls := range classes {
90+
fqcn := cls
91+
if pkg != "" {
92+
fqcn = pkg + "." + cls
93+
}
94+
r.classIndex[fqcn] = coords.ArtifactString()
95+
}
96+
}
97+
}
98+
}
7499
}
75100

76101
return &r, nil

java/gazelle/private/maven/resolver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestResolver(t *testing.T) {
1818

1919
m := make(map[string]struct{})
2020
m["@maven//:com_google_j2objc_j2objc_annotations"] = struct{}{}
21-
r, err := NewResolver("testdata/guava_maven_install.json", logger)
21+
r, err := NewResolver("testdata/guava_maven_install.json", "", logger)
2222
if err != nil {
2323
t.Fatal(err)
2424
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": 2,
3+
"classes": {
4+
"com.example:lib-a": {
5+
"com.example.split": [
6+
"ClassA"
7+
]
8+
},
9+
"com.example:lib-b": {
10+
"com.example.split": [
11+
"ClassB"
12+
]
13+
}
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "2",
3+
"artifacts": {
4+
"com.example:lib-a": {
5+
"version": "1.0"
6+
},
7+
"com.example:lib-b": {
8+
"version": "1.0"
9+
}
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_java//java:defs.bzl", "java_library")
2+
3+
java_library(
4+
name = "app",
5+
srcs = ["Main.java"],
6+
visibility = ["//:__subpackages__"],
7+
deps = [
8+
"@maven//:com_example_lib_a",
9+
"@maven//:com_example_lib_b",
10+
],
11+
)

0 commit comments

Comments
 (0)