-
Notifications
You must be signed in to change notification settings - Fork 386
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
Evaluate build tags as both true and false #1938
Open
patrickmscott
wants to merge
4
commits into
bazel-contrib:master
Choose a base branch
from
patrickmscott:patrick/conditional-tags
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1b7c214
Evaluate build tags as both true and false
patrickmscott d313a10
Merge branch 'master' into patrick/conditional-tags
patrickmscott 8a60b63
Merge branch 'master' into patrick/conditional-tags
patrickmscott b2abf0d
Address PR feedback
patrickmscott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -41,6 +41,16 @@ import ( | |||||
|
||||||
var minimumRulesGoVersion = version.Version{0, 29, 0} | ||||||
|
||||||
type tagSet map[string]bool | ||||||
|
||||||
func (ts tagSet) clone() tagSet { | ||||||
c := make(tagSet) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for k, v := range ts { | ||||||
c[k] = v | ||||||
} | ||||||
return c | ||||||
} | ||||||
|
||||||
// goConfig contains configuration values related to Go rules. | ||||||
type goConfig struct { | ||||||
// The name under which the rules_go repository can be referenced from the | ||||||
|
@@ -53,7 +63,7 @@ type goConfig struct { | |||||
|
||||||
// genericTags is a set of tags that Gazelle considers to be true. Set with | ||||||
// -build_tags or # gazelle:build_tags. Some tags, like gc, are always on. | ||||||
genericTags map[string]bool | ||||||
genericTags []tagSet | ||||||
|
||||||
// prefix is a prefix of an import path, used to generate importpath | ||||||
// attributes. Set with -go_prefix or # gazelle:prefix. | ||||||
|
@@ -178,8 +188,10 @@ func newGoConfig() *goConfig { | |||||
goProtoCompilers: defaultGoProtoCompilers, | ||||||
goGrpcCompilers: defaultGoGrpcCompilers, | ||||||
goGenerateProto: true, | ||||||
genericTags: []tagSet{ | ||||||
{"gc": true}, | ||||||
}, | ||||||
} | ||||||
gc.preprocessTags() | ||||||
return gc | ||||||
} | ||||||
|
||||||
|
@@ -189,28 +201,18 @@ func getGoConfig(c *config.Config) *goConfig { | |||||
|
||||||
func (gc *goConfig) clone() *goConfig { | ||||||
gcCopy := *gc | ||||||
gcCopy.genericTags = make(map[string]bool) | ||||||
for k, v := range gc.genericTags { | ||||||
gcCopy.genericTags[k] = v | ||||||
gcCopy.genericTags = make([]tagSet, 0, len(gc.genericTags)) | ||||||
for _, ts := range gc.genericTags { | ||||||
gcCopy.genericTags = append(gcCopy.genericTags, ts.clone()) | ||||||
} | ||||||
gcCopy.goProtoCompilers = gc.goProtoCompilers[:len(gc.goProtoCompilers):len(gc.goProtoCompilers)] | ||||||
gcCopy.goGrpcCompilers = gc.goGrpcCompilers[:len(gc.goGrpcCompilers):len(gc.goGrpcCompilers)] | ||||||
gcCopy.submodules = gc.submodules[:len(gc.submodules):len(gc.submodules)] | ||||||
return &gcCopy | ||||||
} | ||||||
|
||||||
// preprocessTags adds some tags which are on by default before they are | ||||||
// used to match files. | ||||||
func (gc *goConfig) preprocessTags() { | ||||||
if gc.genericTags == nil { | ||||||
gc.genericTags = make(map[string]bool) | ||||||
} | ||||||
gc.genericTags["gc"] = true | ||||||
} | ||||||
|
||||||
// setBuildTags sets genericTags by parsing as a comma separated list. An | ||||||
// error will be returned for tags that wouldn't be recognized by "go build". | ||||||
// preprocessTags should be called before this. | ||||||
func (gc *goConfig) setBuildTags(tags string) error { | ||||||
if tags == "" { | ||||||
return nil | ||||||
|
@@ -219,7 +221,13 @@ func (gc *goConfig) setBuildTags(tags string) error { | |||||
if strings.HasPrefix(t, "!") { | ||||||
return fmt.Errorf("build tags can't be negated: %s", t) | ||||||
} | ||||||
gc.genericTags[t] = true | ||||||
var newSets []tagSet | ||||||
for _, ts := range gc.genericTags { | ||||||
c := ts.clone() | ||||||
c[t] = true | ||||||
newSets = append(newSets, c) | ||||||
} | ||||||
gc.genericTags = append(gc.genericTags, newSets...) | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
@@ -578,11 +586,6 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.` | |||||
for _, d := range f.Directives { | ||||||
switch d.Key { | ||||||
case "build_tags": | ||||||
if err := gc.setBuildTags(d.Value); err != nil { | ||||||
log.Print(err) | ||||||
continue | ||||||
} | ||||||
gc.preprocessTags() | ||||||
if err := gc.setBuildTags(d.Value); err != nil { | ||||||
log.Print(err) | ||||||
} | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we actually using the "present, but mapped to false" state or should this be a
map[string]struct{}
?