Skip to content

Bazel treats local_jdk as a "can run anywhere" toolchain #18265

@illicitonion

Description

@illicitonion

Description of the bug:

I have a shell test which tries to run Java, and fetches it by adding "@bazel_tools//tools/jdk:current_java_runtime" to both toolchains and data.

If I have not configured any Java toolchains, and try to run this test from my host (which is a Mac) remotely on an Linux remote execution worker (exec=target=linux), rather than failing because no toolchain could be located, it brings along the local_jdk toolchain, which is a Mac toolchain.

This appears to be because the local_java_repository doesn't constraint the toolchain target it generates at all: https://cs.opensource.google/bazel/bazel/+/e8cedf459936895f939906474d9f9878a8a45edc:tools/jdk/local_java_repository.bzl;l=91-96

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

BUILD file:

sh_test(
    name = "list_test",
    srcs = ["list_test.sh"],
    env = {
        "JAVA": "$(JAVA)",
    },
    toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
    data = ["@bazel_tools//tools/jdk:current_java_runtime"],
)

list_test.sh:

#!/bin/bash -eux

if [[ "${JAVA}" == external/* ]]; then
  JAVA="${JAVA:9}"
fi
"${RUNFILES_DIR}/${JAVA}"

Command run on a Mac:

% bazel test :list_test --platforms=@some_linux_platform --extra_execution_platforms=@some_linux_platform --remote_executor=grpcs://linux-remote-execution-server

If I run locally on my Mac, I get the output of java help printed, if I run remotely, I get:

==================== Test output for //:list_test:
+ [[ external/local_jdk/bin/java == external/* ]]
+ JAVA=local_jdk/bin/java
+ /runner/build/4fde3789d4c6a2f3/root/bazel-out/darwin-fastbuild/bin/list_test.runfiles/local_jdk/bin/java
/runner/build/4fde3789d4c6a2f3/root/bazel-out/darwin-fastbuild/bin/list_test.runfiles/__main__/list_test: line 6: /runner/build/4fde3789d4c6a2f3/root/bazel-out/darwin-fastbuild/bin/list_test.runfiles/local_jdk/bin/java: cannot execute binary file: Exec format error

Which operating system are you running Bazel on?

macOS

What is the output of bazel info release?

release 6.1.1

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

/cc @fmeum who was helping me debug, and has #18262 as a possible solution

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2We'll consider working on this in future. (Assignee optional)next_monthIssues that we will review next month. This is currently mainly used by Configurability teamteam-Rules-JavaIssues for Java rulestype: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions