diff --git a/java/gazelle/generate.go b/java/gazelle/generate.go index dcfc8a5f..bb89b705 100644 --- a/java/gazelle/generate.go +++ b/java/gazelle/generate.go @@ -7,6 +7,7 @@ import ( "path/filepath" "sort" "strings" + "unicode" "github.com/bazel-contrib/rules_jvm/java/gazelle/javaconfig" "github.com/bazel-contrib/rules_jvm/java/gazelle/private/java" @@ -234,7 +235,14 @@ func (l javaLang) generateModuleRoot(args language.GenerateArgs, cfg *javaconfig filteredImports := filterImports(allImports, func(i string) bool { for _, n := range allPackageNames { if strings.HasPrefix(i, n) { - return false + // Assume the standard java convention of class names starting with upper case + // and package components starting with lower case. + // Without this check, one module with dependencies on a subpackage which _isn't_ + // in the module won't be detected. + suffixRunes := []rune(i[len(n):]) + if len(suffixRunes) >= 2 && suffixRunes[0] == '.' && unicode.IsUpper(suffixRunes[1]) { + return false + } } } return true diff --git a/java/gazelle/testdata/module-granularity/module1/BUILD.want b/java/gazelle/testdata/module-granularity/module1/BUILD.want index 4db147c1..ed1079e2 100644 --- a/java/gazelle/testdata/module-granularity/module1/BUILD.want +++ b/java/gazelle/testdata/module-granularity/module1/BUILD.want @@ -7,7 +7,10 @@ java_library( "src/main/java/com/example/hello/Hello.java", "src/main/java/com/example/hello/world/World.java", ], - _gazelle_imports = ["java.lang.String"], + _gazelle_imports = [ + "com.example.hello.notworld.NotWorld", + "java.lang.String", + ], _java_packages = [ "com.example.hello", "com.example.hello.world", diff --git a/java/gazelle/testdata/module-granularity/module1/src/main/java/com/example/hello/Hello.java b/java/gazelle/testdata/module-granularity/module1/src/main/java/com/example/hello/Hello.java index b60a197a..f4bdd886 100644 --- a/java/gazelle/testdata/module-granularity/module1/src/main/java/com/example/hello/Hello.java +++ b/java/gazelle/testdata/module-granularity/module1/src/main/java/com/example/hello/Hello.java @@ -1,10 +1,12 @@ package com.example.hello; import com.example.hello.world.World; +import com.example.hello.notworld.NotWorld; public class Hello { public static void main(String[] args) { World one = new World(); System.out.printf("Hello, %s!", one.name); + System.out.printf("Hello also, %s!", NotWorld.NOT_WORLD); } } diff --git a/java/gazelle/testdata/module-granularity/notmodule/src/main/java/com/example/hello/notworld/NotWorld.java b/java/gazelle/testdata/module-granularity/notmodule/src/main/java/com/example/hello/notworld/NotWorld.java new file mode 100644 index 00000000..c37ab2f7 --- /dev/null +++ b/java/gazelle/testdata/module-granularity/notmodule/src/main/java/com/example/hello/notworld/NotWorld.java @@ -0,0 +1,5 @@ +package com.example.hello.notworld; + +public class NotWorld { + public static final String NOT_WORLD = "Not World!"; +}