From 2bff7545cf0a94e66039ab6afd44f1c84d7a998c Mon Sep 17 00:00:00 2001 From: Gabriel Fringeli Date: Mon, 6 Jan 2025 04:44:11 +0100 Subject: [PATCH] fix: Obtain default Python version for Python hub repo from versions.bzl file (#138) --- CHANGELOG.md | 4 ++++ pycross/private/toolchain_helpers.bzl | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4041a416..b2153e73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [unreleased] +### Fixed + +- Obtain default Python version for Python hub repo from `versions.bzl` file, falling back to `interpreters.bzl` for backwards compatibility. `DEFAULT_PYTHON_VERSION` was [removed](https://github.com/bazelbuild/rules_python/blob/6a04d3832e82fec0a7b0675e9964b360bc358554/CHANGELOG.md?plain=1#L211) from `interpreters.bzl` in rules_python version 1.0.0. + ## [0.6.1] ### Added diff --git a/pycross/private/toolchain_helpers.bzl b/pycross/private/toolchain_helpers.bzl index 538c9009..a5ee0823 100644 --- a/pycross/private/toolchain_helpers.bzl +++ b/pycross/private/toolchain_helpers.bzl @@ -209,10 +209,19 @@ def _get_multi_python_versions(rctx, python_toolchain_repo): return versions def _get_default_python_version_bzlmod(rctx, pythons_hub_repo): - interpreters_bzl_file = Label("@@{}//:interpreters.bzl".format(pythons_hub_repo.workspace_name)) - build_content = rctx.read(interpreters_bzl_file) - - for line in build_content.splitlines(): + # Check if Python hub repo has versions.bzl file. versions.bzl was introduced in rules_python 0.37.0. + pythons_hub_build_file = pythons_hub_repo.relative("//:BUILD.bazel") + pythons_hub_repo_dir = rctx.path(pythons_hub_build_file).dirname + if pythons_hub_repo_dir.get_child("versions.bzl").exists: + # Use versions.bzl for rules_python 0.37.0+. + versions_bzl_file = Label("@@{}//:versions.bzl".format(pythons_hub_repo.workspace_name)) + content = rctx.read(versions_bzl_file) + else: + # Fall back to interpreters.bzl as versions.bzl does not exists for rules_python < 0.37.0. + # DEFAULT_PYTHON_VERSION was removed from interpreters.bzl in rules_python 1.0.0. + interpreters_bzl_file = Label("@@{}//:interpreters.bzl".format(pythons_hub_repo.workspace_name)) + content = rctx.read(interpreters_bzl_file) + for line in content.splitlines(): if line.startswith("DEFAULT_PYTHON_VERSION"): _, val = line.split("=") val = val.strip(" \"'")