Patch rules_graalvm to avoid using deprecated ctx.resolve_tools

Fix Bazel build with Bazel@HEAD: https://buildkite.com/bazel/bazel-at-head-plus-downstream/builds/4181#019265b3-8ad1-45e3-b9a6-b32812ef6bfd

PiperOrigin-RevId: 683102846
Change-Id: I62d9a103f4a349af57b8655695eb89c01aa54766
diff --git a/BUILD b/BUILD
index 46edc42..0d6ed38 100644
--- a/BUILD
+++ b/BUILD
@@ -83,6 +83,7 @@
         "//third_party/remoteapis:MODULE.bazel",
         "//third_party:BUILD",
         "//third_party:rules_jvm_external_6.0.patch",
+        "//third_party:rules_graalvm_fix.patch",
         "//third_party/protobuf:BUILD",
         "//third_party/protobuf:proto_info_bzl_deps.patch",
         "//third_party/protobuf:remove_rules_rust.patch",
diff --git a/MODULE.bazel b/MODULE.bazel
index 0bebed3..6d82d39 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -73,6 +73,13 @@
     version = "6.0",
 )
 
+# TODO(pcloudy): Remove once the next version of rules_graalvm is available.
+single_version_override(
+    module_name = "rules_graalvm",
+    patch_strip = 1,
+    patches = ["//third_party:rules_graalvm_fix.patch"],
+)
+
 local_path_override(
     module_name = "remoteapis",
     path = "./third_party/remoteapis",
diff --git a/third_party/rules_graalvm_fix.patch b/third_party/rules_graalvm_fix.patch
new file mode 100644
index 0000000..eec1f9d
--- /dev/null
+++ b/third_party/rules_graalvm_fix.patch
@@ -0,0 +1,64 @@
+commit 07d43c2655c595b4a6fb6eab685c9a9a052ddee6
+Author: Tiago Quelhas <tjgq@google.com>
+Date:   Mon Sep 16 15:12:33 2024 +0200
+
+    fix: avoid using deprecated ctx.resolve_tools
+
+    See https://github.com/bazelbuild/bazel/issues/22249 for context.
+
+    Signed-off-by: Tiago Quelhas <tjgq@google.com>
+
+diff --git a/internal/native_image/rules.bzl b/internal/native_image/rules.bzl
+index 0d0a2ce..ca84a91 100644
+--- a/internal/native_image/rules.bzl
++++ b/internal/native_image/rules.bzl
+@@ -37,41 +37,28 @@ def _build_action_message(ctx):
+     return (_mode_label[ctx.attr.optimization_mode or "default"])
+
+ def _graal_binary_implementation(ctx):
+-    graal_attr = ctx.attr.native_image_tool
+-    extra_tool_deps = []
+-    gvm_toolchain = None
++    graal_attr = ctx.executable.native_image_tool
++
+     classpath_depset = depset(transitive = [
+         dep[JavaInfo].transitive_runtime_jars
+         for dep in ctx.attr.deps
+     ])
+
+-    graal = None
+     direct_inputs = []
+     transitive_inputs = [classpath_depset]
+
+     # resolve via toolchains
+-    info = ctx.toolchains[_GVM_TOOLCHAIN_TYPE].graalvm
+-
+-    # but fall back to explicitly-provided tool, which should override, with the
+-    # remainder of the resolved toolchain present
+-    resolved_graal = graal_attr or info.native_image_bin
+-    gvm_toolchain = info
+-    extra_tool_deps.append(info.gvm_files)
++    gvm_toolchain = ctx.toolchains[_GVM_TOOLCHAIN_TYPE].graalvm
+
+-    graal_inputs, _ = ctx.resolve_tools(tools = [
+-        resolved_graal,
+-    ] + extra_tool_deps)
+-
+-    graal = graal_inputs.to_list()[0]
++    # if a native-image tool is explicitly provided, it should override the one
++    # provided by the toolchain, but not the rest of the files it provides
++    graal = graal_attr or gvm_toolchain.native_image_bin.files_to_run
+
+     # add toolchain files to transitive inputs
+     transitive_inputs.append(gvm_toolchain.gvm_files[DefaultInfo].files)
+
+-    # if we're using an explicit tool, add it to the direct inputs
+-    if graal:
+-        direct_inputs.append(graal)
+-    else:
+-        # still failed to resolve: cannot resolve via either toolchains or attributes.
++    if not graal:
++        # cannot resolve via either toolchains or attributes.
+         fail("""
+             No `native-image` tool found. Please either define a `native_image_tool` in your target,
+             or install a GraalVM `native-image` toolchain.