Skip to content

Commit

Permalink
[go_sdk download] allow patches to standard library
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-french committed Sep 14, 2023
1 parent 2e821f6 commit 43adf30
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ tasks:
# installed. It appears bazel is no longer in PATH on the host machines
# in this configuration either.
- "--test_tag_filters=-local"
# https://github.com/bazelbuild/stardoc/issues/112
- --incompatible_allow_tags_propagation
test_targets:
- "--"
- "//..."
Expand Down
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ build:ci --spawn_strategy=standalone
build:ci --genrule_strategy=standalone
test:ci --test_strategy=standalone

# https://github.com/bazelbuild/stardoc/issues/112
common --incompatible_allow_tags_propagation

# Incompatible flags to test in a dedicated CI pipeline.
build:incompatible --incompatible_load_proto_rules_from_bzl
build:incompatible --incompatible_enable_cc_toolchain_resolution
Expand Down
1 change: 1 addition & 0 deletions docs/doc_helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def stardoc_with_diff_test(
out = out_file.replace(".md", "-docgen.md"),
input = bzl_library_target + ".bzl",
rule_template = rule_template,
tags = ["no-sandbox"], # https://github.com/bazelbuild/stardoc/issues/112
deps = [bzl_library_target],
)

Expand Down
14 changes: 14 additions & 0 deletions go/private/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ _download_tag = tag_class(
),
"urls": attr.string_list(default = ["https://dl.google.com/go/{}"]),
"version": attr.string(),
"patches": attr.label_list(
doc = "A list of patches to apply to the SDK after downloading it",
),
"patch_strip": attr.int(
default = 0,
doc = "The number of leading path segments to be stripped from the file name in the patches.",
),
"strip_prefix": attr.string(default = "go"),
},
)
Expand Down Expand Up @@ -93,6 +100,8 @@ def _go_sdk_impl(ctx):
goarch = download_tag.goarch,
sdks = download_tag.sdks,
experiments = download_tag.experiments,
patches = download_tag.patches,
patch_args = _get_patch_args(download_tag.patch_strip),
urls = download_tag.urls,
version = download_tag.version,
strip_prefix = download_tag.strip_prefix,
Expand Down Expand Up @@ -184,6 +193,11 @@ def _left_pad_zero(index, length):
fail("index must be non-negative")
return ("0" * length + str(index))[-length:]

def _get_patch_args(patch_strip):
if patch_strip:
return ["-p{}".format(patch_strip)]
return []

go_sdk = module_extension(
implementation = _go_sdk_impl,
tag_classes = {
Expand Down
27 changes: 15 additions & 12 deletions go/private/sdk.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(
"//go/private:common.bzl",
"executable_path",
)
load(
"//go/private:nogo.bzl",
"go_register_nogo",
)
load(
"//go/private/skylib/lib:versions.bzl",
"versions",
)
load("//go/private:common.bzl", "executable_path")
load("//go/private:nogo.bzl", "go_register_nogo")
load("//go/private/skylib/lib:versions.bzl", "versions")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch")

MIN_SUPPORTED_VERSION = (1, 14, 0)

Expand Down Expand Up @@ -97,6 +89,8 @@ def _go_download_sdk_impl(ctx):
sdks_by_version = _parse_versions_json(data)

if not version:
if ctx.attr.patches:
fail("version must be specified when patches are specified")
highest_version = None
for v in sdks_by_version.keys():
pv = parse_version(v)
Expand All @@ -115,7 +109,9 @@ def _go_download_sdk_impl(ctx):
if platform not in sdks:
fail("unsupported platform {}".format(platform))
filename, sha256 = sdks[platform]

_remote_sdk(ctx, [url.format(filename) for url in ctx.attr.urls], ctx.attr.strip_prefix, sha256)
patch(ctx)

detected_version = _detect_sdk_version(ctx, ".")
_sdk_build_file(ctx, platform, detected_version, experiments = ctx.attr.experiments)
Expand Down Expand Up @@ -146,6 +142,13 @@ go_download_sdk_rule = repository_rule(
"urls": attr.string_list(default = ["https://dl.google.com/go/{}"]),
"version": attr.string(),
"strip_prefix": attr.string(default = "go"),
"patches": attr.label_list(
doc = "A list of patches to apply to the SDK after downloading it",
),
"patch_args": attr.string_list(
default = ["-p0"],
doc = "Arguments passed to the patch tool when applying patches.",
),
"_sdk_build_file": attr.label(
default = Label("//go/private:BUILD.sdk.bazel"),
),
Expand Down
5 changes: 5 additions & 0 deletions tests/bcr/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ go_test(
embed = [":lib"],
)

go_test(
name = "sdk_patch_test",
srcs = ["sdk_patch_test.go"],
)

go_library(
name = "mockable",
srcs = [
Expand Down
7 changes: 6 additions & 1 deletion tests/bcr/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ bazel_dep(name = "gazelle", version = "0.32.0")
bazel_dep(name = "protobuf", version = "3.19.6")

go_sdk = use_extension("@my_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.19.5")
go_sdk.download(
patch_strip = 1,
patches = [
"//:test_go_sdk.patch",
],
)

# Request an invalid SDK to verify that it isn't fetched since the first tag takes precedence.
go_sdk.host(version = "3.0.0")
Expand Down
13 changes: 13 additions & 0 deletions tests/bcr/sdk_patch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package lib

import (
"os"

"testing"
)

func TestName(t *testing.T) {
if os.SayHello != "Hello" {
t.Fail()
}
}
13 changes: 13 additions & 0 deletions tests/bcr/test_go_sdk.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/src/os/dir.go b/src/os/dir.go
index 5306bcb..d110a19 100644
--- a/src/os/dir.go
+++ b/src/os/dir.go
@@ -17,6 +17,8 @@ const (
readdirFileInfo
)

+const SayHello = "Hello"
+
// Readdir reads the contents of the directory associated with file and
// returns a slice of up to n FileInfo values, as would be returned
// by Lstat, in directory order. Subsequent calls on the same file will yield
3 changes: 3 additions & 0 deletions tests/core/go_download_sdk/go_download_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ go_download_sdk(
"linux_amd64": ("go1.16.linux-amd64.tar.gz", "013a489ebb3e24ef3d915abe5b94c3286c070dfe0818d5bca8108f1d6e8440d2"),
"windows_amd64": ("go1.16.windows-amd64.zip", "5cc88fa506b3d5c453c54c3ea218fc8dd05d7362ae1de15bb67986b72089ce93"),
},
patches = [
"//stdlib_cgo.patch",
],
)
`,
optToWantVersion: map[string]string{"": "go1.16"},
Expand Down

0 comments on commit 43adf30

Please sign in to comment.