Skip to content

Commit

Permalink
feat(gazelle): add kotlin support for rules_jvm_external maven_instal…
Browse files Browse the repository at this point in the history
…l deps (#2998)

GitOrigin-RevId: ed09073be587906135e32de4089a9c75ae1d45de
  • Loading branch information
jbedard authored and alexeagle committed Aug 14, 2023
1 parent ec99c46 commit b50fa3a
Show file tree
Hide file tree
Showing 22 changed files with 587 additions and 10 deletions.
12 changes: 11 additions & 1 deletion gazelle/kotlin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_gazelle//:def.bzl", "gazelle_binary")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//gazelle:gazelle.bzl", "gazelle_generation_test")

# Disable the kotlin extension
Expand Down Expand Up @@ -31,9 +31,13 @@ go_library(
"@bazel_gazelle//resolve:go_default_library",
"@bazel_gazelle//rule:go_default_library",
"@com_github_bazel_contrib_rules_jvm//java/gazelle/javaconfig",
"@com_github_bazel_contrib_rules_jvm//java/gazelle/private/java",
"@com_github_bazel_contrib_rules_jvm//java/gazelle/private/maven",
"@com_github_bazel_contrib_rules_jvm//java/gazelle/private/types",
"@com_github_emirpasic_gods//maps/treemap",
"@com_github_emirpasic_gods//sets/treeset",
"@com_github_emirpasic_gods//utils",
"@com_github_rs_zerolog//:zerolog",
],
)

Expand All @@ -53,3 +57,9 @@ gazelle_binary(
)
for test_workspace in glob(["tests/**/WORKSPACE"])
]

go_test(
name = "kotlin_test",
srcs = ["kotlin_test.go"],
embed = [":kotlin"],
)
32 changes: 28 additions & 4 deletions gazelle/kotlin/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import (

. "aspect.build/cli/gazelle/common/log"
"aspect.build/cli/gazelle/kotlin/kotlinconfig"
"github.com/bazel-contrib/rules_jvm/java/gazelle/javaconfig"
jvm_javaconfig "github.com/bazel-contrib/rules_jvm/java/gazelle/javaconfig"
jvm_maven "github.com/bazel-contrib/rules_jvm/java/gazelle/private/maven"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/rs/zerolog"
)

type Configurer struct {
config.Configurer

lang *kotlinLang

mavenInstallFile string
}

func NewConfigurer(lang *kotlinLang) *Configurer {
Expand All @@ -24,7 +28,7 @@ func NewConfigurer(lang *kotlinLang) *Configurer {

func (kt *Configurer) KnownDirectives() []string {
return []string{
javaconfig.JavaMavenInstallFile,
jvm_javaconfig.JavaMavenInstallFile,
}
}

Expand Down Expand Up @@ -56,15 +60,35 @@ func (kt *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
for _, d := range f.Directives {
switch d.Key {

// TODO: invoke java gazelle.Configure()?
case javaconfig.JavaMavenInstallFile:
// TODO: invoke java gazelle.Configure() to support all jvm directives?
// TODO: JavaMavenRepositoryName: https://github.com/bazel-contrib/rules_jvm/commit/e46bb11bedb2ead45309eae04619caca684f6243

case jvm_javaconfig.JavaMavenInstallFile:
cfg.SetMavenInstallFile(d.Value)
}
}
}

if kt.lang.mavenResolver == nil {
BazelLog.Tracef("Creating Maven resolver: %s", cfg.MavenInstallFile())

// TODO: better zerolog configuration
logger := zerolog.New(BazelLog.Out).Level(zerolog.TraceLevel)

resolver, err := jvm_maven.NewResolver(
cfg.MavenInstallFile(),
cfg.ExcludedArtifacts(),
logger,
)
if err != nil {
BazelLog.Fatalln("error creating Maven resolver: %s", err.Error())
}
kt.lang.mavenResolver = &resolver
}
}

func (kc *Configurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {
// TODO: support rules_jvm flags such as 'java-maven-install-file'? (see rules_jvm java/gazelle/configure.go)
}

func (kc *Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error {
Expand Down
18 changes: 17 additions & 1 deletion gazelle/kotlin/kotlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@ import (

import "github.com/emirpasic/gods/sets/treeset"

import (
jvm_java "github.com/bazel-contrib/rules_jvm/java/gazelle/private/java"
jvm_types "github.com/bazel-contrib/rules_jvm/java/gazelle/private/types"
)

func IsNativeImport(impt string) bool {
return strings.HasPrefix(impt, "kotlin.") || strings.HasPrefix(impt, "kotlinx.") || strings.HasPrefix(impt, "java.") || strings.HasPrefix(impt, "javax.")
if strings.HasPrefix(impt, "kotlin.") || strings.HasPrefix(impt, "kotlinx.") {
return true
}

jvm_import := jvm_types.NewPackageName(impt)

// Java native/standard libraries
if jvm_java.IsStdlib(jvm_import) {
return true
}

return false
}

type KotlinTarget struct {
Expand Down
27 changes: 27 additions & 0 deletions gazelle/kotlin/kotlin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gazelle

import (
"testing"
)

func assertTrue(t *testing.T, b bool, msg string) {
if !b {
t.Error(msg)
}
}

func TestKotlinNative(t *testing.T) {
t.Run("kotlin native libraries", func(t *testing.T) {
assertTrue(t, IsNativeImport("kotlin.io"), "kotlin.io should be native")
assertTrue(t, IsNativeImport("kotlinx.foo"), "kotlinx.* should be native")
})

t.Run("java native libraries", func(t *testing.T) {
assertTrue(t, IsNativeImport("java.foo"), "java.* should be native")
assertTrue(t, IsNativeImport("javax.accessibility"), "javax should be native")
assertTrue(t, IsNativeImport("javax.net"), "javax should be native")
assertTrue(t, IsNativeImport("javax.sql"), "javax should be native")
assertTrue(t, IsNativeImport("javax.xml"), "javax should be native")
assertTrue(t, IsNativeImport("org.xml.sax"), "org.xml.sax should be native")
})
}
4 changes: 4 additions & 0 deletions gazelle/kotlin/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gazelle

import (
"aspect.build/cli/gazelle/common/git"
jvm_maven "github.com/bazel-contrib/rules_jvm/java/gazelle/private/maven"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/language"
Expand Down Expand Up @@ -33,6 +34,9 @@ type kotlinLang struct {

// Ignore configurations for the workspace.
gitignore *git.GitIgnore

// TODO: extend rules_jvm extension instead of duplicating?
mavenResolver *jvm_maven.Resolver
}

// NewLanguage initializes a new TypeScript that satisfies the language.Language
Expand Down
2 changes: 1 addition & 1 deletion gazelle/kotlin/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func readIdentifier(node *sitter.Node, sourceCode []byte, ignoreLast bool) strin
}
s.WriteString(nodeC.Content(sourceCode))
} else if nodeC.Type() != "comment" {
fmt.Printf("Unexpected node type: %v - %s", nodeC.Type(), nodeC.Content(sourceCode))
fmt.Printf("Unexpected node type '%v' within: %s", nodeC.Type(), node.Content(sourceCode))
os.Exit(1)
}
}
Expand Down
19 changes: 16 additions & 3 deletions gazelle/kotlin/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/bazelbuild/bazel-gazelle/resolve"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/emirpasic/gods/sets/treeset"

jvm_types "github.com/bazel-contrib/rules_jvm/java/gazelle/private/types"
)

type Resolver struct {
Expand Down Expand Up @@ -146,7 +148,7 @@ func (kt *Resolver) resolveImport(
ix *resolve.RuleIndex,
impt ImportStatement,
from label.Label,
) (ResolutionType, *label.Label, error) {
) (resType ResolutionType, resLabel *label.Label, err error) {
imptSpec := impt.ImportSpec

// Gazelle overrides
Expand Down Expand Up @@ -188,9 +190,20 @@ func (kt *Resolver) resolveImport(
return Resolution_NativeKotlin, nil, nil
}

// TODO: maven imports
jvm_import := jvm_types.NewPackageName(impt.Imp)

// Maven imports
if mavenResolver := kt.lang.mavenResolver; mavenResolver != nil {
if l, err := (*mavenResolver).Resolve(jvm_import); err == nil {
return Resolution_Label, &l, nil
}
}

if err != nil {
BazelLog.Warn("Kotlin resolution error: ", err)
}

return Resolution_NotFound, nil, nil
return Resolution_NotFound, nil, err
}

// targetListFromResults returns a string with the human-readable list of
Expand Down
12 changes: 12 additions & 0 deletions gazelle/kotlin/tests/native_deps/lib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import kotlin.text.*
// as
import kotlinx.serialization.json.Json as Jason

// Java, see rules_jvm java/gazelle/private/java/java.go
import java.util.*
import java.lang.String
import javax.net.*
import javax.xml.*
import jdk.*
import netscape.javascript.*
import org.ietf.jgss.*
import org.jcp.xml.dsig.internal.*
import org.w3c.dom.*
import org.xml.sax.*

@Serializable
data class Data(val a: Int, val b: String)

Expand Down
1 change: 1 addition & 0 deletions gazelle/kotlin/tests/rules_jvm-maven/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO(gazelle:)java_maven_repository_name vendor_java
1 change: 1 addition & 0 deletions gazelle/kotlin/tests/rules_jvm-maven/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO(gazelle:)java_maven_repository_name vendor_java
8 changes: 8 additions & 0 deletions gazelle/kotlin/tests/rules_jvm-maven/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Copied from
[rules_jvm "maven" test](https://github.com/bazel-contrib/rules_jvm/tree/v0.17.0/java/gazelle/testdata/maven).

Created by ~ following steps:

1. Copy the rules_jvm "maven" test
2. Rename .java to .kt
3. Modify .kt files to Kotlin syntax with a minimal diff
27 changes: 27 additions & 0 deletions gazelle/kotlin/tests/rules_jvm-maven/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_jvm_external",
sha256 = "23fe83890a77ac1a3ee143e2306ec12da4a845285b14ea13cb0df1b1e23658fe",
strip_prefix = "rules_jvm_external-4.3",
urls = ["https://github.com/bazelbuild/rules_jvm_external/archive/refs/tags/4.3.tar.gz"],
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"junit:junit:4.13.1",
"com.google.guava:guava:30.0-jre",
],
fetch_sources = True,
maven_install_json = "//:maven_install.json",
repositories = [
"http://uk.maven.org/maven2",
"https://jcenter.bintray.com/",
],
)

load("@maven//:defs.bzl", "pinned_maven_install")

pinned_maven_install()
Loading

0 comments on commit b50fa3a

Please sign in to comment.