Skip to content

Commit

Permalink
feat(bzlmod): allow patches in archive_overrides (bazel-contrib#1665)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-french authored and jeromep-stripe committed Mar 22, 2024
1 parent 223c9d3 commit dcf2b9d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
11 changes: 11 additions & 0 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def _process_archive_override(archive_override_tag):
urls = archive_override_tag.urls,
sha256 = archive_override_tag.sha256,
strip_prefix = archive_override_tag.strip_prefix,
patches = archive_override_tag.patches,
patch_strip = archive_override_tag.patch_strip,
)

def _extension_metadata(module_ctx, *, root_module_direct_deps, root_module_direct_dev_deps):
Expand Down Expand Up @@ -393,6 +395,8 @@ def _go_deps_impl(module_ctx):
"urls": archive_override.urls,
"strip_prefix": archive_override.strip_prefix,
"sha256": archive_override.sha256,
"patches": _get_patches(path, archive_overrides),
"patch_args": _get_patch_args(path, archive_overrides),
})
else:
go_repository_args.update({
Expand Down Expand Up @@ -510,6 +514,13 @@ _archive_override_tag = tag_class(
SHA-256 sum of the downloaded archive. When set, Bazel will verify the archive
against this sum before extracting it.""",
),
"patches": attr.label_list(
doc = "A list of patches to apply to the repository *after* gazelle runs.",
),
"patch_strip": attr.int(
default = 0,
doc = "The number of leading path segments to be stripped from the file name in the patches.",
),
},
doc = "Override the default source location on a given Go module in this extension.",
)
Expand Down
4 changes: 4 additions & 0 deletions tests/bcr/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ go_deps.archive_override(
urls = [
"https://github.com/bazelbuild/buildtools/archive/ae8e3206e815d086269eb208b01f300639a4b194.tar.gz",
],
patch_strip = 1,
patches = [
"//patches:buildtools.patch",
],
strip_prefix = "buildtools-ae8e3206e815d086269eb208b01f300639a4b194",
path = "github.com/bazelbuild/buildtools",
sha256 = "05d7c3d2bd3cc0b02d15672fefa0d6be48c7aebe459c1c99dced7ac5e598508f",
Expand Down
24 changes: 16 additions & 8 deletions tests/bcr/MODULE.bazel.lock

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

13 changes: 13 additions & 0 deletions tests/bcr/patches/buildtools.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/labels/labels.go b/labels/labels.go
index 7564621..aba07c0 100644
--- a/labels/labels.go
+++ b/labels/labels.go
@@ -23,6 +23,8 @@ import (
"strings"
)

+const Patched = "hello"
+
// Label represents a Bazel target label.
type Label struct {
Repository string // Repository of the target, can be empty if the target belongs to the current repository
4 changes: 4 additions & 0 deletions tests/bcr/pkg/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ func TestArchiveOverrideUsed(t *testing.T) {
label := labels.Parse("@com_github_bazelbuild_buildtools//labels:labels")
require.NotEmpty(t, label)
}

func TestArchiveOverrideWithPatch(t *testing.T) {
require.Equal(t, labels.Patched, "hello")
}

0 comments on commit dcf2b9d

Please sign in to comment.