Skip to content

Commit

Permalink
chore: replace rules_go with prebuilt buildifier (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle authored Apr 20, 2022
1 parent 9ff991e commit 33b65d2
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.2
5.1.0
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: abhinavsingh/setup-bazel@v3
with:
# Must match version in repo's .bazelversion file.
version: 4.2.2
version: 5.1.0

- name: Run tests
run: ./test.sh
17 changes: 12 additions & 5 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")

load("@buildifier_prebuilt//:rules.bzl", "buildifier")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@rules_python//:version.bzl", "version")

package(default_visibility = ["//visibility:private"])

buildifier(
name = "buildifier",
name = "buildifier.check",
lint_mode = "warn",
mode = "diff",
)

package(default_visibility = ["//visibility:private"])
buildifier(
name = "buildifier",
lint_mode = "fix",
mode = "fix",
)

licenses(["notice"]) # MIT

Expand All @@ -22,9 +29,9 @@ filegroup(
"BUILD",
"LICENSE",
"config.bzl",
"current_mypy_version.txt",
"mypy.bzl",
"rules.bzl",
"current_mypy_version.txt",
"//mypy:distribution",
"//repositories:distribution",
"//templates:distribution",
Expand Down
34 changes: 11 additions & 23 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,26 @@ load("//repositories:deps.bzl", mypy_integration_deps = "deps")

mypy_integration_deps("//:current_mypy_version.txt")

########################
# GOLANG SUPPORT
########################
# buildifier is written in Go and hence needs rules_go to be built.
# See https://github.com/bazelbuild/rules_go for the up to date setup instructions.
http_archive(
name = "io_bazel_rules_go",
sha256 = "f99a9d76e972e0c8f935b2fe6d0d9d778f67c760c6d2400e23fc2e469016e2bd",
name = "buildifier_prebuilt",
sha256 = "0450069a99db3d414eff738dd8ad4c0969928af13dc8614adbd1c603a835caad",
strip_prefix = "buildifier-prebuilt-0.4.0",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.21.2/rules_go-v0.21.2.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/v0.21.2/rules_go-v0.21.2.tar.gz",
"http://github.com/keith/buildifier-prebuilt/archive/0.4.0.tar.gz",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@buildifier_prebuilt//:deps.bzl", "buildifier_prebuilt_deps")

go_rules_dependencies()
buildifier_prebuilt_deps()

go_register_toolchains()
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

http_archive(
name = "bazel_gazelle",
sha256 = "d8c45ee70ec39a57e7a05e5027c32b1576cc7f16d9dd37135b0eddde45cf1b10",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/bazel-gazelle/releases/download/v0.20.0/bazel-gazelle-v0.20.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.20.0/bazel-gazelle-v0.20.0.tar.gz",
],
)
bazel_skylib_workspace()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@buildifier_prebuilt//:defs.bzl", "buildifier_prebuilt_register_toolchains")

gazelle_dependencies()
buildifier_prebuilt_register_toolchains()

http_archive(
name = "com_google_protobuf",
Expand Down Expand Up @@ -75,9 +63,9 @@ http_archive(

http_archive(
name = "rules_pkg",
sha256 = "4ba8f4ab0ff85f2484287ab06c0d871dcb31cc54d439457d28fd4ae14b18450a",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.2.4/rules_pkg-0.2.4.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.2.4/rules_pkg-0.2.4.tar.gz",
],
sha256 = "4ba8f4ab0ff85f2484287ab06c0d871dcb31cc54d439457d28fd4ae14b18450a",
)
2 changes: 2 additions & 0 deletions examples/tools/linting/aspect.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"Shows how to hook up a linter"

load("@linting_system//:generator.bzl", "linting_aspect_generator")

lint = linting_aspect_generator(
Expand Down
18 changes: 6 additions & 12 deletions mypy.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"Public API"

load("@bazel_skylib//lib:shell.bzl", "shell")
load("@bazel_skylib//lib:sets.bzl", "sets")
load("//:rules.bzl", "MyPyStubsInfo")

MyPyAspectInfo = provider(
"TODO: documentation",
fields = {
"exe": "Used to pass the rule implementation built exe back to calling aspect.",
"out": "Used to pass the dummy output file back to calling aspect.",
Expand Down Expand Up @@ -31,7 +34,6 @@ DEFAULT_ATTRS = {
),
}


def _sources_to_cache_map_triples(srcs, is_aspect):
triples_as_flat_list = []
for f in srcs:
Expand All @@ -49,15 +51,12 @@ def _sources_to_cache_map_triples(srcs, is_aspect):
])
return triples_as_flat_list


def _is_external_dep(dep):
return dep.label.workspace_root.startswith("external/")


def _is_external_src(src_file):
return src_file.path.startswith("external/")


def _extract_srcs(srcs):
direct_src_files = []
for src in srcs:
Expand All @@ -66,15 +65,13 @@ def _extract_srcs(srcs):
direct_src_files.append(f)
return direct_src_files


def _extract_transitive_deps(deps):
transitive_deps = []
for dep in deps:
if MyPyStubsInfo not in dep and PyInfo in dep and not _is_external_dep(dep):
transitive_deps.append(dep[PyInfo].transitive_sources)
return transitive_deps


def _extract_stub_deps(deps):
# Need to add the .py files AND the .pyi files that are
# deps of the rule
Expand All @@ -87,21 +84,20 @@ def _extract_stub_deps(deps):
stub_files.append(src_f)
return stub_files


def _extract_imports(imports, label):
# NOTE: Bazel's implementation of this for py_binary, py_test is at
# src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java
mypypath_parts = []
for import_ in imports:
if import_.startswith("/"):
# buildifier: disable=print
print("ignoring invalid absolute path '{}'".format(import_))
elif import_ in ["", "."]:
mypypath_parts.append(label.package)
else:
mypypath_parts.append("{}/{}".format(label.package, import_))
return mypypath_parts


def _mypy_rule_impl(ctx, is_aspect = False):
base_rule = ctx
if is_aspect:
Expand Down Expand Up @@ -188,12 +184,11 @@ def _mypy_rule_impl(ctx, is_aspect = False):
if is_aspect:
return [
DefaultInfo(executable = exe, runfiles = runfiles),
MyPyAspectInfo(exe = exe, out = out)
MyPyAspectInfo(exe = exe, out = out),
]
return DefaultInfo(executable = exe, runfiles = runfiles)


def _mypy_aspect_impl(target, ctx):
def _mypy_aspect_impl(_, ctx):
if (ctx.rule.kind not in ["py_binary", "py_library", "py_test", "mypy_test"] or
ctx.label.workspace_root.startswith("external")):
return []
Expand Down Expand Up @@ -223,7 +218,6 @@ def _mypy_aspect_impl(target, ctx):
),
]


def _mypy_test_impl(ctx):
info = _mypy_rule_impl(ctx, is_aspect = False)
if not info:
Expand Down
3 changes: 2 additions & 1 deletion repositories/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ repository.

load(":py_repositories.bzl", "py_deps")

def deps(mypy_requirements_file, python_interpreter=None, python_interpreter_target=None, extra_pip_args=None):
def deps(mypy_requirements_file, python_interpreter = None, python_interpreter_target = None, extra_pip_args = None):
"""Pull in external dependencies needed by rules in this repo.
Pull in all dependencies needed to run rules in this
repository.
Expand Down
2 changes: 2 additions & 0 deletions repositories/py_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ Provides functions to pull the external Mypy package dependency.

load("@rules_python//python:pip.bzl", "pip_install")

# buildifier: disable=function-docstring-args
def py_deps(mypy_requirements_file, python_interpreter, python_interpreter_target, extra_pip_args):
"""Pull in external Python packages needed by py binaries in this repo.
Pull in all dependencies needed to build the Py binaries in this
repository. This function assumes the repositories imported by the macro
'repositories' in //repositories:repositories.bzl have been imported
Expand Down
7 changes: 2 additions & 5 deletions repositories/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"""Rules to load all dependencies of this project."""

load(
"@bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive",
"http_file",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# buildifier: disable=function-docstring
def repositories():
excludes = native.existing_rules().keys()

Expand Down
2 changes: 2 additions & 0 deletions rules.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"mypy_stubs rule"
MyPyStubsInfo = provider(
"TODO: docs",
fields = {
"srcs": ".pyi stub files",
},
Expand Down
18 changes: 12 additions & 6 deletions test/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package(default_testonly = 1)

load("//:mypy.bzl", "mypy_test")

package(default_testonly = 1)

py_library(
name = "empty_srcs_lib",
srcs = [],
Expand All @@ -22,13 +22,19 @@ py_library(

py_library(
name = "correct_imported_mypy_typings",
srcs = [":correct_generated_mypy_typings", "correct_imported_typings.py"],
srcs = [
"correct_imported_typings.py",
":correct_generated_mypy_typings",
],
srcs_version = "PY3",
)

py_library(
name = "broken_imported_mypy_typings",
srcs = [":broken_generated_mypy_typings", "broken_imported_typings.py"],
srcs = [
"broken_imported_typings.py",
":broken_generated_mypy_typings",
],
srcs_version = "PY3",
)

Expand Down Expand Up @@ -60,21 +66,21 @@ mypy_test(
genrule(
name = "correct_generated_mypy_typings",
outs = ["correct_generated.py"],
local = True,
cmd =
("echo -e '" +
"from typing import Callable\n\n" +
"def thrice(i: int, next_: Callable[[int], int]) -> int:\n" +
" return next_(next_(next_(i)))\n' > $@"),
local = True,
)

genrule(
name = "broken_generated_mypy_typings",
outs = ["broken_generated.py"],
local = True,
cmd =
("echo -e '" +
"from typing import Callable\n\n" +
"def thrice(i: int, next_: Callable[[int], int]) -> str:\n" +
" return next_(next_(next_(i)))\n' > $@"),
local = True,
)
4 changes: 2 additions & 2 deletions test/foo/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "foo",
srcs = ["bar.py"],
srcs_version = "PY3",
visibility = ["//test:__subpackages__"],
deps = [
"//test/types",
],
srcs_version = "PY3",
visibility = ["//test:__subpackages__"],
)
2 changes: 1 addition & 1 deletion test/types/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "types",
srcs = ["__init__.py"],
deps = [],
srcs_version = "PY3",
visibility = ["//test:__subpackages__"],
deps = [],
)

0 comments on commit 33b65d2

Please sign in to comment.