Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Bazel's own build to use Java 11 language features #14592

Closed
cushon opened this issue Jan 17, 2022 · 4 comments
Closed

Allow Bazel's own build to use Java 11 language features #14592

cushon opened this issue Jan 17, 2022 · 4 comments

Comments

@cushon
Copy link
Contributor

cushon commented Jan 17, 2022

Bazel's build now requires a minimum of JDK 11, but it's still configured to use Java 8 language features.

We should enable Java 11 language features for Bazel.

e.g.

 git diff
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java b/src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java
index 8d5fd508f8..e760bb31d0 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java
@@ -187,7 +187,7 @@ public class DigestHashFunction {

   private static boolean supportsClone(MessageDigest toCheck) {
     try {
-      toCheck.clone();
+      var unused = toCheck.clone();
       return true;
     } catch (CloneNotSupportedException e) {
       return false;
$ bazel build //src:bazel
...
src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java:190: warning: as of release 10, 'var' is a restricted local variable type and cannot be used for type declarations or as the element type of an array
      var unused = toCheck.clone();
          ^
@linzhp
Copy link
Contributor

linzhp commented Jan 21, 2022

After checking out latest master and run bazel build //src:bazel, I still got:

src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java:190: error: cannot find symbol
      var unused = toCheck.clone();
      ^
  symbol:   class var
  location: class DigestHashFunction

My local javac -version is "javac 11.0.11". What should I do to compile Java 11 features in Bazel source code?

@cushon
Copy link
Contributor Author

cushon commented Jan 21, 2022

Are you using bazel 5.0.0?

@linzhp
Copy link
Contributor

linzhp commented Jan 21, 2022

Ah, Bazel 5.0 fixed the issue. I was on Bazel 4.2.2. Didn't realize that Bazel now requires 5.0 to build itself 3 days after its released.

@damingerdai
Copy link

but i also meet this issue with bazel 5.0
https://github.com/damingerdai/jobs/runs/7263067500?check_suite_focus=true

lyqaiym pushed a commit to lyqaiym/bazel that referenced this issue May 14, 2023
3.1 bazelbuild#15168

distdir_deps.bzl 文件修改
"abseil-cpp": {
        "archive": "997aaf3a28308eba1b9156aa35ab7bca9688e9f6.tar.gz",
        "sha256": "35f22ef5cb286f09954b7cc4c85b5a3f6221c9d4df6b8c4a1e9d399555b366ee",
        "urls": [
            "https://mirror.bazel.build/github.com/abseil/abseil-cpp/archive/997aaf3a28308eba1b9156aa35ab7bca9688e9f6.tar.gz",
            "https://github.com/abseil/abseil-cpp/archive/997aaf3a28308eba1b9156aa35ab7bca9688e9f6.tar.gz",
        ],
        "used_in": [
            "additional_distfiles",
            "test_WORKSPACE_files",
        ],
    },
"com_google_absl": {
        "archive": "20211102.0.tar.gz",
        "sha256": "dcf71b9cba8dc0ca9940c4b316a0c796be8fab42b070bb6b7cab62b48f0e66c4",
        "urls": [
            "https://mirror.bazel.build/github.com/abseil/abseil-cpp/archive/refs/tags/20211102.0.tar.gz",
            "https://github.com/abseil/abseil-cpp/archive/refs/tags/20211102.0.tar.gz",
        ],
        "used_in": [
            "additional_distfiles",
            "test_WORKSPACE_files",
        ],
        "strip_prefix": "abseil-cpp-20211102.0",
    },

WORKSPACE 文件增加
# Override the abseil-cpp version defined in grpc_deps(), which doesn't work on latest macOS
# Fixes bazelbuild#15168
dist_http_archive(
    name = "com_google_absl",
)

  3.2 src/main/java/net/starlark/java/eval/EvalUtils.java:399: 错误: 找不到符号
      return s.repeat(n);
              ^
  符号:   方法 repeat(int)
  位置: 类型为String的变量 s

    .bazelrc
     # Enable Bzlmod
     build:bzlmod --experimental_enable_bzlmod
     # TODO(pcloudy): The following should be removed after fixing bazelbuild#14279
     build:bzlmod --crosstool_top=@rules_cc.0.0.1.cc_configure.local_config_cc//:toolchain
     build:bzlmod --xcode_version_config=@rules_cc.0.0.1.cc_configure.local_config_xcode//:host_xcodes

     # Enable Java 11 language features (bazelbuild#14592)
     # Toolchain resolution configuration for Bazel >= 5
     build --java_language_version=11
     build --tool_java_language_version=11
     # Legacy configuration for Bazel <= 4
     build --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
     build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
lyqaiym pushed a commit to lyqaiym/bazel that referenced this issue May 14, 2023
3.1 bazelbuild#15168

distdir_deps.bzl 文件修改
"abseil-cpp": {
        "archive": "997aaf3a28308eba1b9156aa35ab7bca9688e9f6.tar.gz",
        "sha256": "35f22ef5cb286f09954b7cc4c85b5a3f6221c9d4df6b8c4a1e9d399555b366ee",
        "urls": [
            "https://mirror.bazel.build/github.com/abseil/abseil-cpp/archive/997aaf3a28308eba1b9156aa35ab7bca9688e9f6.tar.gz",
            "https://github.com/abseil/abseil-cpp/archive/997aaf3a28308eba1b9156aa35ab7bca9688e9f6.tar.gz",
        ],
        "used_in": [
            "additional_distfiles",
            "test_WORKSPACE_files",
        ],
    },
"com_google_absl": {
        "archive": "20211102.0.tar.gz",
        "sha256": "dcf71b9cba8dc0ca9940c4b316a0c796be8fab42b070bb6b7cab62b48f0e66c4",
        "urls": [
            "https://mirror.bazel.build/github.com/abseil/abseil-cpp/archive/refs/tags/20211102.0.tar.gz",
            "https://github.com/abseil/abseil-cpp/archive/refs/tags/20211102.0.tar.gz",
        ],
        "used_in": [
            "additional_distfiles",
            "test_WORKSPACE_files",
        ],
        "strip_prefix": "abseil-cpp-20211102.0",
    },

WORKSPACE 文件增加
# Override the abseil-cpp version defined in grpc_deps(), which doesn't work on latest macOS
# Fixes bazelbuild#15168
dist_http_archive(
    name = "com_google_absl",
)

  3.2 src/main/java/net/starlark/java/eval/EvalUtils.java:399: 错误: 找不到符号
      return s.repeat(n);
              ^
  符号:   方法 repeat(int)
  位置: 类型为String的变量 s

    .bazelrc
     # Enable Bzlmod
     build:bzlmod --experimental_enable_bzlmod
     # TODO(pcloudy): The following should be removed after fixing bazelbuild#14279
     build:bzlmod --crosstool_top=@rules_cc.0.0.1.cc_configure.local_config_cc//:toolchain
     build:bzlmod --xcode_version_config=@rules_cc.0.0.1.cc_configure.local_config_xcode//:host_xcodes

     # Enable Java 11 language features (bazelbuild#14592)
     # Toolchain resolution configuration for Bazel >= 5
     build --java_language_version=11
     build --tool_java_language_version=11
     # Legacy configuration for Bazel <= 4
     build --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
     build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
     -------------
     def use_cpp_toolchain(mandatory = True):
         """
         Helper to depend on the c++ toolchain.

         Usage:
         ```
         my_rule = rule(
             toolchains = [other toolchain types] + use_cpp_toolchain(),
         )
         ```

         Args:
           mandatory: Whether or not it should be an error if the toolchain cannot be resolved.
             Currently ignored, this will be enabled when optional toolchain types are added.

         Returns:
           A list that can be used as the value for `rule.toolchains`.
         """
         return [CPP_TOOLCHAIN_TYPE]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants