Repository: bazelbuild/bazel
Issue: java_import_external rule is partly broken
Labels: team-Rules-Java
untriaged

On most recent Bazel master (d0a3c5eb67320906e4b937df5434f0e673cb6dce), combining `neverlink = True` and `generated_linkable_rule_name = "foo"` attributes seems to be broken:

```
def external_plugin_deps():
  java_import_external(
    name = "javamelody-core",
    jar_sha256 = "eb2806497676da54e257840626b64e8e53121fe2f3599457f7748e381918b2c1",
    jar_urls = [
      "https://github.com/davido/javamelody/releases/download/javamelody-core-1.73.2/javamelody-core-1.73.2.jar",
    ],
    licenses = ["notice"],
    neverlink = True,
    generated_linkable_rule_name = "linkable-javamelody-core",
  )
```

The failure is:

```
$ bazel build plugins/javamelody:javamelody
DEBUG: /home/davido/.cache/bazel/_bazel_davido/5c01f4f713b675540b8b424c5c647f63/external/bazel_skylib/lib/versions.bzl:98:7: 
Current Bazel is not a release version, cannot check for compatibility.
DEBUG: /home/davido/.cache/bazel/_bazel_davido/5c01f4f713b675540b8b424c5c647f63/external/bazel_skylib/lib/versions.bzl:99:7: Make sure that you are running at least Bazel 0.14.0.
INFO: Build options have changed, discarding analysis cache.
ERROR: /home/davido/projects/gerrit2/plugins/javamelody/BUILD:10:1: no such package '@javamelody-core//': Traceback (most recent call last):
	File "/home/davido/.cache/bazel/_bazel_davido/5c01f4f713b675540b8b424c5c647f63/external/bazel_tools/tools/build_defs/repo/jvm.bzl", line 92
		lines.extend(_serialize_given_rule_import(nam...))
	File "/home/davido/.cache/bazel/_bazel_davido/5c01f4f713b675540b8b424c5c647f63/external/bazel_tools/tools/build_defs/repo/jvm.bzl", line 92, in lines.extend
		_serialize_given_rule_import(name = repository_ctx.attr.gener..., <7 more arguments>)
unexpected keyword 'additonal_rule_attrs' in call to _serialize_given_rule_import(rule_name, import_attr, name, path, srcpath, attrs, props, additional_rule_attrs) and referenced by '//plugins/javamelody:javamelody__plugin'
ERROR: Analysis of target '//plugins/javamelody:javamelody' failed; build aborted: no such package '@javamelody-core//': Traceback (most recent call last):
	File "/home/davido/.cache/bazel/_bazel_davido/5c01f4f713b675540b8b424c5c647f63/external/bazel_tools/tools/build_defs/repo/jvm.bzl", line 92
		lines.extend(_serialize_given_rule_import(nam...))
	File "/home/davido/.cache/bazel/_bazel_davido/5c01f4f713b675540b8b424c5c647f63/external/bazel_tools/tools/build_defs/repo/jvm.bzl", line 92, in lines.extend
		_serialize_given_rule_import(name = repository_ctx.attr.gener..., <7 more arguments>)
unexpected keyword 'additonal_rule_attrs' in call to _serialize_given_rule_import(rule_name, import_attr, name, path, srcpath, attrs, props, additional_rule_attrs)
INFO: Elapsed time: 0.379s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (16 packages loaded)
```

Workaround is to use `extra_build_file_content` attribute instead:

```
java_import_external(
    name = "javamelody-core",
    jar_sha256 = "eb2806497676da54e257840626b64e8e53121fe2f3599457f7748e381918b2c1",
    jar_urls = [
      "https://github.com/davido/javamelody/releases/download/javamelody-core-1.73.2/javamelody-core-1.73.2.jar",
    ],
    licenses = ["notice"],
    neverlink = True,
    # This should just work
    #generated_linkable_rule_name = "linkable-javamelody-core",
    extra_build_file_content = "\n".join([
          "java_import(",
          "    name = \"linkable-javamelody-core\",",
          "    jars = [\"javamelody-core-1.73.2.jar\"],",
          ")",
    ]),
  )
```

Related discussion on the mailing list: [1].

[1] https://groups.google.com/d/topic/bazel-discuss/PvssQ709iGw/discussion

Issue comments:

Pull request review comments:

Patch that resolved the issue:
diff --git a/tools/build_defs/repo/jvm.bzl b/tools/build_defs/repo/jvm.bzl
index 1538ea1b7e892d..dfa17f4692b176 100644
--- a/tools/build_defs/repo/jvm.bzl
+++ b/tools/build_defs/repo/jvm.bzl
@@ -91,7 +91,7 @@ def _jvm_import_external(repository_ctx):
         repository_ctx.attr.generated_linkable_rule_name):
         lines.extend(_serialize_given_rule_import(
             name = repository_ctx.attr.generated_linkable_rule_name,
-            additonal_rule_attrs = repository_ctx.attr.additional_rule_attrs,
+            additional_rule_attrs = repository_ctx.attr.additional_rule_attrs,
             attrs = repository_ctx.attr,
             import_attr = repository_ctx.attr.rule_metadata["import_attr"],
             path = path,


Test patch: