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

[Bug]: "unbound variable" error in py_test, py_binary when using py_runtime with interpreter_path set #302

Closed
wade-arista opened this issue Mar 8, 2024 · 0 comments · Fixed by #313
Assignees
Labels
bug Something isn't working untriaged Requires traige

Comments

@wade-arista
Copy link
Contributor

wade-arista commented Mar 8, 2024

What happened?

I declared a custom toolchain that points at the system python interpreter.

load("@rules_python//python:defs.bzl", "py_runtime", "py_runtime_pair")

py_runtime(
    name = "my_py3_runtime",
    interpreter_path = "/usr/bin/python",
    interpreter_version_info = {
        "major": "3",
        "minor": "10",
        "micro": "8",
    },
    python_version = "PY3",
)

py_runtime_pair(
    name = "my_py_runtime_pair",
    py2_runtime = None,
    py3_runtime = ":my_py3_runtime",
)

toolchain(
    name = "default",
    toolchain = ":my_py_runtime_pair",
    toolchain_type = "@bazel_tools//tools/python:toolchain_type",
)

I registered it in MODULE.bazel:

register_toolchains("//toolchain:default")

When I tried to run a py_test, the sh wrapper failed:

$ bazel test --action_env=RUNFILES_LIB_DEBUG=1 src_test
==================== Test output for //:src_test:
INFO[runfiles.bash]: rlocation(aspect_rules_py~override/py/tools/venv_bin/venv_bin): start
INFO[runfiles.bash]: runfiles_current_repository(2): caller's path is (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_main/src_test)
INFO[runfiles.bash]: runfiles_current_repository(2): (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_main/src_test) has path (_main/src_test) relative to the runfiles directory (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles)
INFO[runfiles.bash]: runfiles_current_repository(2): (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_main/src_test) corresponds to rlocation path (_main/src_test)
INFO[runfiles.bash]: runfiles_current_repository(2): (_main/src_test) lies in the main repository
INFO[runfiles.bash]: rlocation(aspect_rules_py~override/py/tools/venv_bin/venv_bin): looking up canonical name for (aspect_rules_py~override) from () in (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_repo_mapping)
INFO[runfiles.bash]: rlocation(aspect_rules_py~override/py/tools/venv_bin/venv_bin): canonical name of target repo is ()
INFO[runfiles.bash]: rlocation(aspect_rules_py~override/py/tools/venv_bin/venv_bin): found under RUNFILES_DIR (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles), return
INFO[runfiles.bash]: rlocation(_main//usr/bin/python): start
ERROR[runfiles.bash]: rlocation(_main//usr/bin/python): path is not normalized
/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_main/src_test: line 25: $1: unbound variable
INFO[runfiles.bash]: rlocation(_main/src_test.venv.pth): start
INFO[runfiles.bash]: runfiles_current_repository(2): caller's path is (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_main/src_test)
INFO[runfiles.bash]: runfiles_current_repository(2): (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_main/src_test) has path (_main/src_test) relative to the runfiles directory (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles)
INFO[runfiles.bash]: runfiles_current_repository(2): (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_main/src_test) corresponds to rlocation path (_main/src_test)
INFO[runfiles.bash]: runfiles_current_repository(2): (_main/src_test) lies in the main repository
INFO[runfiles.bash]: rlocation(_main/src_test.venv.pth): looking up canonical name for (_main) from () in (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_repo_mapping)
INFO[runfiles.bash]: rlocation(_main/src_test.venv.pth): canonical name of target repo is ()
INFO[runfiles.bash]: rlocation(_main/src_test.venv.pth): found under RUNFILES_DIR (/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles), return
error: a value is required for '--python <PYTHON>' but none was supplied

Version

Development (host) and target OS/architectures: linux x86-64

Output of bazel --version: bazel 7.0.2

Version of the Aspect rules: rules_py 6962d6a (also v0.7.1)

Language(s) and/or frameworks involved: Python 3.10.8 (happens with 3.9, 3.11 as well)

How to reproduce

After defining the python toolchain as shown above, trying to run any py_test or py_binary target will reproduce the problem.

Min repro:
https://github.com/wade-arista/bazel-demo/tree/23ff2cebe6058a5e99535bd3f76c72b1b2b188ce

Test run:
https://github.com/wade-arista/bazel-demo/actions/runs/8209925408/job/22456408401#step:4:615

Any other information?

ERROR[runfiles.bash]: rlocation(_main//usr/bin/python): path is not normalized
/tmp/bazel-working-directory/_main/bazel-out/k8-fastbuild/bin/src_test.runfiles/_main/src_test: line 25: $1: unbound variable

It's trying to resolve {{ARG_PYTHON}} in

"${VENV_TOOL}" \
    --location "${VIRTUAL_ENV}" \
    --python "$(alocation $(rlocation {{ARG_PYTHON}}))" \
    --python-version "{{ARG_VENV_PYTHON_VERSION}}" \
    --pth-file "$(rlocation {{ARG_PTH_FILE}})"

See

"${VENV_TOOL}" \
--location "${VIRTUAL_ENV}" \
--python "$(alocation $(rlocation {{ARG_PYTHON}}))" \
--python-version "{{ARG_VENV_PYTHON_VERSION}}" \
--pth-file "$(rlocation {{ARG_PTH_FILE}})"
and
"{{ARG_PYTHON}}": to_rlocation_path(ctx, py_toolchain.python),

@wade-arista wade-arista added the bug Something isn't working label Mar 8, 2024
@github-actions github-actions bot added the untriaged Requires traige label Mar 8, 2024
@mattem mattem self-assigned this Mar 19, 2024
@github-project-automation github-project-automation bot moved this to ✅ Done in Open Source Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working untriaged Requires traige
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants