Skip to content

Commit 384d290

Browse files
author
Jay Conrod
authored
go_test: pass cdeps files to linker after split test recompilation (bazel-contrib#2625)
When a test contains both internal and external files, and the external files indirectly import the library under test, some package need to be recompiled so that the packages that import the library under test are compiled with the internal test archive instead. This was implemented in bazel-contrib#2579. That change caused a regression: the recompiled archives didn't include input files from cdeps dependencies. This change adds a new GoArchiveData field to track direct dependencies. That can be used to rebuild a depset of all the needed files. Fixes bazel-contrib#2622
1 parent bacfbec commit 384d290

File tree

11 files changed

+103
-1
lines changed

11 files changed

+103
-1
lines changed

go/private/actions/archive.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def emit_archive(go, source = None, _recompile_suffix = ""):
167167
file = out_lib,
168168
export_file = out_export,
169169
data_files = as_tuple(data_files),
170+
_cgo_deps = as_tuple(cgo_deps),
170171
)
171172
x_defs = dict(source.x_defs)
172173
for a in direct:

go/private/rules/test.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def _recompile_external_deps(go, external_source, internal_archive, library_labe
422422
libs = depset(direct = [arc_data.file], transitive = [a.libs for a in deps]),
423423
transitive = depset(direct = [arc_data], transitive = [a.transitive for a in deps]),
424424
x_defs = source.x_defs,
425-
cgo_deps = depset(), # deprecated, not used
425+
cgo_deps = depset(direct = arc_data._cgo_deps, transitive = [a.cgo_deps for a in deps]),
426426
cgo_exports = depset(direct = list(source.cgo_exports), transitive = [a.cgo_exports for a in deps]),
427427
runfiles = source.runfiles,
428428
mode = go.mode,

tests/core/cgo/BUILD.bazel

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,43 @@ cc_library(
225225
name = "cgo_link_dep",
226226
srcs = ["cgo_link_dep.c"],
227227
)
228+
229+
go_test(
230+
name = "split_import_test",
231+
srcs = [
232+
"split_import_i_test.go",
233+
"split_import_x_test.go",
234+
],
235+
embed = [":split_import_a"],
236+
deps = [":split_import_b"],
237+
)
238+
239+
go_library(
240+
name = "split_import_a",
241+
srcs = ["split_import_a.go"],
242+
importpath = "github.com/bazelbuild/rules_go/tests/core/cgo/split_import/a",
243+
)
244+
245+
go_library(
246+
name = "split_import_b",
247+
srcs = ["split_import_b.go"],
248+
importpath = "github.com/bazelbuild/rules_go/tests/core/cgo/split_import/b",
249+
deps = [
250+
":split_import_a",
251+
":split_import_cgo",
252+
],
253+
)
254+
255+
go_library(
256+
name = "split_import_cgo",
257+
srcs = ["split_import_cgo.go"],
258+
cdeps = [":split_import_c"],
259+
cgo = True,
260+
importpath = "github.com/bazelbuild/rules_go/tests/core/cgo/split_import/cgo",
261+
)
262+
263+
cc_library(
264+
name = "split_import_c",
265+
srcs = ["split_import_c.c"],
266+
hdrs = ["split_import_c.h"],
267+
)

tests/core/cgo/README.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.. _#2067: https://github.com/bazelbuild/rules_go/issues/2067
2+
.. _#2622: https://github.com/bazelbuild/rules_go/issues/2622
23

34
Basic cgo functionality
45
=======================
@@ -52,3 +53,10 @@ cdeps_link_test
5253

5354
Checks that libraries in ``cdeps`` are linked into the generated ``_cgo_.o``
5455
executable used to produce ``_cgo_imports.go``. Verifies `#2067`_.
56+
57+
split_import_test
58+
-----------------
59+
60+
Checks that when a package with ``cdeps`` is recompiled due to a split test,
61+
the input files from ``cdeps`` are included in the recompilation and are passed
62+
to the linker. Verifies `#2622`_.

tests/core/cgo/split_import_a.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package a
2+
3+
func Answer() int { return 42 }

tests/core/cgo/split_import_b.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package b
2+
3+
import (
4+
"github.com/bazelbuild/rules_go/tests/core/cgo/split_import/a"
5+
"github.com/bazelbuild/rules_go/tests/core/cgo/split_import/cgo"
6+
)
7+
8+
func HalfAnswer() int {
9+
return cgo.Half(a.Answer())
10+
}

tests/core/cgo/split_import_c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int half(int x) { return x/2; }
2+

tests/core/cgo/split_import_c.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef split_import_c
2+
#define split_import_c
3+
4+
int half(int);
5+
6+
#endif

tests/core/cgo/split_import_cgo.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cgo
2+
3+
/*
4+
#include "tests/core/cgo/split_import_c.h"
5+
*/
6+
import "C"
7+
8+
func Half(x int) int {
9+
return int(C.half(C.int(x)))
10+
}

tests/core/cgo/split_import_i_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package a
2+
3+
import "testing"
4+
5+
func TestInternal(t *testing.T) {
6+
if Answer() != 42 {
7+
t.Error("wrong answer")
8+
}
9+
}

0 commit comments

Comments
 (0)