Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .plzconfig
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,9 @@ Repeatable = true
Optional = true
Help = Build tags to pass to the Go compiler

[PluginConfig "experiment"]
Copy link
Contributor

@chrisnovakovic chrisnovakovic Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be more "Please-like" for the enabled experiments to be a list (i.e. a repeatable config entry) which is then concatenated into a string with commas in _goexperiment_env?

Optional = true
Help = Value to pass for GOEXPERIMENT

[Plugin "shell"]
Target = //plugins:shell
20 changes: 18 additions & 2 deletions build_defs/go.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ def _go_toolchain(name:str, srcs:list, labels:list=[], copy_cmd:str, visibility:
'link': f'{name}/pkg/tool/{CONFIG.HOSTOS}_{CONFIG.HOSTARCH}/link',
'cover': f'{name}/pkg/tool/{CONFIG.HOSTOS}_{CONFIG.HOSTARCH}/cover',
},
tools = {"CC": [CONFIG.GO.CC_TOOL]} if CONFIG.GO.CGO_ENABLED else {},
env = _goexperiment_env(),
tools = ({
"CC": [CONFIG.GO.CC_TOOL],
} if CONFIG.GO.CGO_ENABLED == "1" else {}) | _goexperiment_env(),
binary = True,
labels = labels,
visibility = visibility,
Expand Down Expand Up @@ -210,6 +213,7 @@ def go_stdlib(name:str, tags:list=CONFIG.GO.BUILD_TAGS, labels:list=[], visibili
"GOOS": CONFIG.OS,
"GOARCH": CONFIG.ARCH,
"GODEBUG": "installgoroot=all",
"GOEXPERIMENT": CONFIG.GO.EXPERIMENT,
},
labels = ["go_pkg_info", "go_stdlib", "go"] + labels,
output_is_complete = True,
Expand Down Expand Up @@ -293,6 +297,7 @@ def go_library(name:str, srcs:list, resources:list=[], asm_srcs:list=None, hdrs:
test_only = test_only,
deps = deps,
)
link_prefix, _, _ = _module.partition('@')
else:
# Dummy modinfo so we don't have to provide everything for binary modinfo actions.
modinfo = filegroup(
Expand All @@ -302,6 +307,7 @@ def go_library(name:str, srcs:list, resources:list=[], asm_srcs:list=None, hdrs:
requires = ["modinfo"],
test_only = test_only,
)
link_prefix = ''
deps += [modinfo]

# We could just depend on go_src for all our deps but that would mean bringing in srcs for targets outside this
Expand Down Expand Up @@ -493,6 +499,7 @@ def go_library(name:str, srcs:list, resources:list=[], asm_srcs:list=None, hdrs:
outs = outs,
cmd = cmds,
visibility = visibility,
env = _goexperiment_env(),
building_description = "Compiling...",
requires = ['go'],
provides = provides,
Expand All @@ -503,6 +510,12 @@ def go_library(name:str, srcs:list, resources:list=[], asm_srcs:list=None, hdrs:
exported_deps = exported_deps,
)


def _goexperiment_env():
"""Returns the GOEXPERIMENT env var, or None if there isn't one."""
return {"GOEXPERIMENT": CONFIG.GO.EXPERIMENT} if CONFIG.GO.EXPERIMENT else {}


def _generate_pkg_import_cfg_cmd(name, out, pkg_location, handle_basename_eq_dirname=False):
"""
Generates a .importconfig file for all .a files within a go pkg directory for use with
Expand Down Expand Up @@ -648,6 +661,7 @@ def go_binary(name:str, srcs:list=[], resources:list=None, asm_srcs:list=[], out
building_description="Linking...",
needs_transitive_deps=True,
binary=True,
env = _goexperiment_env(),
output_is_complete=True,
test_only=test_only,
tools=tools,
Expand Down Expand Up @@ -829,6 +843,7 @@ def go_test(name:str, srcs:list, resources:list=None, data:list|dict=None, deps:
env["GOCOVERDIR"] = "$TMP_DIR"
else:
env = {"GOCOVERDIR": "$TMP_DIR"}
env |= _goexperiment_env()

return build_rule(
name = name,
Expand Down Expand Up @@ -966,6 +981,7 @@ def go_benchmark(name:str, srcs:list, resources:list=None, data:list|dict=None,
visibility=visibility,
sandbox=sandbox,
build_timeout=timeout,
env = _goexperiment_env(),
requires=['go', 'test'],
labels=labels,
binary=True,
Expand Down Expand Up @@ -1475,7 +1491,7 @@ def go_module(name:str='', module:str, version:str='', download:str='', deps:lis
visibility = visibility,
licences = licences,
linker_flags = linker_flags,
env = env,
env = env | {"GOEXPERIMENT": CONFIG.GO.EXPERIMENT},
build_tags = build_tags,
labels = labels if binary else [],
)
Expand Down
2 changes: 1 addition & 1 deletion test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ genrule(
deps = exports,
visibility=["//test/..."],
tools = [CONFIG.E2E.PLEASE_TOOL],
)
)
7 changes: 7 additions & 0 deletions test/goexperiment/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
subinclude("//test/build_defs:e2e")

please_repo_e2e_test(
name = "goexperiment_test",
plz_command = "plz test",
repo = "test_repo",
)
10 changes: 10 additions & 0 deletions test/goexperiment/test_repo/.plzconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Parse]
BuildFileName = BUILD_FILE
BuildFileName = BUILD
PreloadSubincludes = ///go//build_defs:go

[Plugin "go"]
ImportPath = github.com/please-build/please-rules/root-test
Target = //plugins:go
Stdlib = //third_party/go:std
experiment = rangefunc
4 changes: 4 additions & 0 deletions test/goexperiment/test_repo/experiment/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go_test(
name = "experiment_test",
srcs = ["experiment_test.go"],
)
28 changes: 28 additions & 0 deletions test/goexperiment/test_repo/experiment/experiment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package experiment_test

import (
"iter"
"slices"
"testing"
)

func EverySecond[T any](x []T) iter.Seq[T] {
return func(yield func(T) bool) {
for i := 0; i < len(x); i += 2 {
if !yield(x[i]) {
return
}
}
}
}

func TestRangefunc(t *testing.T) {
x := []int{1, 2, 3, 4, 5}
y := []int{}
for it := range EverySecond(x) {
y = append(y, it)
}
if !slices.Equal(y, []int{1, 3, 5}) {
t.Errorf("unexpected y: %v", y)
}
}
1 change: 1 addition & 0 deletions test/goexperiment/test_repo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module github.com/please-build/please-rules/goexperiment-test
4 changes: 4 additions & 0 deletions test/goexperiment/test_repo/plugins/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugin_repo(
name = "go",
revision = "master",
)
3 changes: 3 additions & 0 deletions test/goexperiment/test_repo/third_party/go/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go_stdlib(
name = "std",
)