@@ -27,12 +27,19 @@ go_library(
2727)
2828
2929go_test(
30- name = "hello_test",
31- srcs = [
32- "hello_test.go",
33- "hello_external_test.go",
34- ],
35- embed = [":hello"],
30+ name = "hello_test",
31+ srcs = [
32+ "hello_test.go",
33+ "hello_external_test.go",
34+ ],
35+ embed = [":hello"],
36+ )
37+
38+ go_library(
39+ name = "incompatible",
40+ srcs = ["incompatible.go"],
41+ importpath = "example.com/incompatible",
42+ target_compatible_with = ["@platforms//:incompatible"],
3643)
3744
3845-- hello.go --
@@ -58,6 +65,10 @@ import "testing"
5865
5966func TestHelloExternal(t *testing.T) {}
6067
68+ -- incompatible.go --
69+ //go:build ignore
70+ package hello
71+
6172-- subhello/BUILD.bazel --
6273load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
6374
@@ -76,7 +87,7 @@ import "os"
7687func main() {
7788 fmt.Fprintln(os.Stderr, "Subdirectory Hello World!")
7889}
79- ` ,
90+ ` ,
8091 })
8192}
8293
@@ -278,8 +289,8 @@ func TestOverlay(t *testing.T) {
278289 subhelloPath := path .Join (wd , "subhello/subhello.go" )
279290
280291 expectedImportsPerFile := map [string ][]string {
281- helloPath : [] string {"fmt" },
282- subhelloPath : [] string {"os" , "encoding/json" },
292+ helloPath : {"fmt" },
293+ subhelloPath : {"os" , "encoding/json" },
283294 }
284295
285296 overlayDriverRequest := DriverRequest {
@@ -324,6 +335,33 @@ func TestOverlay(t *testing.T) {
324335 expectSetEquality (t , expectedImportsPerFile [subhelloPath ], subhelloPkgImportPaths , "subhello imports" )
325336}
326337
338+ // TestIncompatible checks that a target that can be queried but not analyzed
339+ // does not appear in .Roots.
340+ func TestIncompatible (t * testing.T ) {
341+ resp := runForTest (t , DriverRequest {}, "." , "./..." )
342+
343+ rootLabels := make (map [string ]bool )
344+ for _ , root := range resp .Roots {
345+ rootLabels [root ] = true
346+ }
347+
348+ // Verify //:hello is in .Roots and check whether its label starts with
349+ // "@@" (bzlmod) or "@" (not bzlmod).
350+ var incompatibleLabel string
351+ if rootLabels ["@@//:hello" ] {
352+ incompatibleLabel = "@@//:incompatible"
353+ } else if rootLabels ["@//:hello" ] {
354+ incompatibleLabel = "@//:incompatible"
355+ } else {
356+ t .Fatalf ("response does not contain //:hello; roots were %s" , strings .Join (resp .Roots , ", " ))
357+ }
358+
359+ // Verify //:incompatible is NOT in .Roots.
360+ if rootLabels [incompatibleLabel ] {
361+ t .Fatalf ("response contains root %s" , incompatibleLabel )
362+ }
363+ }
364+
327365func runForTest (t * testing.T , driverRequest DriverRequest , relativeWorkingDir string , args ... string ) driverResponse {
328366 t .Helper ()
329367
0 commit comments