Skip to content

run_binary() a py_binary() fails on a pure system (NixOS) #3575

@hzeller

Description

@hzeller

A binary build with py_binary() will not run with run_binary() on a pure system (NixOS), but will work when using a genrule and explicitly choosing the Python toolchain coming from rules_python:

Example:

py_binary(
    name = "hello",
    srcs = ["hello.py"],
    main = "hello.py",
)

run_binary(
    name = "hello_run_binary",
    outs = ["hello_run_binary.out"],
    args = ["$(location hello_run_binary.out)"],
    tool = ":hello",
)

genrule(
    name = "hello_genrule",
    outs = ["hello_genrule.out"],
    cmd = "$(PYTHON3) $(location :hello) $@",
    toolchains = ["@rules_python//python:current_py_toolchain"],
    tools = [":hello"],
)

The MODULE.bazel gets latest rules_python and also registers the toolchain

bazel_dep(name = "bazel_skylib", version = "1.9.0")
bazel_dep(name = "rules_python", version = "1.8.3")

# Registering toolchain to leave no doubt
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    python_version = "3.11",
    is_default = True,
)

Running these targets with bazel 7 or bazel 8 on a pure system:

bazel build :hello_genrule    # works
bazel build :hello_run_binary # fails with "env: 'python3': No such file or directory"

Important is it to run on a pure hermetic systems such as NixOS, it seems to (accidentally ?) be working on impure systems such as Ubuntu.

Here is the full example with MODULE.bazel, BUILD and hello.py as well with a shell.nix

https://github.com/hzeller/python-run_binary-issue

Not marked as bug right now as I don't know if working as intended, and one has to always use genrule(), or if run_binary() is supposed to work, but for whatever reason it is not using the proper toolchain.

Context

I attempt to make the glib BZR module work by replacing run_binary() with genrule(), but in the review, the maintainer suspects this might actually be an issue in rules_python instead:

bazelbuild/bazel-central-registry#7467

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions