From 93995a50c447a5492dbd2c61e8258a64c6a65a7e Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Fri, 29 Sep 2023 22:08:22 +0900 Subject: [PATCH 1/3] test: work with minor versions --- examples/bzlmod/MODULE.bazel | 8 ++++++-- examples/bzlmod/libs/my_lib/__init__.py | 7 ++++++- examples/bzlmod/other_module/MODULE.bazel | 2 +- examples/bzlmod/tests/my_lib_test.py | 10 +++++++--- examples/multi_python_versions/WORKSPACE | 6 +++--- examples/multi_python_versions/libs/my_lib/__init__.py | 10 +++++++++- 6 files changed, 32 insertions(+), 11 deletions(-) diff --git a/examples/bzlmod/MODULE.bazel b/examples/bzlmod/MODULE.bazel index 0d1c7a736b..cd24ed10d9 100644 --- a/examples/bzlmod/MODULE.bazel +++ b/examples/bzlmod/MODULE.bazel @@ -28,7 +28,8 @@ python.toolchain( # work in progress. python.toolchain( configure_coverage_tool = True, - python_version = "3.10", + # One can also provide the full Python version. + python_version = "3.10.9", ) # You only need to load this repositories if you are using multiple Python versions. @@ -102,7 +103,10 @@ pip.parse( ) pip.parse( hub_name = "pip", - python_version = "3.10", + # NOTE: if you are registering the python toolchain with the full version + # in 'python.toolchain' call, then you need to use the full version here as + # well or otherwise the resolution will fail. + python_version = "3.10.9", requirements_lock = "//:requirements_lock_3_10.txt", requirements_windows = "//:requirements_windows_3_10.txt", # These modifications were created above and we diff --git a/examples/bzlmod/libs/my_lib/__init__.py b/examples/bzlmod/libs/my_lib/__init__.py index 6db2e85191..9b99feb755 100644 --- a/examples/bzlmod/libs/my_lib/__init__.py +++ b/examples/bzlmod/libs/my_lib/__init__.py @@ -19,4 +19,9 @@ def websockets_is_for_python_version(sanitized_version_check): # We are checking that the name of the repository folders # match the expexted generated names. If we update the folder # structure or naming we will need to modify this test - return f"pip_{sanitized_version_check}_websockets" in websockets.__file__ + if f"pip_{sanitized_version_check}_websockets" in websockets.__file__: + return True + + raise RuntimeError( + f"Expected version '{sanitized_version_check}' was not in {websockets.__file__}" + ) diff --git a/examples/bzlmod/other_module/MODULE.bazel b/examples/bzlmod/other_module/MODULE.bazel index 959501abc2..3895787e6d 100644 --- a/examples/bzlmod/other_module/MODULE.bazel +++ b/examples/bzlmod/other_module/MODULE.bazel @@ -27,7 +27,7 @@ PYTHON_NAME_311 = "python_3_11" python = use_extension("@rules_python//python/extensions:python.bzl", "python") python.toolchain( configure_coverage_tool = True, - python_version = "3.9", + python_version = "3.9.10", ) python.toolchain( configure_coverage_tool = True, diff --git a/examples/bzlmod/tests/my_lib_test.py b/examples/bzlmod/tests/my_lib_test.py index b06374c983..dbef1a874a 100644 --- a/examples/bzlmod/tests/my_lib_test.py +++ b/examples/bzlmod/tests/my_lib_test.py @@ -21,6 +21,10 @@ # If we update the folder structure or naming we need to modify this test. sanitized_version_check = f"{sys.version_info.major}{sys.version_info.minor}" -if not my_lib.websockets_is_for_python_version(sanitized_version_check): - print("expected package for Python version is different than returned") - sys.exit(1) +try: + my_lib.websockets_is_for_python_version(sanitized_version_check) +except RuntimeError as e: + print( + f"Expected package for Python version is different than returned. Current version is {sys.version_info}" + ) + raise e diff --git a/examples/multi_python_versions/WORKSPACE b/examples/multi_python_versions/WORKSPACE index 35855ca1e1..aa4be69cb7 100644 --- a/examples/multi_python_versions/WORKSPACE +++ b/examples/multi_python_versions/WORKSPACE @@ -22,7 +22,7 @@ python_register_multi_toolchains( "3.8", "3.9", "3.10", - "3.11", + "3.11.1", ], register_coverage_tool = True, ) @@ -38,13 +38,13 @@ multi_pip_parse( default_version = default_python_version, python_interpreter_target = { "3.10": interpreter_3_10, - "3.11": interpreter_3_11, + "3.11.1": interpreter_3_11, "3.8": interpreter_3_8, "3.9": interpreter_3_9, }, requirements_lock = { "3.10": "//requirements:requirements_lock_3_10.txt", - "3.11": "//requirements:requirements_lock_3_11.txt", + "3.11.1": "//requirements:requirements_lock_3_11.txt", "3.8": "//requirements:requirements_lock_3_8.txt", "3.9": "//requirements:requirements_lock_3_9.txt", }, diff --git a/examples/multi_python_versions/libs/my_lib/__init__.py b/examples/multi_python_versions/libs/my_lib/__init__.py index 33cfb414f5..171fc5603a 100644 --- a/examples/multi_python_versions/libs/my_lib/__init__.py +++ b/examples/multi_python_versions/libs/my_lib/__init__.py @@ -16,4 +16,12 @@ def websockets_is_for_python_version(sanitized_version_check): - return f"pypi_{sanitized_version_check}_websockets" in websockets.__file__ + # We are checking that the name of the repository folders + # match the expexted generated names. If we update the folder + # structure or naming we will need to modify this test + if f"pypi_{sanitized_version_check}_websockets" in websockets.__file__: + return True + + raise RuntimeError( + f"Expected version '{sanitized_version_check}' was not in {websockets.__file__}" + ) From 9f06b4e66bf34606790fc627ad1667753f6146ff Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Fri, 29 Sep 2023 22:20:42 +0900 Subject: [PATCH 2/3] feat(bzlmod): use X.Y to search for toolchain duplicates --- python/extensions/python.bzl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/extensions/python.bzl b/python/extensions/python.bzl index c7c2c82c05..58925e561c 100644 --- a/python/extensions/python.bzl +++ b/python/extensions/python.bzl @@ -16,6 +16,7 @@ load("//python:repositories.bzl", "python_register_toolchains") load("//python/extensions/private:pythons_hub.bzl", "hub_repo") +load("//python/private:full_version.bzl", "full_version") load("//python/private:toolchains_repo.bzl", "multi_toolchain_aliases") # This limit can be increased essentially arbitrarily, but doing so will cause a rebuild of all @@ -74,7 +75,7 @@ def _python_impl(module_ctx): module_toolchain_versions = [] for toolchain_attr in mod.tags.toolchain: - toolchain_version = toolchain_attr.python_version + toolchain_version, _, _ = full_version(toolchain_attr.python_version).rpartition(".") toolchain_name = "python_" + toolchain_version.replace(".", "_") # Duplicate versions within a module indicate a misconfigured module. From 3ecc4dfeda6f328b219e5eafafa466300f92b7a2 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Fri, 29 Sep 2023 22:37:42 +0900 Subject: [PATCH 3/3] feat(legacy): ensure that we are using an X.Y version in pip_multiparse --- python/pip.bzl | 3 ++- python/private/toolchains_repo.bzl | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/python/pip.bzl b/python/pip.bzl index fb842cc4ce..789a344a65 100644 --- a/python/pip.bzl +++ b/python/pip.bzl @@ -19,6 +19,7 @@ load("//python/pip_install:requirements.bzl", _compile_pip_requirements = "compi load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") load("//python/private:full_version.bzl", "full_version") load("//python/private:render_pkg_aliases.bzl", "NO_MATCH_ERROR_MESSAGE_TEMPLATE") +load("//python/private:version_label.bzl", "version_label") compile_pip_requirements = _compile_pip_requirements package_annotation = _package_annotation @@ -369,7 +370,7 @@ def multi_pip_parse(name, default_version, python_versions, python_interpreter_t if not python_version in requirements_lock: fail("Missing requirements_lock for Python version %s in '%s'" % (python_version, name)) - pip_parse_name = name + "_" + python_version.replace(".", "_") + pip_parse_name = name + "_" + version_label(full_version(python_version), sep = "_") pip_parse( name = pip_parse_name, python_interpreter_target = python_interpreter_target[python_version], diff --git a/python/private/toolchains_repo.bzl b/python/private/toolchains_repo.bzl index 20dc9763e0..cba9f57b20 100644 --- a/python/private/toolchains_repo.bzl +++ b/python/private/toolchains_repo.bzl @@ -30,6 +30,7 @@ load( "PLATFORMS", "WINDOWS_NAME", ) +load(":full_version.bzl", "full_version") load(":which.bzl", "which_with_fail") def get_repository_name(repository_workspace): @@ -252,8 +253,12 @@ def _multi_toolchain_aliases_impl(rctx): rules_python = rctx.attr._rules_python_workspace.workspace_name for python_version, repository_name in rctx.attr.python_versions.items(): - file = "{}/defs.bzl".format(python_version) - rctx.file(file, content = """\ + python_version = full_version(python_version) + for is_short in [False, True]: + python_version_short, _, _ = python_version.rpartition(".") + v = python_version_short if is_short else python_version + + rctx.file("{}/defs.bzl".format(v), content = """\ # Generated by python/private/toolchains_repo.bzl load( @@ -266,6 +271,7 @@ load( _py_test = "py_test", ) +{warning} compile_pip_requirements = _compile_pip_requirements host_platform = _host_platform interpreter = _interpreter @@ -273,9 +279,10 @@ py_binary = _py_binary py_console_script_binary = _py_console_script_binary py_test = _py_test """.format( - repository_name = repository_name, - )) - rctx.file("{}/BUILD.bazel".format(python_version), "") + repository_name = repository_name, + warning = "" if is_short else 'print("switch to importing from \"{}:defs.bzl\"")'.format(python_version_short), + )) + rctx.file("{}/BUILD.bazel".format(v), "") pip_bzl = """\ # Generated by python/private/toolchains_repo.bzl