Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gopackagesdriver: skip root packages that can't be built #3806

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
60 changes: 53 additions & 7 deletions go/tools/gopackagesdriver/gopackagesdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ go_library(
)

go_test(
name = "hello_test",
srcs = [
"hello_test.go",
"hello_external_test.go",
],
embed = [":hello"],
name = "hello_test",
srcs = [
"hello_test.go",
"hello_external_test.go",
],
embed = [":hello"],
)

go_library(
name = "incompatible",
srcs = ["incompatible.go"],
importpath = "example.com/incompatible",
target_compatible_with = ["@platforms//:incompatible"],
)

-- hello.go --
Expand All @@ -61,7 +68,11 @@ package hello_test
import "testing"

func TestHelloExternal(t *testing.T) {}
`,

-- incompatible.go --
//go:build ignore
package hello
`,
})
}

Expand Down Expand Up @@ -167,6 +178,41 @@ func TestExternalTests(t *testing.T) {
}
}

// TestIncompatible checks that a target that can be queried but not analyzed
// does not appear in .Roots.
func TestIncompatible(t *testing.T) {
reader := strings.NewReader("{}")
out, _, err := bazel_testing.BazelOutputWithInput(reader, "run", "@io_bazel_rules_go//go/tools/gopackagesdriver", "--", "./...")
if err != nil {
t.Fatalf("error running bazel: %v", err)
}
var resp response
if err := json.Unmarshal(out, &resp); err != nil {
t.Fatalf("unmarshaling response: %v", err)
}

rootLabels := make(map[string]bool)
for _, root := range resp.Roots {
rootLabels[root] = true
}

// Verify //:hello is in .Roots and check whether its label starts with
// "@@" (bzlmod) or "@" (not bzlmod).
var incompatibleLabel string
if rootLabels["@@//:hello"] {
incompatibleLabel = "@@//:incompatible"
} else if rootLabels["@//:hello"] {
incompatibleLabel = "@//:incompatible"
} else {
t.Fatalf("response does not contain //:hello; roots were %s", strings.Join(resp.Roots, ", "))
}

// Verify //:incompatible is NOT in .Roots.
if rootLabels[incompatibleLabel] {
t.Fatalf("response contains root %s", incompatibleLabel)
}
}

func runForTest(t *testing.T, args ...string) driverResponse {
t.Helper()

Expand Down
8 changes: 7 additions & 1 deletion go/tools/gopackagesdriver/packageregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,18 @@ func (pr *PackageRegistry) Match(labels []string) ([]string, []*FlatPackage) {
roots[pkg.ID] = struct{}{}
}
}
} else {
} else if _, ok := pr.packagesByID[label]; ok {
roots[label] = struct{}{}
// If an xtest package exists for this package add it to the roots
if _, ok := pr.packagesByID[label+"_xtest"]; ok {
roots[label+"_xtest"] = struct{}{}
}
} else {
// Skip a package if we don't have .pkg.json for it.
// This happens if 'bazel query' matches the target, but 'bazel build'
// can't analyze it, for example, if target_compatible_with is set
// with contraints not compatible with the host platform.
continue
}
}

Expand Down