Skip to content

Commit

Permalink
Ensure go versions are synced between MODULE.bazel and go.mod.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemnmez committed Jan 24, 2025
1 parent ab388dc commit 0056497
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 194 deletions.
27 changes: 4 additions & 23 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
load("//bzl:rules.bzl", "bazel_lint")
load("//go/cmd/version_sync:go_version_sync.bzl", "go_version_sync")
load("//js:rules.bzl", "copy_to_bin", "js_library")
load("//ts:rules.bzl", "ts_config")

Expand Down Expand Up @@ -111,29 +112,8 @@ bin.renovate_config_validator_test(

npm_link_all_packages(name = "node_modules")

native_test(
name = "go_versions_synced",
size = "small",
src = "//go/cmd/version_sync",
out = "version_sync_test.o",
data = [
"MODULE.bazel",
"go.mod",
],
)

sh_binary(
name = "go_versions_synced.fix",
srcs = ["fix_go_version_sync.sh"],
data = [
"//go/cmd/version_sync",
],
env = {
"VERSION_SYNC": "$(rlocationpath //go/cmd/version_sync)",
},
deps = [
"@bazel_tools//tools/bash/runfiles",
],
go_version_sync(
name = "go_version_sync",
)

js_library(
Expand Down Expand Up @@ -246,6 +226,7 @@ py_venv(
"//project/cultist/gen/testing",
"//py",
"//py/ci/postUpgrade:postUpgrade_bin",
"//py/copy_to_workspace:copy_to_workspace_bin",
"//py/devtools",
"//py/devtools/prep",
"//py/devtools/prep:prep_bin",
Expand Down
5 changes: 4 additions & 1 deletion MODULE.bazel
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ bazel_dep(name = "bazel_features", version = "1.23.0")
bazel_dep(name = "rules_go", version = "0.52.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.41.0", repo_name = "bazel_gazelle")

GO_VERSION = "1.23.5"

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.23.5")
go_sdk.download(version = GO_VERSION)
use_repo(
go_sdk,
"go_toolchains",
Expand All @@ -149,6 +151,7 @@ use_repo(
"com_github_aws_aws_sdk_go_v2_service_s3",
"com_github_bazelbuild_bazel_watcher",
"com_github_bazelbuild_buildtools",
"com_github_blang_semver_v4",
"com_github_go_delve_delve",
"com_github_golang_protobuf",
"com_github_google_go_containerregistry",
Expand Down
14 changes: 14 additions & 0 deletions bzl/binary_with_args/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("//bzl:rules.bzl", "bazel_lint")

exports_files(
["binary_with_args.sh"],
visibility = ["//:__subpackages__"],
)

bazel_lint(
name = "bazel_lint",
srcs = [
"BUILD.bazel",
"binary_with_args.bzl",
],
)
15 changes: 15 additions & 0 deletions bzl/binary_with_args/binary_with_args.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def binary_with_args(name, binary, args, data = [], **kwargs):
native.sh_binary(
name = name,
srcs = ["//bzl/binary_with_args:binary_with_args.sh"],
data = [
binary,
] + data,
args = [
"$(rlocationpath " + binary + ")",
] + args,
deps = [
"@bazel_tools//tools/bash/runfiles",
],
**kwargs
)
18 changes: 18 additions & 0 deletions bzl/binary_with_args/binary_with_args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
# shellcheck disable=SC1090
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

BINARY="$(rlocation $1)"
shift

$BINARY $@
25 changes: 25 additions & 0 deletions bzl/golden_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@aspect_bazel_lib//lib:diff_test.bzl", "diff_test")
load("//py/copy_to_workspace:copy_to_workspace.bzl", "copy_to_workspace")

def golden_test(
name,
src,
golden, # must be a file in this package.
**kwargs):
diff_test(
name = name,
file1 = src,
file2 = golden,
**kwargs
)

package = native.package_name()

if package != "":
package += "/"

copy_to_workspace(
name = name + ".fix",
src = golden,
dst = package + src,
)
1 change: 1 addition & 0 deletions go.mod
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/bazelbuild/bazel-watcher v0.25.3
github.com/bazelbuild/buildtools v0.0.0-20240918101019-be1c24cc9a44
github.com/bazelbuild/rules_go v0.52.0
github.com/blang/semver/v4 v4.0.0
github.com/go-delve/delve v1.24.0
github.com/golang/protobuf v1.5.4
github.com/google/go-containerregistry v0.20.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go/cmd/version_sync/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
srcs = ["main.go"],
importpath = "github.com/zemn-me/monorepo/go/cmd/version_sync",
visibility = ["//visibility:private"],
deps = ["@com_github_blang_semver_v4//:semver"],
)

bazel_lint(
Expand Down
43 changes: 43 additions & 0 deletions go/cmd/version_sync/go_version_sync.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
load("//bzl:golden_test.bzl", "golden_test")


def go_version_sync(name):
native.genrule(
name = name + "_gen_goldens",
tools = [
"//go/cmd/version_sync"
],
cmd_bash = """
$(execpath //go/cmd/version_sync) \\
-go-mod $(rootpath go.mod) \\
-module-bazel $(rootpath MODULE.bazel) \\
-output-go-mod $(location go.mod.golden) \\
-output-module-bazel $(location MODULE.bazel.golden) \\
-fix
""",
srcs = [
"MODULE.bazel",
"go.mod"
],
outs = ["go.mod.golden", "MODULE.bazel.golden"],
)

golden_test(
name = name + "_go_mod_test",
src = "go.mod",
golden = "go.mod.golden"
)

golden_test(
name = name + "_bazel_mod_test",
src = "MODULE.bazel",
golden = "MODULE.bazel.golden"
)

native.test_suite(
name = name,
tests = [
name + "_go_mod_test",
name + "_bazel_mod_test"
]
)
Loading

0 comments on commit 0056497

Please sign in to comment.