Skip to content

Commit 9ebc93c

Browse files
authored
[go_sdk download] allow patches to standard library (#3684)
1 parent f03a723 commit 9ebc93c

File tree

10 files changed

+207
-13
lines changed

10 files changed

+207
-13
lines changed

docs/doc_helpers.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")
216
load("@bazel_skylib//rules:write_file.bzl", "write_file")
317
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")

go/extensions.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
load("//go/private:extensions.bzl", _go_sdk = "go_sdk")
216

317
go_sdk = _go_sdk

go/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ bzl_library(
107107
"//go/private:nogo",
108108
"//go/private:platforms",
109109
"//go/private/skylib/lib:versions",
110+
"@bazel_tools//tools/build_defs/repo:utils.bzl",
110111
],
111112
)
112113

go/private/extensions.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
load("//go/private:sdk.bzl", "detect_host_platform", "go_download_sdk_rule", "go_host_sdk_rule", "go_multiple_toolchains")
216
load("//go/private:repositories.bzl", "go_rules_dependencies")
317

@@ -31,6 +45,13 @@ _download_tag = tag_class(
3145
),
3246
"urls": attr.string_list(default = ["https://dl.google.com/go/{}"]),
3347
"version": attr.string(),
48+
"patches": attr.label_list(
49+
doc = "A list of patches to apply to the SDK after downloading it",
50+
),
51+
"patch_strip": attr.int(
52+
default = 0,
53+
doc = "The number of leading path segments to be stripped from the file name in the patches.",
54+
),
3455
"strip_prefix": attr.string(default = "go"),
3556
},
3657
)
@@ -93,6 +114,8 @@ def _go_sdk_impl(ctx):
93114
goarch = download_tag.goarch,
94115
sdks = download_tag.sdks,
95116
experiments = download_tag.experiments,
117+
patches = download_tag.patches,
118+
patch_strip = download_tag.patch_strip,
96119
urls = download_tag.urls,
97120
version = download_tag.version,
98121
strip_prefix = download_tag.strip_prefix,

go/private/sdk.bzl

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load(
16-
"//go/private:common.bzl",
17-
"executable_path",
18-
)
19-
load(
20-
"//go/private:nogo.bzl",
21-
"go_register_nogo",
22-
)
23-
load(
24-
"//go/private/skylib/lib:versions.bzl",
25-
"versions",
26-
)
15+
load("//go/private:common.bzl", "executable_path")
16+
load("//go/private:nogo.bzl", "go_register_nogo")
17+
load("//go/private/skylib/lib:versions.bzl", "versions")
18+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch")
2719

2820
MIN_SUPPORTED_VERSION = (1, 14, 0)
2921

@@ -75,6 +67,10 @@ def _go_download_sdk_impl(ctx):
7567
version = ctx.attr.version
7668
sdks = ctx.attr.sdks
7769

70+
if not version:
71+
if ctx.attr.patches:
72+
fail("a single version must be specified to apply patches")
73+
7874
if not sdks:
7975
# If sdks was unspecified, download a full list of files.
8076
# If version was unspecified, pick the latest version.
@@ -115,7 +111,9 @@ def _go_download_sdk_impl(ctx):
115111
if platform not in sdks:
116112
fail("unsupported platform {}".format(platform))
117113
filename, sha256 = sdks[platform]
114+
118115
_remote_sdk(ctx, [url.format(filename) for url in ctx.attr.urls], ctx.attr.strip_prefix, sha256)
116+
patch(ctx, patch_args = _get_patch_args(ctx.attr.patch_strip))
119117

120118
detected_version = _detect_sdk_version(ctx, ".")
121119
_sdk_build_file(ctx, platform, detected_version, experiments = ctx.attr.experiments)
@@ -146,6 +144,13 @@ go_download_sdk_rule = repository_rule(
146144
"urls": attr.string_list(default = ["https://dl.google.com/go/{}"]),
147145
"version": attr.string(),
148146
"strip_prefix": attr.string(default = "go"),
147+
"patches": attr.label_list(
148+
doc = "A list of patches to apply to the SDK after downloading it",
149+
),
150+
"patch_strip": attr.int(
151+
default = 0,
152+
doc = "The number of leading path segments to be stripped from the file name in the patches.",
153+
),
149154
"_sdk_build_file": attr.label(
150155
default = Label("//go/private:BUILD.sdk.bazel"),
151156
),
@@ -175,6 +180,11 @@ def _to_constant_name(s):
175180
# Prefix with _ as identifiers are not allowed to start with numbers.
176181
return "_" + "".join([c if c.isalnum() else "_" for c in s.elems()]).upper()
177182

183+
def _get_patch_args(patch_strip):
184+
if patch_strip:
185+
return ["-p{}".format(patch_strip)]
186+
return []
187+
178188
def go_toolchains_single_definition(ctx, *, prefix, goos, goarch, sdk_repo, sdk_type, sdk_version):
179189
if not goos and not goarch:
180190
goos, goarch = detect_host_platform(ctx)

tests/bcr/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ go_test(
1919
embed = [":lib"],
2020
)
2121

22+
go_test(
23+
name = "sdk_patch_test",
24+
srcs = ["sdk_patch_test.go"],
25+
)
26+
2227
go_library(
2328
name = "mockable",
2429
srcs = [

tests/bcr/MODULE.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ bazel_dep(name = "gazelle", version = "0.32.0")
2222
bazel_dep(name = "protobuf", version = "3.19.6")
2323

2424
go_sdk = use_extension("@my_rules_go//go:extensions.bzl", "go_sdk")
25-
go_sdk.download(version = "1.19.5")
25+
go_sdk.download(
26+
version = "1.21.1",
27+
patch_strip = 1,
28+
patches = [
29+
"//:test_go_sdk.patch",
30+
],
31+
)
2632

2733
# Request an invalid SDK to verify that it isn't fetched since the first tag takes precedence.
2834
go_sdk.host(version = "3.0.0")

tests/bcr/sdk_patch_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2019 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package lib
16+
17+
import (
18+
"os"
19+
20+
"testing"
21+
)
22+
23+
func TestName(t *testing.T) {
24+
if os.SayHello != "Hello" {
25+
t.Fail()
26+
}
27+
}

tests/bcr/test_go_sdk.patch

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/src/os/dir.go b/src/os/dir.go
2+
index 5306bcb..d110a19 100644
3+
--- a/src/os/dir.go
4+
+++ b/src/os/dir.go
5+
@@ -17,6 +17,8 @@ const (
6+
readdirFileInfo
7+
)
8+
9+
+const SayHello = "Hello"
10+
+
11+
// Readdir reads the contents of the directory associated with file and
12+
// returns a slice of up to n FileInfo values, as would be returned
13+
// by Lstat, in directory order. Subsequent calls on the same file will yield

tests/core/go_download_sdk/go_download_sdk_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ go_test(
3333
srcs = ["version_test.go"],
3434
)
3535
36+
go_test(
37+
name = "patch_test",
38+
srcs = ["patch_test.go"],
39+
)
40+
3641
-- version_test.go --
3742
package version_test
3843
@@ -49,6 +54,19 @@ func Test(t *testing.T) {
4954
t.Errorf("got version %q; want %q", v, *want)
5055
}
5156
}
57+
-- patch_test.go --
58+
package version_test
59+
60+
import (
61+
"os"
62+
"testing"
63+
)
64+
65+
func Test(t *testing.T) {
66+
if v := os.SayHello; v != "Hello"{
67+
t.Errorf("got version %q; want \"Hello\"", v)
68+
}
69+
}
5270
`,
5371
})
5472
}
@@ -185,3 +203,66 @@ go_register_toolchains()
185203
})
186204
}
187205
}
206+
207+
func TestPatch(t *testing.T) {
208+
origWorkspaceData, err := ioutil.ReadFile("WORKSPACE")
209+
if err != nil {
210+
t.Fatal(err)
211+
}
212+
213+
i := bytes.Index(origWorkspaceData, []byte("go_rules_dependencies()"))
214+
if i < 0 {
215+
t.Fatal("could not find call to go_rules_dependencies()")
216+
}
217+
218+
buf := &bytes.Buffer{}
219+
buf.Write(origWorkspaceData[:i])
220+
buf.WriteString(`
221+
load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk")
222+
223+
go_download_sdk(
224+
name = "go_sdk_patched",
225+
version = "1.21.1",
226+
patch_strip = 1,
227+
patches = ["//:test.patch"],
228+
)
229+
230+
go_rules_dependencies()
231+
232+
go_register_toolchains()
233+
`)
234+
if err := ioutil.WriteFile("WORKSPACE", buf.Bytes(), 0666); err != nil {
235+
t.Fatal(err)
236+
}
237+
238+
patchContent := []byte(`diff --git a/src/os/dir.go b/src/os/dir.go
239+
index 5306bcb..d110a19 100644
240+
--- a/src/os/dir.go
241+
+++ b/src/os/dir.go
242+
@@ -17,6 +17,8 @@ const (
243+
readdirFileInfo
244+
)
245+
246+
+const SayHello = "Hello"
247+
+
248+
// Readdir reads the contents of the directory associated with file and
249+
// returns a slice of up to n FileInfo values, as would be returned
250+
// by Lstat, in directory order. Subsequent calls on the same file will yield
251+
`)
252+
253+
if err := ioutil.WriteFile("test.patch", patchContent, 0666); err != nil {
254+
t.Fatal(err)
255+
}
256+
defer func() {
257+
if err := ioutil.WriteFile("WORKSPACE", origWorkspaceData, 0666); err != nil {
258+
t.Errorf("error restoring WORKSPACE: %v", err)
259+
}
260+
}()
261+
262+
if err := bazel_testing.RunBazel(
263+
"test",
264+
"//:patch_test",
265+
); err != nil {
266+
t.Fatal(err)
267+
}
268+
}

0 commit comments

Comments
 (0)