Skip to content

Commit

Permalink
feat(gpd): support of GOPACKAGESDRIVER_BAZEL_COMMON_FLAGS env variable (
Browse files Browse the repository at this point in the history
#4117)

This new environment variable can be used to explicitly give some flags
for all bazel info, build and query commands, and complete the list of
existing environment variables, i.e:

- GOPACKAGESDRIVER_BAZEL_FLAGS: Startup bazel flags.
- GOPACKAGESDRIVER_BAZEL_QUERY_FLAGS: Flags to give to all bazel query
commands.
- GOPACKAGESDRIVER_BAZEL_BUILD_FLAGS: Flags to give to all bazel build
commands.
  • Loading branch information
lbcjbb authored Sep 25, 2024
1 parent 3e44c78 commit d37da53
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
3 changes: 3 additions & 0 deletions go/tools/gopackagesdriver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ go_bazel_test(
size = "enormous",
srcs = ["gopackagesdriver_test.go"],
embed = [":gopackagesdriver_lib"],
env = {
"GOPACKAGESDRIVER_BAZEL_COMMON_FLAGS": "--noexperimental_enable_bzlmod",
},
rule_files = ["//:all_files"],
x_defs = {
"rulesGoRepositoryName": RULES_GO_REPO_NAME_FOR_TEST,
Expand Down
8 changes: 5 additions & 3 deletions go/tools/gopackagesdriver/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Bazel struct {
bazelBin string
workspaceRoot string
buildWorkingDirectory string
bazelCommonFlags []string
bazelStartupFlags []string
info map[string]string
version bazelVersion
Expand All @@ -53,11 +54,12 @@ type BEPNamedSet struct {
} `json:"namedSetOfFiles"`
}

func NewBazel(ctx context.Context, bazelBin, workspaceRoot string, buildWorkingDirectory string, bazelStartupFlags []string) (*Bazel, error) {
func NewBazel(ctx context.Context, bazelBin, workspaceRoot string, buildWorkingDirectory string, bazelCommonFlags []string, bazelStartupFlags []string) (*Bazel, error) {
b := &Bazel{
bazelBin: bazelBin,
workspaceRoot: workspaceRoot,
buildWorkingDirectory: buildWorkingDirectory,
bazelCommonFlags: bazelCommonFlags,
bazelStartupFlags: bazelStartupFlags,
}
if err := b.fillInfo(ctx); err != nil {
Expand Down Expand Up @@ -87,11 +89,11 @@ func (b *Bazel) fillInfo(ctx context.Context) error {
}

func (b *Bazel) run(ctx context.Context, command string, args ...string) (string, error) {
defaultArgs := []string{
defaultArgs := append([]string{
command,
"--tool_tag=" + toolTag,
"--ui_actions_shown=0",
}
}, b.bazelCommonFlags...)
cmd := exec.CommandContext(ctx, b.bazelBin, concatStringsArrays(b.bazelStartupFlags, defaultArgs, args)...)
fmt.Fprintln(os.Stderr, "Running:", cmd.Args)
cmd.Dir = b.WorkspaceRoot()
Expand Down
26 changes: 1 addition & 25 deletions go/tools/gopackagesdriver/gopackagesdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -78,29 +77,6 @@ func main() {
fmt.Fprintln(os.Stderr, "Subdirectory Hello World!")
}
`,
// Explicitly disable bzlmod which is enabled by default with bazel >=7.0.
// Remove this setup function when the test will be launched with bzlmod and
// the RULES_GO_REPO_NAME_FOR_TEST will be the canonical name of this rules_go module.
// This is required to match the stdlib label returned by the 'bazel query --consistent_labels',
// otherwise the gopackagesdriver will not return any stdlib packages.
SetUp: func() error {
f, err := os.OpenFile(".bazelrc", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("can't open bazelrc file: %v", err)
}

_, err = fmt.Fprintf(f, "common --noexperimental_enable_bzlmod\n")
if err != nil {
_ = f.Close()
return fmt.Errorf("can't update bazelrc file: %v", err)
}

if err := f.Close(); err != nil {
return fmt.Errorf("can't close bazelrc file: %v", err)
}

return nil
},
})
}

Expand Down Expand Up @@ -380,7 +356,7 @@ func runForTest(t *testing.T, driverRequest DriverRequest, relativeWorkingDir st
if !cut {
continue
}
if _, allowed := allowEnv[key]; !allowed {
if _, allowed := allowEnv[key]; !allowed && !strings.HasPrefix(key, "GOPACKAGES") {
os.Unsetenv(key)
oldEnv = append(oldEnv, key, value)
}
Expand Down
3 changes: 2 additions & 1 deletion go/tools/gopackagesdriver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
goDefaultAspect = rulesGoRepositoryName + "//go/tools/gopackagesdriver:aspect.bzl%go_pkg_info_aspect"
bazelBin = getenvDefault("GOPACKAGESDRIVER_BAZEL", "bazel")
bazelStartupFlags = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_FLAGS"))
bazelCommonFlags = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_COMMON_FLAGS"))
bazelQueryFlags = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_QUERY_FLAGS"))
bazelQueryScope = getenvDefault("GOPACKAGESDRIVER_BAZEL_QUERY_SCOPE", "")
bazelBuildFlags = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_BUILD_FLAGS"))
Expand All @@ -82,7 +83,7 @@ func run(ctx context.Context, in io.Reader, out io.Writer, args []string) error
return fmt.Errorf("unable to read request: %w", err)
}

bazel, err := NewBazel(ctx, bazelBin, workspaceRoot, buildWorkingDirectory, bazelStartupFlags)
bazel, err := NewBazel(ctx, bazelBin, workspaceRoot, buildWorkingDirectory, bazelCommonFlags, bazelStartupFlags)
if err != nil {
return fmt.Errorf("unable to create bazel instance: %w", err)
}
Expand Down

0 comments on commit d37da53

Please sign in to comment.