Skip to content

rules_python 0.26 changes behavior for sequence of py_repositories call #1560

Closed
@mikex-oss

Description

@mikex-oss

🐞 bug report

Affected Rule

The issue is caused by the rule:

It seems to be a new behavior introduced in rules_python 0.26 that forces the py_repositories call to take place before loading other rules_python loads:

load("@<python_register_toolchains_name>//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

Seems to be related to #1428.

Is this a regression?

Yes, the previous version in which this bug was not present was: 0.25.0

Yes, the py_repositories() call can take place later within a helper function of a .bzl file with other rules_python loads (see below).

Description

A clear and concise description of the problem...

In rules_python 0.25, using Bazel workspaces, the following works:

# WORKSPACE
load("@some_repo//:load_deps.bzl", "load_external_deps")

load_external_deps()

load("@some_repo//:init_toolchains.bzl", "initialize_python_toolchain")

initialize_python_toolchain(version = "3.9")

load("@some_repo//:init_deps.bzl", "initialize_external_deps")

initialize_external_deps()
# load_deps.bzl
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def load_external_deps():
    ...
    http_archive(
        name = "rules_python",
        ...
    )
    ...
# init_toolchains.bzl
load("@rules_python//python:repositories.bzl", "python_register_toolchains")

def initialize_python_toolchain(version):
    python_register_toolchains(
      name = "project_python",
      version = version,
    )
# init_deps.bzl
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
load("@project_python//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")
load("@rules_python//python:repositories.bzl", "py_repositories")

def initialize_external_deps():
    bazel_skylib_workspace()
    protobuf_deps()
    py_repositories()  # moving this to first line of function does not matter

    pip_parse(
        ...
        python_interpreter_target = interpreter,
    )

This same WORKSPACE sequence fails in rules_python 0.26. To avoid updating every repo WORKSPACE we have, the only workaround seems to be to move the py_repositories call to anywhere within the initialize_python_toolchain helper function. It does not seem to matter whether that call happens before or after the call to python_register_toolchains. It just must be before the interpreter and pip_parse loads (moving py_repositories before any other external dep helpers inside initialize_external_deps does not matter).

🔬 Minimal Reproduction

See above.

🔥 Exception or Error





ERROR: Failed to load Starlark extension '@rules_python_internal//:rules_python_config.bzl'.
Cycle in the workspace file detected. This indicates that a repository is used prior to being defined.
The following chain of repository dependencies lead to the missing definition.
 - @rules_python_internal
This could either mean you have to add the '@rules_python_internal' repository with a statement like `http_archive` in your WORKSPACE file (note that transitive dependencies are not added automatically), or move an existing definition earlier in your WORKSPACE file.
ERROR: Error computing the main repository mapping: cycles detected during computation of main repo mapping

🌍 Your Environment

Operating System:

  
centos7
  

Output of bazel version:

  
bazel 6.4.0
  

Rules_python version:

  
0.26.0
  

Anything else relevant?

I couldn't figure out if there was a single load that mattered from:

load("@project_python//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

I tried selectively removing one or the other (stuff downstream should still fail due to missing references) but they still failed early at the missing rules_python_internal. Only when I remove both does it generate a different error (about missing pip_parse reference).

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