Skip to content

Commit

Permalink
Fix and expand reproducible usage in extensions (bazel-contrib#1763)
Browse files Browse the repository at this point in the history
* Fix and expand `reproducible` usage in extensions

* Update MODULE.bazel

* Update BUILD.bazel

* Use proper bazel_features to work around rule_go limitation

* Update rules_go

* Downgrade Bzlmod
  • Loading branch information
fmeum authored Apr 2, 2024
1 parent 8d409f0 commit 34bea0a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 30 deletions.
18 changes: 9 additions & 9 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tasks:
ubuntu2204:
name: Ubuntu 22.04 with WORKSPACE
platform: ubuntu2204
bazel: 7.0.2
bazel: 7.1.1
build_flags:
- "--noexperimental_enable_bzlmod"
test_flags:
Expand All @@ -24,7 +24,7 @@ tasks:
ubuntu2204_bzlmod:
name: Ubuntu 22.04 with Bzlmod
platform: ubuntu2204
bazel: 7.0.2
bazel: 7.1.1
build_targets:
- "..."
run_targets:
Expand Down Expand Up @@ -84,7 +84,7 @@ tasks:
macos_arm64_bzlmod:
name: Mac OS Arm 64 with Bzlmod
platform: macos_arm64
bazel: 7.0.2
bazel: 7.1.1
build_targets:
- "..."
test_targets:
Expand All @@ -94,7 +94,7 @@ tasks:
macos_arm64:
name: Mac OS Arm 64 with WORKSPACE
platform: macos_arm64
bazel: 7.0.2
bazel: 7.1.1
build_flags:
- "--noexperimental_enable_bzlmod"
test_flags:
Expand All @@ -106,7 +106,7 @@ tasks:
macos_bzlmod:
name: Mac OS with Bzlmod
platform: macos
bazel: 7.0.2
bazel: 7.1.1
build_targets:
- "..."
test_targets:
Expand All @@ -116,7 +116,7 @@ tasks:
macos:
name: Mac OS with WORKSPACE
platform: macos
bazel: 7.0.2
bazel: 7.1.1
build_flags:
- "--noexperimental_enable_bzlmod"
test_flags:
Expand All @@ -128,7 +128,7 @@ tasks:
windows_bzlmod:
name: Windows with Bzlmod
platform: windows
bazel: 7.0.2
bazel: 7.1.1
build_targets:
- "--"
- "..."
Expand All @@ -152,7 +152,7 @@ tasks:
windows:
name: Windows with WORKSPACE
platform: windows
bazel: 7.0.2
bazel: 7.1.1
build_flags:
- "--noexperimental_enable_bzlmod"
test_flags:
Expand All @@ -179,7 +179,7 @@ tasks:
incompatible:
name: Ubuntu 22.04 with WORKSPACE and --config=incompatible
platform: ubuntu2204
bazel: 7.0.2
bazel: 7.1.1
build_flags:
- "--noexperimental_enable_bzlmod"
- "--config=incompatible"
Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.2
7.1.1
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module(
repo_name = "bazel_gazelle",
)

bazel_dep(name = "bazel_features", version = "1.9.0")
bazel_dep(name = "bazel_features", version = "1.9.1")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "protobuf", version = "3.19.6", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_go", version = "0.44.0", repo_name = "io_bazel_rules_go")
Expand Down
20 changes: 16 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,32 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_go",
integrity = "sha256-fHbWI2so/2laoozzX5XeMXqUcv0fsUrHl8m/aE8Js3w=",
sha256 = "80a98277ad1311dacd837f9b16db62887702e9f1d1c4c9f796d0121a46c8e184",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.44.2/rules_go-v0.44.2.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.44.2/rules_go-v0.44.2.zip",
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.46.0/rules_go-v0.46.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.46.0/rules_go-v0.46.0.zip",
],
)

# TODO: The rules_go bazel_features shim doesn't provide targets for .bzl files.
http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

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

go_rules_dependencies()

go_register_toolchains(
nogo = "@bazel_gazelle//:nogo",
version = "1.21.3",
version = "1.22.0",
)

load("//:deps.bzl", "gazelle_dependencies")
Expand Down
1 change: 1 addition & 0 deletions internal/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ bzl_library(
name = "utils",
srcs = ["utils.bzl"],
visibility = ["//:__subpackages__"],
deps = ["@bazel_features//:features"],
)

bzl_library(
Expand Down
17 changes: 3 additions & 14 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ load(":semver.bzl", "semver")
load(
":utils.bzl",
"drop_nones",
"extension_metadata",
"format_rule_call",
"get_directive_value",
"with_replaced_or_new_fields",
)
load("@bazel_features//:features.bzl", "bazel_features")

visibility("//")

Expand Down Expand Up @@ -244,14 +244,6 @@ def _process_archive_override(archive_override_tag):
patch_strip = archive_override_tag.patch_strip,
)

def _extension_metadata(module_ctx, *, root_module_direct_deps, root_module_direct_dev_deps):
if not hasattr(module_ctx, "extension_metadata"):
return None
return module_ctx.extension_metadata(
root_module_direct_deps = root_module_direct_deps,
root_module_direct_dev_deps = root_module_direct_dev_deps,
)

def _go_repository_config_impl(ctx):
repos = []
for name, importpath in sorted(ctx.attr.importpaths.items()):
Expand Down Expand Up @@ -569,10 +561,7 @@ def _go_deps_impl(module_ctx):
dep_files = dep_files,
)

metadata_kwargs = {}
if bazel_features.external_deps.extension_metadata_has_reproducible:
metadata_kwargs["reproducible"] = True
return _extension_metadata(
return extension_metadata(
module_ctx,
root_module_direct_deps = root_module_direct_deps.keys(),
# If a Go module appears as both a dev and a non-dev dependency, it has to be imported as a
Expand All @@ -582,7 +571,7 @@ def _go_deps_impl(module_ctx):
for repo_name in root_module_direct_dev_deps.keys()
if repo_name not in root_module_direct_deps
}.keys(),
**metadata_kwargs
reproducible = True,
)

def _get_sum_from_module(path, module, sums):
Expand Down
7 changes: 6 additions & 1 deletion internal/bzlmod/non_module_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ load(
"//internal:is_bazel_module.bzl",
"is_bazel_module",
)
load(
"//internal/bzlmod:utils.bzl",
"extension_metadata",
)
load(
"@go_host_compatible_sdk_label//:defs.bzl",
"HOST_COMPATIBLE_SDK",
Expand All @@ -35,7 +39,7 @@ load(

visibility("//")

def _non_module_deps_impl(_):
def _non_module_deps_impl(module_ctx):
go_repository_cache(
name = "bazel_gazelle_go_repository_cache",
# Label.workspace_name is always a canonical name, so use a canonical label.
Expand All @@ -50,6 +54,7 @@ def _non_module_deps_impl(_):
name = "bazel_gazelle_is_bazel_module",
is_bazel_module = True,
)
return extension_metadata(module_ctx, reproducible = True)

non_module_deps = module_extension(
_non_module_deps_impl,
Expand Down
19 changes: 19 additions & 0 deletions internal/bzlmod/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_features//:features.bzl", "bazel_features")

visibility([
"//tests/bzlmod/...",
])
Expand Down Expand Up @@ -116,3 +118,20 @@ def with_replaced_or_new_fields(_struct, **replacements):
new_struct_assignments[key] = value

return struct(**new_struct_assignments)

def extension_metadata(
module_ctx,
*,
root_module_direct_deps = None,
root_module_direct_dev_deps = None,
reproducible = False):
if not hasattr(module_ctx, "extension_metadata"):
return None
metadata_kwargs = {}
if bazel_features.external_deps.extension_metadata_has_reproducible:
metadata_kwargs["reproducible"] = reproducible
return module_ctx.extension_metadata(
root_module_direct_deps = root_module_direct_deps,
root_module_direct_dev_deps = root_module_direct_dev_deps,
**metadata_kwargs
)

0 comments on commit 34bea0a

Please sign in to comment.