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 d435a33
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 13 deletions.
1 change: 1 addition & 0 deletions go/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ bzl_library(
"//go/private:nogo",
"//go/private:platforms",
"//go/private/skylib/lib:versions",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
],
)

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
8 changes: 7 additions & 1 deletion tests/bcr/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ 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(
version = "1.21.1",
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
44 changes: 44 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 @@ -58,6 +58,7 @@ func Test(t *testing.T) {
desc, rule string
optToWantVersion map[string]string
fetchOnly string
patchContent string
}{
{
desc: "version",
Expand Down Expand Up @@ -89,6 +90,44 @@ go_download_sdk(
`,
optToWantVersion: map[string]string{"": "go1.16"},
},
{
desc: "custom_archives_with_patch",
rule: `
load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk")
go_download_sdk(
name = "go_sdk",
sdks = {
"darwin_amd64": ("go1.16.darwin-amd64.tar.gz", "6000a9522975d116bf76044967d7e69e04e982e9625330d9a539a8b45395f9a8"),
"darwin_arm64": ("go1.16.darwin-arm64.tar.gz", "4dac57c00168d30bbd02d95131d5de9ca88e04f2c5a29a404576f30ae9b54810"),
"linux_amd64": ("go1.16.linux-amd64.tar.gz", "013a489ebb3e24ef3d915abe5b94c3286c070dfe0818d5bca8108f1d6e8440d2"),
"windows_amd64": ("go1.16.windows-amd64.zip", "5cc88fa506b3d5c453c54c3ea218fc8dd05d7362ae1de15bb67986b72089ce93"),
},
patch_args = ["-p1"],
patches = [
"//:test.patch",
],
)
`,
optToWantVersion: map[string]string{"": "go-test"},
patchContent: `diff --git a/src/runtime/extern.go b/src/runtime/extern.go
index dacdf4f..1b53d0d 100644
--- a/src/runtime/extern.go
+++ b/src/runtime/extern.go
@@ -244,7 +244,7 @@ func GOROOT() string {
// Version returns the Go tree's version string.
// It is either the commit hash and date at the time of the build or,
// when possible, a release tag like "go1.3".
func Version() string {
- return sys.TheVersion
+ return "go-test"
}
// GOOS is the running program's operating system target:
// one of darwin, freebsd, linux, and so on.
// To view possible combinations of GOOS and GOARCH, run "go tool dist list".
`,
},
{
desc: "multiple_sdks",
rule: `
Expand Down Expand Up @@ -154,6 +193,11 @@ go_register_toolchains()
if err := ioutil.WriteFile("WORKSPACE", buf.Bytes(), 0666); err != nil {
t.Fatal(err)
}
if test.patchContent != "" {
if err := ioutil.WriteFile("test.patch", []byte(test.patchContent), 0666); err != nil {
t.Fatal(err)
}
}
defer func() {
if err := ioutil.WriteFile("WORKSPACE", origWorkspaceData, 0666); err != nil {
t.Errorf("error restoring WORKSPACE: %v", err)
Expand Down

0 comments on commit d435a33

Please sign in to comment.