From d9d2156124c72578c2dfd6622000fcc17d781b97 Mon Sep 17 00:00:00 2001 From: rhang Date: Tue, 7 Oct 2025 22:29:58 +0000 Subject: [PATCH] gopackagesdriver: Add additional labels When using the Bazel gopackagesdriver to analyze a rules_go project it's possible to run into missing packages errors that look like ``` /home/user/go/bazel-pkgdrv/execroot/__main__/bazel-out/ k8-fastbuild/example.go:8:7: could not import example.org/mock (missing package: "@//src/example.org/mock:go_default_library") ``` The root cause is that the gopackagesdriver aspect only traverses target's deps field to populate the package information in the driver registry. In cases where a target depends on another target that is not declared in the "deps" field this packages will be missing from the gopackagesdriver response. ref: https://github.com/bazel-contrib/rules_go/blob/master/go/tools/gopackagesdriver/aspect.bzl#L30 An example of this edge case is on-the-fly generated go code produced by a bazel rule where the bazel rule provides a static dependency in the rule implementation instead of relying soley on the deps attribute. In this situation, this package is still required for the package analysis to complete but it is missing from the gopackagesdriver response. This change adds a mechanism to the gopackagesdriver to specify packages that should always be added. ref https://github.com/bazel-contrib/rules_go/issues/4473 --- go/tools/gopackagesdriver/bazel_json_builder.go | 5 ++--- go/tools/gopackagesdriver/main.go | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go/tools/gopackagesdriver/bazel_json_builder.go b/go/tools/gopackagesdriver/bazel_json_builder.go index bb50f7a044..0f5e92e559 100644 --- a/go/tools/gopackagesdriver/bazel_json_builder.go +++ b/go/tools/gopackagesdriver/bazel_json_builder.go @@ -189,7 +189,7 @@ func (b *BazelJSONBuilder) query(ctx context.Context, query string) ([]string, e return labels, nil } -func (b *BazelJSONBuilder) Labels(ctx context.Context, requests []string) ([]string, error) { +func (b *BazelJSONBuilder) Labels(ctx context.Context, requests []string, additionalLabels []string) ([]string, error) { labels, err := b.query(ctx, b.queryFromRequests(requests...)) if err != nil { return nil, fmt.Errorf("query failed: %w", err) @@ -198,8 +198,7 @@ func (b *BazelJSONBuilder) Labels(ctx context.Context, requests []string) ([]str if len(labels) == 0 { return nil, fmt.Errorf("found no labels matching the requests") } - - return labels, nil + return append(labels, additionalLabels...), nil } func (b *BazelJSONBuilder) Build(ctx context.Context, labels []string, mode packages.LoadMode) ([]string, error) { diff --git a/go/tools/gopackagesdriver/main.go b/go/tools/gopackagesdriver/main.go index dea1e57909..28d7482b5b 100644 --- a/go/tools/gopackagesdriver/main.go +++ b/go/tools/gopackagesdriver/main.go @@ -66,6 +66,7 @@ var ( buildWorkingDirectory = os.Getenv("BUILD_WORKING_DIRECTORY") additionalAspects = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_ADDTL_ASPECTS")) additionalKinds = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_KINDS")) + additionalLabels = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_ADDTL_LABELS")) emptyResponse = &driverResponse{ NotHandled: true, Compiler: "gc", @@ -93,7 +94,7 @@ func run(ctx context.Context, in io.Reader, out io.Writer, args []string) error return fmt.Errorf("unable to build JSON files: %w", err) } - labels, err := bazelJsonBuilder.Labels(ctx, queries) + labels, err := bazelJsonBuilder.Labels(ctx, queries, additionalLabels) if err != nil { return fmt.Errorf("unable to lookup package: %w", err) }