From 130fade58373846646804b2a0d059d4f2e8c7146 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Tue, 20 Feb 2024 11:12:19 -0800 Subject: [PATCH] Switch to Bazelmod from WORKSPACE (#11) --- .bazelrc | 4 ++-- .gitignore | 4 ++++ MODULE.bazel | 38 +++++++++++++++++++++++++++++++++ WORKSPACE | 60 ---------------------------------------------------- 4 files changed, 44 insertions(+), 62 deletions(-) create mode 100644 MODULE.bazel delete mode 100644 WORKSPACE diff --git a/.bazelrc b/.bazelrc index 3ea47bb..1208471 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,5 +1,5 @@ -# Don't use the MODULE.bazel stuff yet. -common --noenable_bzlmod +# Use the MODULE.bazel stuff instead of the old WORKSPACE. +common --enable_bzlmod # Show output if tests fail. test --test_output=errors diff --git a/.gitignore b/.gitignore index 9f16ff8..eafa672 100644 --- a/.gitignore +++ b/.gitignore @@ -162,3 +162,7 @@ secrets.sh /.clwb/ # End of https://www.toptal.com/developers/gitignore/api/bazel + +# Ignore Bazel's lockfile until usability gets resolved. See +# https://github.com/bazelbuild/bazel/issues/20369 +MODULE.bazel.lock diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..ccf7a1f --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,38 @@ +# Name the workspace/module +module( + name = "bitwarden-to-keepass", + version = "0.0.0", + compatibility_level = 1, +) + +# Install rules_python, which allows us to define how bazel should work with python files. +# See https://github.com/bazelbuild/rules_python/blob/c6941a8dad4c7a221125fbad7c8bfaac377e00ba/examples/bzlmod/MODULE.bazel +bazel_dep(name = "rules_python", version = "0.31.0") + +# Init the python toolchain using the extension. +# This is similar to the "python_register_toolchains" function in WORKSPACE. +# It creates a hermetic python rather than relying on a system-installed interpreter. +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.toolchain( + configure_coverage_tool = True, + # Available versions are listed in @rules_python//python:versions.bzl. + # For some reason we can't set 3.8.18 here - it causes pip.parse() to fail. + python_version = "3.8" +) + +# Enable pip +pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") + +# Configure how we fetch python dependencies via pip +pip.parse( + # This name is what gets used in other BUILD files with `load()`. + hub_name = "pypi", + python_version = "3.8.18", + # TODO: what to put here? Using this might resolve the "can't set 3.8.18" + # issue mentioned above. + # python_interpreter_target = python, + requirements_lock = "//:requirements_lock.txt", +) + +# Same as WORKSPACE install_deps() - actually install the python deps. +use_repo(pip, "pypi") diff --git a/WORKSPACE b/WORKSPACE deleted file mode 100644 index 90667d4..0000000 --- a/WORKSPACE +++ /dev/null @@ -1,60 +0,0 @@ -# Name the workspace -workspace(name = "bitwarden-to-keepass") - -# Install rules_python, which allows us to define how bazel should work with python files. -# See https://rules-python.readthedocs.io/en/latest/getting-started.html#using-a-workspace-file -# Note: These "##### START ... #####" comments are just my own - they do not have -# any meaning in bazel. -##### START install rules_python snippet (https://github.com/bazelbuild/rules_python/releases/tag/0.28.0) ##### -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "rules_python", - sha256 = "d70cd72a7a4880f0000a6346253414825c19cdd40a28289bdf67b8e6480edff8", - strip_prefix = "rules_python-0.28.0", - url = "https://github.com/bazelbuild/rules_python/releases/download/0.28.0/rules_python-0.28.0.tar.gz", -) - -load("@rules_python//python:repositories.bzl", "py_repositories") - -py_repositories() -##### END install rules_python snippet ##### - - -# Use a hermetic python rather than relying on a system-installed interpreter: -# See https://rules-python.readthedocs.io/en/stable/getting-started.html#toolchain-registration -##### START hermetic python snippet ##### -load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains") - -py_repositories() - -python_register_toolchains( - name = "python3_8", - # Available versions are listed in @rules_python//python:versions.bzl. - # We recommend using the same version your team is already standardized on. - python_version = "3.8.18", - # Support coverage. See https://rules-python.readthedocs.io/en/stable/coverage.html - register_coverage_tool = True, -) - -load("@python3_8//:defs.bzl", interpreter = "interpreter") -##### END hermetic python snippet ##### - - -# Install dependencies from PyPI. -# See https://rules-python.readthedocs.io/en/stable/pypi-dependencies.html#using-a-workspace-file -# and https://rules-python.readthedocs.io/en/stable/pip.html -##### START install python dependencies snippet ##### -load("@rules_python//python:pip.bzl", "pip_parse") - -pip_parse( - name = "pypi", - python_interpreter_target = interpreter, # From hermetic python snippet - # TODO: Can requirements come from pyproject.toml? - requirements_lock = "//:requirements_lock.txt", -) - -load("@pypi//:requirements.bzl", "install_deps") - -install_deps() -##### END install python dependencies snippet #####