Skip to content

Commit

Permalink
use flags-as-proto to determine command line schema
Browse files Browse the repository at this point in the history
  • Loading branch information
tempoz committed Dec 13, 2024
1 parent d4ffd3f commit 81fa377
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 13 deletions.
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ go_deps.gazelle_override(
],
path = "cel.dev/expr",
)

use_repo(
go_deps,
"co_honnef_go_tools",
Expand Down
20 changes: 20 additions & 0 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ load("@io_bazel_rules_webtesting//web:go_repositories.bzl", web_test_go_reposito

web_test_go_repositories()

load("@bazel_gazelle//:deps.bzl", "go_repository", "gazelle_dependencies")

go_repository(
name = "com_github_bazelbuild_bazel",
build_file_generation = "clean",
build_file_proto_mode = "package",
importpath = "github.com/bazelbuild/bazel",
tag = "7.4.1",
repo_mapping = {
"@rules_go": "@io_bazel_rules_go",
},
)

gazelle_dependencies(
go_env = {
"GOPROXY": "https://proxy.golang.org|direct",
},
go_sdk = "rules_go~~go_sdk~buildbuddy__download_0",
)

register_toolchains(
"@buildbuddy_toolchain//:ubuntu_cc_toolchain",
)
2 changes: 1 addition & 1 deletion cli/parser/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ go_library(
"//server/remote_cache/digest",
"//server/util/disk",
"//server/util/proto",
"@com_github_bazelbuild_bazel_protos//",
"@com_github_bazelbuild_bazel//src/main/protobuf:bazel_flags_go_proto",
"@com_github_google_shlex//:shlex",
],
)
Expand Down
73 changes: 64 additions & 9 deletions cli/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,66 @@ func (s *CommandLineSchema) CommandSupportsOpt(opt string) bool {
return false
}

func GetOptionSetsfromProto(args []string, flagCollection *bfpb.FlagCollection, onlyStartupOptions bool) (map[string]*OptionSet, error) {
sets := make(map[string]*OptionSet)
for _, info := range flagCollection.FlagInfos {
o := &Option{
Name: info.GetName(),
ShortName: info.GetAbbreviation(),
Multi: info.GetAllowsMultiple(),
BoolLike: info.GetHasNegativeFlag(),
}
for _, cmd := range info.GetCommands() {
var set *OptionSet
var ok bool
if set, ok = sets[cmd]; !ok {
set = &OptionSet{
All: []*Option{},
ByName: make(map[string]*Option),
ByShortName: make(map[string]*Option),
}
sets[cmd] = set
}
set.All = append(set.All, o)
set.ByName[o.Name] = o
if o.ShortName != "" {
set.ByShortName[o.ShortName] = o
}
}
}
return sets, nil
}

// GetCommandLineSchema returns the effective CommandLineSchemas for the given
// command line.
func getCommandLineSchema(args []string, bazelHelp BazelHelpFunc, onlyStartupOptions bool) (*CommandLineSchema, error) {
startupHelp, err := bazelHelp("startup_options")
if err != nil {
return nil, err
var optionSets map[string]*OptionSet
// try flags-as-proto first; fall back to parsing help if bazel version does not support it.
if protoHelp, err := bazelHelp("flags-as-proto"); err == nil {
protoHelp = strings.TrimSpace(protoHelp)
b, err := base64.StdEncoding.DecodeString(protoHelp)
if err != nil {
return nil, err
}
flagCollection := &bfpb.FlagCollection{}
if err := proto.Unmarshal(b, flagCollection); err != nil {
return nil, err
}
sets, err := GetOptionSetsfromProto(args, flagCollection, onlyStartupOptions)
if err != nil {
return nil, err
}
optionSets = sets
}
schema := &CommandLineSchema{
StartupOptions: parseBazelHelp(startupHelp, "startup_options"),
schema := &CommandLineSchema{}
if startupOptions, ok := optionSets["startup"]; ok {
schema.StartupOptions = startupOptions
} else {
startupHelp, err := bazelHelp("startup_options")
if err != nil {
return nil, err
}
schema.StartupOptions = parseBazelHelp(startupHelp, "startup_options")
}
bazelCommands, err := BazelCommands()
if err != nil {
Expand Down Expand Up @@ -409,11 +460,15 @@ func getCommandLineSchema(args []string, bazelHelp BazelHelpFunc, onlyStartupOpt
if schema.Command == "" {
return schema, nil
}
commandHelp, err := bazelHelp(schema.Command)
if err != nil {
return nil, err
if commandOptions, ok := optionSets[schema.Command]; ok {
schema.CommandOptions = commandOptions
} else {
commandHelp, err := bazelHelp(schema.Command)
if err != nil {
return nil, err
}
schema.CommandOptions = parseBazelHelp(commandHelp, schema.Command)
}
schema.CommandOptions = parseBazelHelp(commandHelp, schema.Command)
return schema, nil
}

Expand Down
9 changes: 6 additions & 3 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,14 @@ def install_go_mod_dependencies(workspace_name = "buildbuddy"):

# keep
go_repository(
name = "com_github_bazelbuild_bazel_protos",
build_file_generation = "on",
name = "com_github_bazelbuild_bazel",
build_file_generation = "clean",
build_file_proto_mode = "package",
importpath = "github.com/bazelbuild/bazel/src/main/protobuf",
importpath = "github.com/bazelbuild/bazel",
tag = "7.4.1",
repo_mapping = {
"@rules_go": "@io_bazel_rules_go",
},
)
go_repository(
name = "com_github_bazelbuild_bazel_gazelle",
Expand Down

0 comments on commit 81fa377

Please sign in to comment.