google / j2cl

Java to Closure JavaScript transpiler
Apache License 2.0
1.22k stars 143 forks source link

Can't run j2cl on Bazel 6 #200

Open sgammon opened 1 year ago

sgammon commented 1 year ago

Describe the bug I'm trying to run some J2CL builds on a Bazel 6 project, but I encounter the following error:

INFO: Invocation ID: a1fff5c4-4b69-439d-a75f-e730e8c490f7
ERROR: /private/var/tmp/_bazel_sam/89d3dd8c1880937e8b55a8dc2c815fe0/external/bazel_tools/tools/jdk/BUILD:124:6: no such target '@rules_java~6.2.2~toolchains~local_jdk//:jar': target 'jar' not declared in package '' defined by /private/var/tmp/_bazel_sam/89d3dd8c1880937e8b55a8dc2c815fe0/external/rules_java~6.2.2~toolchains~local_jdk/BUILD.bazel (Tip: use `query "@@rules_java~6.2.2~toolchains~local_jdk//:*"` to see all the targets in that package) and referenced by '@bazel_tools//tools/jdk:jar'

I'm using bzlmod with rules_java, which seems to be doing something creative with local_jdk. Is there any way I can fix this?

To Reproduce It's a brand new Bazel 6 project with J2CL added via the standard directions. I can provide a reproducer if needed.

Bazel version Please include version of Bazel that you are running J2CL with:

Aspect CLI version: 5.5.4
Build label: 6.2.1
Build target: bazel-out/darwin_arm64-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri Jun 2 17:00:10 2023 (1685725210)
Build timestamp: 1685725210
Build timestamp as int: 1685725210

Expected behavior It should be able to run the build under Bazel 6, ideally.

sgammon commented 1 year ago

I wonder if the J2CL command line transpiler might be an option instead? Is WASM supported through that? I was able to get it to build so long as Java was set to 11. We need 17 for some other targets, although we aren't using any syntax from 17 that makes it into the J2CL target.

gkdn commented 1 year ago

re. Bazel 6 support: Thanks for reporting. We haven't looked into running with Bazel 6. We should address this but we are quite busy with other stuff at the moment so it may take some time.

re. command line tool for Wasm: We should support wasm on the command line tool after we stabilize the transpiler. Currently for Wasm we are doing a monolithic compile (compile all sources in one shot) and we are in the process of rebuilding it to be "modular" similar to J2CL/JS or javac. However being said that with modular compilation the pipeline will be more complicated to use from the command line (again similar to J2CL/JS).

treblereel commented 1 year ago

@gkdn modularization sounds nice, looks like it allows us to perform incremental recompilation in maven multi-module project with the j2cl-maven-plugin. Cool

sgammon commented 1 year ago

@gkdn we're in bazel downstream so cli access is only a way to get access to J2CL before bazel 6 support. thank you for this response, we'll keep an eye out.

gkdn commented 1 year ago

@sgammon I think command line runner has all the flags available to run. The contract will change after the modularization but you can probably make it work for today.

aschleck commented 10 months ago

@sgammon not sure what the exact difference is but j2cl works for me with Bazel 6. I don't want to spend the time to minify my project but my guess is maybe something to do with using the Nix JDK instead of the default JDK? https://github.com/aschleck/trailcatalog/blob/main/WORKSPACE.bzlmod#L23 .

In that file you'll see j2cl is pulled from ../j2cl, that's a local clone based on commit 39754f53dc1f4b4fd869e769b5486182c3e43dda with the following patch applied to pull rules_kotlin forward.

diff --git a/build_defs/repository.bzl b/build_defs/repository.bzl
index 12790b1b6..85dc8de3d 100644
--- a/build_defs/repository.bzl
+++ b/build_defs/repository.bzl
@@ -36,11 +36,12 @@ def load_j2cl_repo_deps():
         sha256 = "64ad2728ccdd2044216e4cec7815918b7bb3bb28c95b7e9d951f9d4eccb07625",
     )

-    _github_repo(
+    http_archive(
         name = "io_bazel_rules_kotlin",
-        repo = "bazelbuild/rules_kotlin",
-        tag = "legacy-1.3.0-rc3",
-        sha256 = "54678552125753d9fc0a37736d140f1d2e69778d3e52cf454df41a913b964ede",
+        sha256 = "fd92a98bd8a8f0e1cdcb490b93f5acef1f1727ed992571232d33de42395ca9b3",
+        urls = [
+            "https://github.com/bazelbuild/rules_kotlin/releases/download/v1.7.1/rules_kotlin_release.tgz",
+        ],
     )

 def _github_repo(name, repo, tag, sha256 = None):
diff --git a/build_defs/workspace.bzl b/build_defs/workspace.bzl
index 64f855d48..72ab780d9 100644
--- a/build_defs/workspace.bzl
+++ b/build_defs/workspace.bzl
@@ -4,7 +4,8 @@ load("@bazel_skylib//lib:versions.bzl", "versions")
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_jar")
 load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external")
 load("@io_bazel_rules_closure//closure:repositories.bzl", "rules_closure_dependencies")
-load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")
+load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
+load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")

 _MAVEN_CENTRAL_URLS = ["https://repo1.maven.org/maven2/"]

@@ -196,14 +197,6 @@ def setup_j2cl_workspace(**kwargs):
         sha256 = "83ce07ec2058d8d629feb4e269216e286560b0e4587dea883f4e16b64ea51cad",
     )

-    kotlin_repositories(
-        compiler_release = {
-            "urls": [
-                "https://github.com/JetBrains/kotlin/releases/download/v1.6.10/kotlin-compiler-1.6.10.zip",
-            ],
-            "sha256": "432267996d0d6b4b17ca8de0f878e44d4a099b7e9f1587a98edc4d27e76c215a",
-        },
-    )
     kt_register_toolchains()

     # Required by protobuf_java_util