Skip to content

Commit

Permalink
internal/config: inferred proto mode can't override explicit default (#…
Browse files Browse the repository at this point in the history
…109)

If the proto mode is explicitly set to the default mode (via the
command line or directive), it will no longer be overridden by an
inferred legacy or disabled mode. It can still be overridden by an
explicit directive in a subdirectory.

Fixes #104
  • Loading branch information
jayconrod committed Feb 1, 2018
1 parent aec2916 commit 869b5a9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
12 changes: 8 additions & 4 deletions cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ func newFixUpdateConfiguration(cmd command, args []string) (*updateConfig, error
mode := fs.String("mode", "fix", "print: prints all of the updated BUILD files\n\tfix: rewrites all of the BUILD files in place\n\tdiff: computes the rewrite but then just does a diff")
outDir := fs.String("experimental_out_dir", "", "write build files to an alternate directory tree")
outSuffix := fs.String("experimental_out_suffix", "", "extra suffix appended to build file names. Only used if -experimental_out_dir is also set.")
proto := fs.String("proto", "default", "default: generates new proto rules\n\tdisable: does not touch proto rules\n\tlegacy (deprecated): generates old proto rules")
var proto explicitFlag
fs.Var(&proto, "proto", "default: generates new proto rules\n\tdisable: does not touch proto rules\n\tlegacy (deprecated): generates old proto rules")
if err := fs.Parse(args); err != nil {
if err == flag.ErrHelp {
fixUpdateUsage(fs)
Expand Down Expand Up @@ -271,9 +272,12 @@ func newFixUpdateConfiguration(cmd command, args []string) (*updateConfig, error
return nil, err
}

uc.c.ProtoMode, err = config.ProtoModeFromString(*proto)
if err != nil {
return nil, err
if proto.set {
uc.c.ProtoMode, err = config.ProtoModeFromString(proto.value)
if err != nil {
return nil, err
}
uc.c.ProtoModeExplicit = true
}

emit, ok := modeFromName[*mode]
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type Config struct {

// ProtoMode determines how rules are generated for protos.
ProtoMode ProtoMode

// ProtoModeExplicit indicates whether the proto mode was set explicitly.
ProtoModeExplicit bool
}

var DefaultValidBuildFileNames = []string{"BUILD.bazel", "BUILD"}
Expand Down
3 changes: 2 additions & 1 deletion internal/config/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func ApplyDirectives(c *Config, directives []Directive, rel string) *Config {
continue
}
modified.ProtoMode = protoMode
modified.ProtoModeExplicit = true
didModify = true
}
}
Expand All @@ -131,7 +132,7 @@ func ApplyDirectives(c *Config, directives []Directive, rel string) *Config {
// repository, legacy mode is used. If go_proto_library is loaded from another
// file, proto rule generation is disabled.
func InferProtoMode(c *Config, rel string, f *bf.File, directives []Directive) *Config {
if c.ProtoMode != DefaultProtoMode {
if c.ProtoMode != DefaultProtoMode || c.ProtoModeExplicit {
return c
}
for _, d := range directives {
Expand Down
8 changes: 8 additions & 0 deletions internal/config/directives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ func TestInferProtoMode(t *testing.T) {
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
`,
want: DefaultProtoMode,
}, {
desc: "explicit_no_override",
content: `load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")`,
c: Config{
ProtoMode: DefaultProtoMode,
ProtoModeExplicit: true,
},
want: DefaultProtoMode,
}, {
desc: "vendor",
rel: "vendor",
Expand Down

0 comments on commit 869b5a9

Please sign in to comment.