-
Notifications
You must be signed in to change notification settings - Fork 389
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
feat: support only updating existing BUILD files #1921
Open
jbedard
wants to merge
1
commit into
bazel-contrib:master
Choose a base branch
from
jbedard:1832-gen-mode
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 all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,14 +33,27 @@ import ( | |
gzflag "github.com/bazelbuild/bazel-gazelle/flag" | ||
) | ||
|
||
// generationModeType represents one of the generation modes. | ||
type generationModeType string | ||
|
||
// Generation modes | ||
const ( | ||
// Update: update and maintain existing BUILD files | ||
generationModeUpdate generationModeType = "update_only" | ||
|
||
// Create: create new and update existing BUILD files | ||
generationModeCreate generationModeType = "create_and_update" | ||
) | ||
|
||
// TODO(#472): store location information to validate each exclude. They | ||
// may be set in one directory and used in another. Excludes work on | ||
// declared generated files, so we can't just stat. | ||
|
||
type walkConfig struct { | ||
excludes []string | ||
ignore bool | ||
follow []string | ||
updateOnly bool | ||
excludes []string | ||
ignore bool | ||
follow []string | ||
} | ||
|
||
const walkName = "_walk" | ||
|
@@ -70,7 +83,7 @@ func (*Configurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) | |
func (*Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error { return nil } | ||
|
||
func (*Configurer) KnownDirectives() []string { | ||
return []string{"exclude", "follow", "ignore"} | ||
return []string{"generation_mode", "exclude", "follow", "ignore"} | ||
fmeum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (cr *Configurer) Configure(c *config.Config, rel string, f *rule.File) { | ||
|
@@ -82,6 +95,16 @@ func (cr *Configurer) Configure(c *config.Config, rel string, f *rule.File) { | |
if f != nil { | ||
for _, d := range f.Directives { | ||
switch d.Key { | ||
case "generation_mode": | ||
switch generationModeType(strings.TrimSpace(d.Value)) { | ||
case generationModeUpdate: | ||
wcCopy.updateOnly = true | ||
case generationModeCreate: | ||
wcCopy.updateOnly = false | ||
default: | ||
log.Printf("unknown generation_mode %q in //%s", d.Value, f.Pkg) | ||
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. @fmeum I added this print+continue since you last reviewed, so now it ignores invalid values... |
||
continue | ||
} | ||
case "exclude": | ||
if err := checkPathMatchPattern(path.Join(rel, d.Value)); err != nil { | ||
log.Printf("the exclusion pattern is not valid %q: %s", path.Join(rel, d.Value), 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
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.
This is the directive named used by the Aspect plugins today. I think we need something better here that doesn't include the word "mode" which already has a meaning within gazelle.
Ideas?
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.
build_file_action?
create_build_files yes/no?
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.
I am going to try to this patch at Airtable. I am thinking instead of manually specifying
generation_mode
to isntead do something like (pseudocode)collectionOnly := f == nil !any(file.endswith('.go') for file in ents)
, since we have go code spread throughout the treeThere 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.
I think different repos will have different preferences and we can't really make any assumptions based on which files exist.
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.
Assuming you are in David's situation, could you still leverage
generation_mode: update
to simplify your language implementation or would you need to resort tocreate
with the same hack mentioned in the linked issue?I'm asking because adding a new directive comes with a complexity cost that may not be justified if it doesn't address many use cases. Do you see a way to simplify what a language would need to do to get this behavior without adding a new directive?
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.
I've changed my hack described in the issue to a patch that's essentially the same as this PR. The hack required manually walking the fs (in addition to gazelle walking it) which is bad in many ways, performance being a big one.
I think no matter what a new API is needed so gazelle can generate the
language.GenerateArgs
with the correct files. It could be an API in theConfigure
phase, but today there are no APIs there that change gazelle's behaviour and adding such an API sounds far more complex then adding a directive.