Skip to content

Commit

Permalink
chore: bzlmod (#162)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alexeagle authored Jun 2, 2023
1 parent 913676d commit bb956d0
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 24 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e2e/
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 3 additions & 0 deletions .bcr/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fixedReleaser:
login: mattem
email: [email protected]
15 changes: 15 additions & 0 deletions .bcr/metadata.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"homepage": "https://docs.aspect.build/rules/aspect_rules_py",
"maintainers": [
{
"email": "[email protected]",
"github": "aspect-build",
"name": "Aspect team"
}
],
"repository": [
"github:aspect-build/rules_py"
],
"versions": [],
"yanked_versions": {}
}
10 changes: 10 additions & 0 deletions .bcr/presubmit.yml
Original file line number Diff line number Diff line change
@@ -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:
- "//..."
5 changes: 5 additions & 0 deletions .bcr/source.template.json
Original file line number Diff line number Diff line change
@@ -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"
}
36 changes: 25 additions & 11 deletions .github/workflows/release_prep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 "\`\`\`"
13 changes: 13 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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")
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
<https://github.com/aspect-build/rules_py/releases>
copy the WORKSPACE snippet into your `WORKSPACE` file.
1 change: 1 addition & 0 deletions e2e/smoke/.bazelversion
6 changes: 6 additions & 0 deletions e2e/smoke/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@aspect_rules_py//py:defs.bzl", "py_test")

py_test(
name = "bin",
srcs = ["__main__.py"],
)
22 changes: 22 additions & 0 deletions e2e/smoke/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
14 changes: 14 additions & 0 deletions e2e/smoke/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions e2e/smoke/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file replaces `WORKSPACE.bazel` when --enable_bzlmod is set.
1 change: 1 addition & 0 deletions e2e/smoke/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello world")
2 changes: 2 additions & 0 deletions py/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion py/private/py_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions py/tests/external-deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
18 changes: 9 additions & 9 deletions py/tests/external-deps/expected_pathing
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bb956d0

Please sign in to comment.