diff --git a/.bazelrc b/.bazelrc index 03b8a6c348..fe8510d9e2 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 diff --git a/docs/doc_helpers.bzl b/docs/doc_helpers.bzl index dbec0d5f88..edefd74903 100644 --- a/docs/doc_helpers.bzl +++ b/docs/doc_helpers.bzl @@ -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], ) diff --git a/go/private/extensions.bzl b/go/private/extensions.bzl index bcceaa7c3a..8900cebf23 100644 --- a/go/private/extensions.bzl +++ b/go/private/extensions.bzl @@ -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"), }, ) @@ -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, @@ -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 = { diff --git a/go/private/sdk.bzl b/go/private/sdk.bzl index 08c6e74c00..055fe260e7 100644 --- a/go/private/sdk.bzl +++ b/go/private/sdk.bzl @@ -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) @@ -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) @@ -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) @@ -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"), ), diff --git a/tests/bcr/BUILD.bazel b/tests/bcr/BUILD.bazel index d5c5f6f1fd..851dd60c0e 100644 --- a/tests/bcr/BUILD.bazel +++ b/tests/bcr/BUILD.bazel @@ -19,6 +19,11 @@ go_test( embed = [":lib"], ) +go_test( + name = "sdk_patch_test", + srcs = ["sdk_patch_test.go"], +) + go_library( name = "mockable", srcs = [ diff --git a/tests/bcr/MODULE.bazel b/tests/bcr/MODULE.bazel index 8cd19d052a..3d779fee1c 100644 --- a/tests/bcr/MODULE.bazel +++ b/tests/bcr/MODULE.bazel @@ -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") diff --git a/tests/bcr/sdk_patch_test.go b/tests/bcr/sdk_patch_test.go new file mode 100644 index 0000000000..f3c1a64f78 --- /dev/null +++ b/tests/bcr/sdk_patch_test.go @@ -0,0 +1,13 @@ +package lib + +import ( + "os" + + "testing" +) + +func TestName(t *testing.T) { + if os.SayHello != "Hello" { + t.Fail() + } +} diff --git a/tests/bcr/test_go_sdk.patch b/tests/bcr/test_go_sdk.patch new file mode 100644 index 0000000000..83beb83b5f --- /dev/null +++ b/tests/bcr/test_go_sdk.patch @@ -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 diff --git a/tests/core/go_download_sdk/go_download_sdk_test.go b/tests/core/go_download_sdk/go_download_sdk_test.go index 61bd05b89f..e823bfb09b 100644 --- a/tests/core/go_download_sdk/go_download_sdk_test.go +++ b/tests/core/go_download_sdk/go_download_sdk_test.go @@ -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"},