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

fix(native_image): clean up construction of inputs #70

Merged
merged 2 commits into from
Sep 1, 2023

Conversation

fmeum
Copy link
Collaborator

@fmeum fmeum commented Aug 31, 2023

depset structures should be kept as flat as possible. C++ toolchain files are also already depsets and thus shouldn't be passed into tools.

@fmeum fmeum requested a review from sgammon as a code owner August 31, 2023 06:40
@fmeum fmeum force-pushed the depset branch 2 times, most recently from d41ec9f to 94796f0 Compare August 31, 2023 12:27
@fmeum
Copy link
Collaborator Author

fmeum commented Aug 31, 2023

Stacked on #71

`depset` structures should be kept as flat as possible. C++ toolchain
files are also already `depset`s and thus shouldn't be passed into
`tools`.

Signed-off-by: Sam Gammon <sam@elide.ventures>
@sgammon
Copy link
Owner

sgammon commented Sep 1, 2023

#71 merged, rebased (and signed-off and signed)

will examine expected build failure and hopefully solve so i can hand off to you

@sgammon sgammon self-assigned this Sep 1, 2023
@sgammon sgammon added this to the 1.0.0 milestone Sep 1, 2023
@sgammon sgammon added the enhancement New feature or request label Sep 1, 2023
sgammon added a commit that referenced this pull request Sep 1, 2023
- fix: explicitly check against modern rule status before resolving
  toolchain, unconditionally take it if running modern

applies on top of #70

Signed-off-by: Sam Gammon <sam@elide.ventures>
sgammon added a commit that referenced this pull request Sep 1, 2023
- fix: explicitly check against modern rule status before resolving
  toolchain, unconditionally take it if running modern
- fix: direct deps for `native_image_tool` attr
- fix: preserve legacy rule behavior

applies on top of #70

Signed-off-by: Sam Gammon <sam@elide.ventures>
- fix: explicitly check against modern rule status before resolving
  toolchain, unconditionally take it if running modern
- fix: direct deps for `native_image_tool` attr
- fix: preserve legacy rule behavior

applies on top of sgammon#70

Signed-off-by: Sam Gammon <sam@elide.ventures>
@sgammon
Copy link
Owner

sgammon commented Sep 1, 2023

@fmeum adjusted to avoid breakages with legacy rules (from a0c767a):

diff --git a/internal/native_image/rules.bzl b/internal/native_image/rules.bzl
index c667cf1..03fa527 100644
--- a/internal/native_image/rules.bzl
+++ b/internal/native_image/rules.bzl
@@ -107,28 +107,39 @@ def _graal_binary_implementation(ctx):
     direct_inputs = []
     transitive_inputs = [classpath_depset]
 
-    if graal_attr == None:
+    # if we aren't handed a specific `native-image` tool binary, or we are running
+    # under the modern `native_image` rule, then attempt to resolve via toolchains...
+    if graal_attr == None and not (ctx.attr._legacy_rule):
         # resolve via toolchains
         info = ctx.toolchains[_GVM_TOOLCHAIN_TYPE].graalvm
-        graal_attr = info.native_image_bin
+
+        # 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)
 
         graal_inputs, _ = ctx.resolve_tools(tools = [
-            graal_attr,
+            resolved_graal,
         ] + extra_tool_deps)
 
         graal = graal_inputs.to_list()[0]
+
+        # if we're using an explicit tool, add it to the direct inputs
+        if graal_attr:
+            direct_inputs.append(graal_inputs)
+
+        # add toolchain files to transitive inputs
         transitive_inputs.append(gvm_toolchain.gvm_files[DefaultInfo].files)
-    else:
-        # otherwise, use the legacy code path
+    elif graal_attr != None:
+        # otherwise, use the legacy code path. the `graal` value is used in the run
+        # `executable` so it isn't listed in deps for earlier Bazel versions.
         graal_inputs, _, _ = ctx.resolve_command(tools = [
             graal_attr,
         ])
         graal = graal_inputs[0]
-        direct_inputs.append(graal_inputs)
-
-    if graal_attr == None:
+        direct_inputs.append(graal)
+    else:
         # still failed to resolve: cannot resolve via either toolchains or attributes.
         fail("""
             No `native-image` tool found. Please either define a `native_image_tool` in your target,
@@ -263,6 +274,7 @@ def _graal_binary_implementation(ctx):
         args.add("-H:+JNI")
 
     inputs = depset(direct_inputs, transitive = transitive_inputs)
+
     run_params = {
         "outputs": [binary],
         "arguments": [args],

it was

direct_inputs.append(graal_inputs)

that was causing the classic rules to fail with

ERROR: /home/runner/work/rules_graalvm/rules_graalvm/example/integration_tests/legacy-rules/sample/BUILD.bazel:19:13: in _native_image rule //sample:main-native: 
Traceback (most recent call last):
	File "/home/runner/.cache/bazel/_bazel_runner/69fba1e10e2cd[32](https://github.com/sgammon/rules_graalvm/actions/runs/6044192459/job/16402423305#step:9:33)bab37c75b69f47e2f/external/rules_graalvm/internal/native_image/rules.bzl", line 270, column 20, in _graal_binary_implementation
		inputs = depset(direct_inputs, transitive = transitive_inputs)
Error in depset: depset elements must not be mutable values

it seems to fix it to append only the file, like

direct_inputs.append(graal)  # graal is `graal_inputs[0]`

and it shouldn't affect your use in bazelbuild/bazel#19361, since that code path is only used by the legacy rules.

@sonarcloud
Copy link

sonarcloud bot commented Sep 1, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sgammon sgammon merged commit f6d03d3 into sgammon:main Sep 1, 2023
43 checks passed
@sgammon sgammon mentioned this pull request Sep 1, 2023
@fmeum fmeum deleted the depset branch September 1, 2023 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging this pull request may close these issues.

2 participants