Skip to content

Commit b1d3aba

Browse files
authored
go_path: add include_transitive (bazel-contrib#2181)
add include_transitive so that only the packages in deps are included in the GoPath. When used with include_pkg, this allows to only sandbox the needed packages, and thus invalidate potentially less.
1 parent 8a2ba42 commit b1d3aba

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

go/core.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,17 @@ Attributes
863863
| included in the output directory. Files listed in the :param:`data` attribute |
864864
| for this rule will be included regardless of this attribute. |
865865
+----------------------------+-----------------------------+---------------------------------------+
866+
| :param:`include_pkg` | :type:`bool` | :value:`False` |
867+
+----------------------------+-----------------------------+---------------------------------------+
868+
| When true, a `pkg` subdirectory containing the compiled libraries will be created in the |
869+
| generated `GOPATH` containing compiled libraries. |
870+
+----------------------------+-----------------------------+---------------------------------------+
871+
| :param:`include_transitive`| :type:`bool` | :value:`True` |
872+
+----------------------------+-----------------------------+---------------------------------------+
873+
| When true, the transitive dependency graph will be included in the generated `GOPATH`. This is |
874+
| the default behaviour. When false, only the direct dependencies will be included in the |
875+
| generated `GOPATH`. |
876+
+----------------------------+-----------------------------+---------------------------------------+
866877

867878
Defines and stamping
868879
--------------------

go/private/tools/path.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def _go_path_impl(ctx):
3838
mode_to_archive = {}
3939
for mode, archives in mode_to_deps.items():
4040
direct = [a.data for a in archives]
41-
transitive = [a.transitive for a in archives]
41+
transitive = []
42+
if ctx.attr.include_transitive:
43+
transitive = [a.transitive for a in archives]
4244
mode_to_archive[mode] = depset(direct = direct, transitive = transitive)
4345

4446
# Collect sources and data files from archives. Merge archives into packages.
@@ -163,6 +165,7 @@ go_path = rule(
163165
),
164166
"include_data": attr.bool(default = True),
165167
"include_pkg": attr.bool(default = False),
168+
"include_transitive": attr.bool(default = True),
166169
"_go_path": attr.label(
167170
default = "@io_bazel_rules_go//go/tools/builders:go_path",
168171
executable = True,

tests/core/go_path/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ go_path(
2727
deps = ["//tests/core/go_path/pkg/lib:go_default_library"],
2828
)
2929

30+
go_path(
31+
name = "notransitive_path",
32+
testonly = True,
33+
include_transitive = False,
34+
mode = "copy",
35+
deps = ["//tests/core/go_path/pkg/lib:go_default_library"],
36+
)
37+
3038
go_test(
3139
name = "go_path_test",
3240
srcs = ["go_path_test.go"],
@@ -35,12 +43,14 @@ go_test(
3543
"-copy_path=$(location :copy_path)",
3644
"-link_path=tests/core/go_path/link_path", # can't use location; not a single file
3745
"-nodata_path=$(location :nodata_path)",
46+
"-notransitive_path=$(location :notransitive_path)",
3847
],
3948
data = [
4049
":archive_path",
4150
":copy_path",
4251
":link_path",
4352
":nodata_path",
53+
":notransitive_path",
4454
],
4555
deps = ["//go/tools/bazel:go_default_library"],
4656
rundir = ".",

tests/core/go_path/go_path_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/bazelbuild/rules_go/go/tools/bazel"
3030
)
3131

32-
var copyPath, linkPath, archivePath, nodataPath string
32+
var copyPath, linkPath, archivePath, nodataPath, notransitivePath string
3333

3434
var defaultMode = runtime.GOOS + "_" + runtime.GOARCH
3535

@@ -58,6 +58,7 @@ func TestMain(m *testing.M) {
5858
flag.StringVar(&linkPath, "link_path", "", "path to symlinked go_path")
5959
flag.StringVar(&archivePath, "archive_path", "", "path to archive go_path")
6060
flag.StringVar(&nodataPath, "nodata_path", "", "path to go_path without data")
61+
flag.StringVar(&notransitivePath, "notransitive_path", "", "path to go_path without transitive dependencies")
6162
flag.Parse()
6263
os.Exit(m.Run())
6364
}
@@ -133,6 +134,16 @@ func TestNoDataPath(t *testing.T) {
133134
checkPath(t, nodataPath, files)
134135
}
135136

137+
func TestNoTransitivePath(t *testing.T) {
138+
if notransitivePath == "" {
139+
t.Fatal("-notransitive_path not set")
140+
}
141+
files := []string{
142+
"-src/example.com/repo/pkg/lib/transitive/transitive.go",
143+
}
144+
checkPath(t, notransitivePath, files)
145+
}
146+
136147
// checkPath checks that dir contains a list of files. files is a list of
137148
// slash-separated paths relative to dir. Files that start with "-" should be
138149
// absent. Files that end with "/" should be directories.

tests/core/go_path/pkg/lib/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ go_library(
88
"data.txt",
99
"testdata/testdata.txt",
1010
],
11+
deps = [":transitive_lib"],
1112
importpath = "example.com/repo/pkg/lib",
1213
visibility = ["//visibility:public"],
1314
)
@@ -42,3 +43,10 @@ go_library(
4243
importmap = "example.com/repo/vendor/example.com/repo2",
4344
visibility = ["//visibility:public"],
4445
)
46+
47+
go_library(
48+
name = "transitive_lib",
49+
srcs = ["transitive.go"],
50+
importpath = "example.com/repo/pkg/lib/transitive",
51+
visibility = ["//visibility:public"],
52+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package transitive
2+
3+
var (
4+
_ = ""
5+
)

0 commit comments

Comments
 (0)