From bb956d0d98dc60297787be8b906d7294ff38a983 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Fri, 2 Jun 2023 12:19:50 -0600 Subject: [PATCH] chore: bzlmod (#162) * chore: bzlmod - Upgrade to Bazel 6.2.0 since Bazel 5 has bzlmod bugs. TODO: update the GHA ci.yaml to test on Bazel 5 as well. - Introduce simple MODULE.bazel file - Add .bcr folder so the Publish to BCR app automates publishing on releases * chore: update goldens for Bazel 6 * chore: fixup BAZEL_TARGET rather than change assertion --- .bazelignore | 1 + .bazelversion | 2 +- .bcr/config.yml | 3 +++ .bcr/metadata.template.json | 15 +++++++++++ .bcr/presubmit.yml | 10 +++++++ .bcr/source.template.json | 5 ++++ .github/workflows/release_prep.sh | 36 +++++++++++++++++-------- MODULE.bazel | 13 +++++++++ README.md | 3 +-- e2e/smoke/.bazelversion | 1 + e2e/smoke/BUILD.bazel | 6 +++++ e2e/smoke/MODULE.bazel | 22 +++++++++++++++ e2e/smoke/WORKSPACE.bazel | 14 ++++++++++ e2e/smoke/WORKSPACE.bzlmod | 1 + e2e/smoke/__main__.py | 1 + py/defs.bzl | 2 ++ py/private/py_binary.bzl | 2 +- py/tests/external-deps/BUILD.bazel | 1 + py/tests/external-deps/expected_pathing | 18 ++++++------- 19 files changed, 132 insertions(+), 24 deletions(-) create mode 100644 .bazelignore create mode 100644 .bcr/config.yml create mode 100644 .bcr/metadata.template.json create mode 100644 .bcr/presubmit.yml create mode 100644 .bcr/source.template.json create mode 100644 MODULE.bazel create mode 120000 e2e/smoke/.bazelversion create mode 100644 e2e/smoke/BUILD.bazel create mode 100644 e2e/smoke/MODULE.bazel create mode 100644 e2e/smoke/WORKSPACE.bazel create mode 100644 e2e/smoke/WORKSPACE.bzlmod create mode 100644 e2e/smoke/__main__.py diff --git a/.bazelignore b/.bazelignore new file mode 100644 index 00000000..b1e3af62 --- /dev/null +++ b/.bazelignore @@ -0,0 +1 @@ +e2e/ diff --git a/.bazelversion b/.bazelversion index 86d00b98..6326acce 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1,4 +1,4 @@ -5.4.0 +6.2.0 # The first line of this file is used by Bazelisk and Bazel to be sure # the right version of Bazel is used to build and test this repo. # This also defines which version is used on CI. diff --git a/.bcr/config.yml b/.bcr/config.yml new file mode 100644 index 00000000..0a31de8c --- /dev/null +++ b/.bcr/config.yml @@ -0,0 +1,3 @@ +fixedReleaser: + login: mattem + email: matt@aspect.dev \ No newline at end of file diff --git a/.bcr/metadata.template.json b/.bcr/metadata.template.json new file mode 100644 index 00000000..d0ee3a72 --- /dev/null +++ b/.bcr/metadata.template.json @@ -0,0 +1,15 @@ +{ + "homepage": "https://docs.aspect.build/rules/aspect_rules_py", + "maintainers": [ + { + "email": "hello@aspect.dev", + "github": "aspect-build", + "name": "Aspect team" + } + ], + "repository": [ + "github:aspect-build/rules_py" + ], + "versions": [], + "yanked_versions": {} +} diff --git a/.bcr/presubmit.yml b/.bcr/presubmit.yml new file mode 100644 index 00000000..34333e34 --- /dev/null +++ b/.bcr/presubmit.yml @@ -0,0 +1,10 @@ +bcr_test_module: + module_path: "e2e/smoke" + matrix: + platform: ["debian10", "macos", "ubuntu2004", "windows"] + tasks: + run_tests: + name: "Run test module" + platform: ${{ platform }} + test_targets: + - "//..." diff --git a/.bcr/source.template.json b/.bcr/source.template.json new file mode 100644 index 00000000..acee4b17 --- /dev/null +++ b/.bcr/source.template.json @@ -0,0 +1,5 @@ +{ + "integrity": "**leave this alone**", + "strip_prefix": "{REPO}-{VERSION}", + "url": "https://github.com/{OWNER}/{REPO}/releases/download/{TAG}/{REPO}-{TAG}.tar.gz" +} diff --git a/.github/workflows/release_prep.sh b/.github/workflows/release_prep.sh index ce390332..3a24855e 100755 --- a/.github/workflows/release_prep.sh +++ b/.github/workflows/release_prep.sh @@ -12,7 +12,28 @@ git archive --format=tar --prefix=${PREFIX}/ ${TAG} | gzip > $ARCHIVE SHA=$(shasum -a 256 $ARCHIVE | awk '{print $1}') cat << EOF -WORKSPACE snippet: +## Using [Bzlmod] with Bazel 6: + +Add to your \`MODULE.bazel\` file: + +\`\`\`starlark +bazel_dep(name = "aspect_rules_py", version = "${TAG:1}") +\`\`\` + +And also register a Python toolchain, see rules_python. For example: + +\`\`\`starlark +EOF + +awk 'f;/--SNIP--/{f=1}' e2e/smoke/MODULE.bazel + +cat << EOF +\`\`\` + +[Bzlmod]: https://bazel.build/build/bzlmod + +## Using WORKSPACE + \`\`\`starlark load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( @@ -21,14 +42,7 @@ http_archive( strip_prefix = "${PREFIX}", url = "https://github.com/aspect-build/rules_py/releases/download/${TAG}/${ARCHIVE}", ) - -# Fetches the rules_py dependencies. -# If you want to have a different version of some dependency, -# you should fetch it *before* calling this. -# Alternatively, you can skip calling this function, so long as you've -# already fetched all the dependencies. -load("@aspect_rules_py//py:repositories.bzl", "rules_py_dependencies") -rules_py_dependencies() - -\`\`\` EOF + +awk 'f;/--SNIP--/{f=1}' e2e/smoke/WORKSPACE.bazel +echo "\`\`\`" diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 00000000..c1e7945b --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,13 @@ +"aspect-build/rules_py" + +module( + name = "aspect_rules_py", + compatibility_level = 1, + version = "0.0.0", +) + +# Lower-bound versions of direct dependencies. +# When bumping, add a comment explaining what's required from the newer release. +bazel_dep(name = "aspect_bazel_lib", version = "1.30.2") +bazel_dep(name = "bazel_skylib", version = "1.4.0") +bazel_dep(name = "rules_python", version = "0.19.0") diff --git a/README.md b/README.md index 1ab57c7f..6769ca63 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,5 @@ _Need help?_ This ruleset has support provided by https://aspect.dev. ## Installation -From the release you wish to use: +Follow instructions from the release you wish to use: -copy the WORKSPACE snippet into your `WORKSPACE` file. diff --git a/e2e/smoke/.bazelversion b/e2e/smoke/.bazelversion new file mode 120000 index 00000000..96cf9496 --- /dev/null +++ b/e2e/smoke/.bazelversion @@ -0,0 +1 @@ +../../.bazelversion \ No newline at end of file diff --git a/e2e/smoke/BUILD.bazel b/e2e/smoke/BUILD.bazel new file mode 100644 index 00000000..ccfc1ad6 --- /dev/null +++ b/e2e/smoke/BUILD.bazel @@ -0,0 +1,6 @@ +load("@aspect_rules_py//py:defs.bzl", "py_test") + +py_test( + name = "bin", + srcs = ["__main__.py"], +) diff --git a/e2e/smoke/MODULE.bazel b/e2e/smoke/MODULE.bazel new file mode 100644 index 00000000..1fddf778 --- /dev/null +++ b/e2e/smoke/MODULE.bazel @@ -0,0 +1,22 @@ +"Bazel dependencies" +bazel_dep(name = "aspect_rules_py", dev_dependency = True, version = "0.0.0") + +local_path_override( + module_name = "aspect_rules_py", + path = "../..", +) + +#---SNIP--- Below here is re-used in the snippet published on releases +bazel_dep(name = "rules_python", dev_dependency = True, version = "0.22.0") + +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.toolchain( + name = "python3_9", + configure_coverage_tool = True, + python_version = "3.9", +) +use_repo(python, "python3_9_toolchains") + +register_toolchains( + "@python3_9_toolchains//:all", +) \ No newline at end of file diff --git a/e2e/smoke/WORKSPACE.bazel b/e2e/smoke/WORKSPACE.bazel new file mode 100644 index 00000000..1a7ef897 --- /dev/null +++ b/e2e/smoke/WORKSPACE.bazel @@ -0,0 +1,14 @@ +# Override http_archive for local testing +local_repository( + name = "aspect_rules_py", + path = "../..", +) + +#---SNIP--- Below here is re-used in the snippet published on releases +# Fetches the rules_py dependencies. +# If you want to have a different version of some dependency, +# you should fetch it *before* calling this. +# Alternatively, you can skip calling this function, so long as you've +# already fetched all the dependencies. +load("@aspect_rules_py//py:repositories.bzl", "rules_py_dependencies") +rules_py_dependencies() diff --git a/e2e/smoke/WORKSPACE.bzlmod b/e2e/smoke/WORKSPACE.bzlmod new file mode 100644 index 00000000..a9d10936 --- /dev/null +++ b/e2e/smoke/WORKSPACE.bzlmod @@ -0,0 +1 @@ +# This file replaces `WORKSPACE.bazel` when --enable_bzlmod is set. diff --git a/e2e/smoke/__main__.py b/e2e/smoke/__main__.py new file mode 100644 index 00000000..8cde7829 --- /dev/null +++ b/e2e/smoke/__main__.py @@ -0,0 +1 @@ +print("hello world") diff --git a/py/defs.bzl b/py/defs.bzl index 22cb275b..b17c4f78 100644 --- a/py/defs.bzl +++ b/py/defs.bzl @@ -31,6 +31,8 @@ def py_binary(name, srcs = [], main = None, imports = ["."], **kwargs): main: the entry point. If absent, then the first entry in srcs is used. **kwargs: see [py_binary attributes](./py_binary) """ + if not main and not len(srcs): + fail("When 'main' is not specified, 'srcs' must be non-empty") _py_binary( name = name, srcs = srcs, diff --git a/py/private/py_binary.bzl b/py/private/py_binary.bzl index ddb10ed8..f5ac44de 100644 --- a/py/private/py_binary.bzl +++ b/py/private/py_binary.bzl @@ -19,7 +19,7 @@ def _py_binary_rule_imp(ctx): ) env = dict({ - "BAZEL_TARGET": str(ctx.label), + "BAZEL_TARGET": str(ctx.label).lstrip("@"), "BAZEL_WORKSPACE": ctx.workspace_name, "BAZEL_TARGET_NAME": ctx.attr.name, }, **ctx.attr.env) diff --git a/py/tests/external-deps/BUILD.bazel b/py/tests/external-deps/BUILD.bazel index 5b64f97a..3706cb72 100644 --- a/py/tests/external-deps/BUILD.bazel +++ b/py/tests/external-deps/BUILD.bazel @@ -27,6 +27,7 @@ genrule( "$(execpath pathing)", """sed "s#$$(pwd)#(pwd)#" """, "sed 's#^.*execroot/aspect_rules_py/external/python_toolchain_%s#(py_toolchain)#'" % host_platform, + "sed 's#bazel-out/[_a-z0-9-]*-exec-[A-Z0-9]*/bin/#bazel-out/[exec]/bin/#'", "sed 's#(main, .*)#(main, REDACTED)#'", ]) + "> $@", tools = ["pathing"], diff --git a/py/tests/external-deps/expected_pathing b/py/tests/external-deps/expected_pathing index b2ab8a35..00411ec2 100644 --- a/py/tests/external-deps/expected_pathing +++ b/py/tests/external-deps/expected_pathing @@ -1,23 +1,23 @@ -Python: (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/bin/python +Python: (pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/bin/python version: 3.9.15 (main, REDACTED) [Clang 14.0.3 ] version info: sys.version_info(major=3, minor=9, micro=15, releaselevel='final', serial=0) cwd: (pwd) -site-packages folder: ['(pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/lib/python3.9/site-packages'] +site-packages folder: ['(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/lib/python3.9/site-packages'] sys path: (py_toolchain)/lib/python39.zip (py_toolchain)/lib/python3.9 (py_toolchain)/lib/python3.9/lib-dynload -(pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/lib/python3.9/site-packages -(pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles -(pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps -(pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py +(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/lib/python3.9/site-packages +(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles +(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps +(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py -Entrypoint Path: (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps/pathing.py +Entrypoint Path: (pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps/pathing.py -Django location: (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/lib/python3.9/site-packages/django/__init__.py +Django location: (pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/lib/python3.9/site-packages/django/__init__.py Django version: 4.0.10 From lib with wheel dependency: Hello Matt -lib filepath: (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps/lib.py +lib filepath: (pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps/lib.py