Repository: bazelbuild/buildtools
Pull request #671 (2019-07-17 13:09:14)

Add native-java fix to buildifier.

iirina: FYI @oquenchil 

Patch:
diff --git a/WARNINGS.md b/WARNINGS.md
index fe370ac..b1cbc4b 100644
--- a/WARNINGS.md
+++ b/WARNINGS.md
@@ -30,6 +30,7 @@ Warning categories supported by buildifier's linter:
   * [name-conventions](#name-conventions)
   * [native-android](#native-android)
   * [native-build](#native-build)
+  * [native-java](#native-java)
   * [native-package](#native-package)
   * [no-effect](#no-effect)
   * [out-of-order-load](#out-of-order-load)
@@ -478,6 +479,17 @@ there.
 
 --------------------------------------------------------------------------------
 
+## <a name="native-java"></a>All Java build rules should be loaded from Starlark
+
+  * Category name: `native-java`
+  * Flag in Bazel: [`--incompatible_load_java_rules_from_bzl`](https://github.com/bazelbuild/bazel/issues/8746)
+  * Automatic fix: yes
+
+The Java build rules should be loaded from Starlark. The native rules [will be
+disabled](https://github.com/bazelbuild/bazel/issues/8746).
+
+--------------------------------------------------------------------------------
+
 ## <a name="native-package"></a>`native.package()` shouldn't be used in .bzl files
 
   * Category name: `native-package`
diff --git a/tables/tables.go b/tables/tables.go
index a4ca123..6b47ad3 100644
--- a/tables/tables.go
+++ b/tables/tables.go
@@ -223,6 +223,23 @@ var AndroidNativeRules = []string{
 // AndroidLoadPath is the load path for the Starlark Android Rules.
 var AndroidLoadPath = "@rules_android//android:rules.bzl"
 
+// JavaNativeRules lists all Java rules that are being migrated from Native to Starlark.
+var JavaNativeRules = []string{
+	"java_binary",
+	"java_import",
+	"java_library",
+	"java_lite_proto_library",
+	"java_proto_library",
+	"java_test",
+	"java_package_configuration",
+	"java_plugin",
+	"java_runtime",
+	"java_toolchain",
+}
+
+// JavaLoadPath is the load path for the Starlark Java Rules.
+var JavaLoadPath = "@rules_java//java:defs.bzl"
+
 // OverrideTables allows a user of the build package to override the special-case rules. The user-provided tables replace the built-in tables.
 func OverrideTables(labelArg, blacklist, listArg, sortableListArg, sortBlacklist, sortWhitelist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool) {
 	IsLabelArg = labelArg
diff --git a/warn/warn.go b/warn/warn.go
index cf17cc5..fdc0137 100644
--- a/warn/warn.go
+++ b/warn/warn.go
@@ -128,6 +128,7 @@ var FileWarningMap = map[string]func(f *build.File) []*LinterFinding{
 	"name-conventions":          nameConventionsWarning,
 	"native-android":            nativeAndroidRulesWarning,
 	"native-build":              nativeInBuildFilesWarning,
+	"native-java":               nativeJavaRulesWarning,
 	"native-package":            nativePackageWarning,
 	"no-effect":                 noEffectWarning,
 	"output-group":              outputGroupWarning,
diff --git a/warn/warn_bazel_api.go b/warn/warn_bazel_api.go
index e8f4cc9..3c4533d 100644
--- a/warn/warn_bazel_api.go
+++ b/warn/warn_bazel_api.go
@@ -578,6 +578,13 @@ func nativeAndroidRulesWarning(f *build.File) []*LinterFinding {
 	return notLoadedFunctionUsageCheck(f, tables.AndroidNativeRules, tables.AndroidLoadPath)
 }
 
+func nativeJavaRulesWarning(f *build.File) []*LinterFinding {
+	if f.Type != build.TypeBzl && f.Type != build.TypeBuild {
+		return nil
+	}
+	return notLoadedFunctionUsageCheck(f, tables.JavaNativeRules, tables.JavaLoadPath)
+}
+
 func contextArgsAPIWarning(f *build.File) []*LinterFinding {
 	if f.Type != build.TypeBzl {
 		return nil


Test patch:
diff --git a/warn/warn_bazel_api_test.go b/warn/warn_bazel_api_test.go
index bdd13a8..27088f3 100644
--- a/warn/warn_bazel_api_test.go
+++ b/warn/warn_bazel_api_test.go
@@ -493,3 +493,37 @@ android_binary()
 		},
 		scopeBzl|scopeBuild)
 }
+
+func TestNativeJavaWarning(t *testing.T) {
+	checkFindingsAndFix(t, "native-java", `
+"""My file"""
+
+def macro():
+    java_import()
+    java_library()
+    native.java_library()
+    native.java_binary()
+
+java_test()
+`, fmt.Sprintf(`
+"""My file"""
+
+load(%q, "java_binary", "java_import", "java_library", "java_test")
+
+def macro():
+    java_import()
+    java_library()
+    java_library()
+    java_binary()
+
+java_test()
+`, tables.JavaLoadPath),
+		[]string{
+			fmt.Sprintf(`:4: Function "java_import" is not global anymore and needs to be loaded from "%s".`, tables.JavaLoadPath),
+			fmt.Sprintf(`:5: Function "java_library" is not global anymore and needs to be loaded from "%s".`, tables.JavaLoadPath),
+			fmt.Sprintf(`:6: Function "java_library" is not global anymore and needs to be loaded from "%s".`, tables.JavaLoadPath),
+			fmt.Sprintf(`:7: Function "java_binary" is not global anymore and needs to be loaded from "%s".`, tables.JavaLoadPath),
+			fmt.Sprintf(`:9: Function "java_test" is not global anymore and needs to be loaded from "%s".`, tables.JavaLoadPath),
+		},
+		scopeBzl|scopeBuild)
+}