Skip to content
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

refactor: remove nested walk function literal #1884

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 55 additions & 55 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,72 +122,72 @@ func Walk(c *config.Config, cexts []config.Configurer, dirs []string, mode Mode,
log.Printf("error loading .bazelignore: %v", err)
}

var visit func(*config.Config, string, string, bool)
visit = func(c *config.Config, dir, rel string, updateParent bool) {
haveError := false

// TODO: OPT: ReadDir stats all the files, which is slow. We just care about
// names and modes, so we should use something like
// golang.org/x/tools/internal/fastwalk to speed this up.
ents, err := os.ReadDir(dir)
if err != nil {
log.Print(err)
return
}
visit(c, cexts, isBazelIgnored, knownDirectives, updateRels, wf, c.RepoRoot, "", false)
}

f, err := loadBuildFile(c, rel, dir, ents)
if err != nil {
log.Print(err)
if c.Strict {
// TODO(https://github.com/bazelbuild/bazel-gazelle/issues/1029):
// Refactor to accumulate and propagate errors to main.
log.Fatal("Exit as strict mode is on")
}
haveError = true
}
func visit(c *config.Config, cexts []config.Configurer, isBazelIgnored isIgnoredFunc, knownDirectives map[string]bool, updateRels *UpdateFilter, wf WalkFunc, dir, rel string, updateParent bool) {
fmeum marked this conversation as resolved.
Show resolved Hide resolved
haveError := false

if isBazelIgnored(rel) {
return
// TODO: OPT: ReadDir stats all the files, which is slow. We just care about
// names and modes, so we should use something like
// golang.org/x/tools/internal/fastwalk to speed this up.
ents, err := os.ReadDir(dir)
if err != nil {
log.Print(err)
return
}

f, err := loadBuildFile(c, rel, dir, ents)
if err != nil {
log.Print(err)
if c.Strict {
// TODO(https://github.com/bazelbuild/bazel-gazelle/issues/1029):
// Refactor to accumulate and propagate errors to main.
log.Fatal("Exit as strict mode is on")
}
haveError = true
}

c = configure(cexts, knownDirectives, c, rel, f)
wc := getWalkConfig(c)
if isBazelIgnored(rel) {
return
}

if wc.isExcluded(rel, ".") {
return
}
c = configure(cexts, knownDirectives, c, rel, f)
wc := getWalkConfig(c)

var subdirs, regularFiles []string
for _, ent := range ents {
base := ent.Name()
if isBazelIgnored(path.Join(rel, base)) || wc.isExcluded(rel, base) {
continue
}
ent := resolveFileInfo(wc, dir, rel, ent)
switch {
case ent == nil:
continue
case ent.IsDir():
subdirs = append(subdirs, base)
default:
regularFiles = append(regularFiles, base)
}
}
if wc.isExcluded(rel, ".") {
return
}

shouldUpdate := updateRels.shouldUpdate(rel, updateParent)
for _, sub := range subdirs {
if subRel := path.Join(rel, sub); updateRels.shouldVisit(subRel, shouldUpdate) {
visit(c, filepath.Join(dir, sub), subRel, shouldUpdate)
}
var subdirs, regularFiles []string
for _, ent := range ents {
base := ent.Name()
if isBazelIgnored(path.Join(rel, base)) || wc.isExcluded(rel, base) {
continue
}
ent := resolveFileInfo(wc, dir, rel, ent)
switch {
case ent == nil:
continue
case ent.IsDir():
subdirs = append(subdirs, base)
default:
regularFiles = append(regularFiles, base)
}
}

update := !haveError && !wc.ignore && shouldUpdate
if updateRels.shouldCall(rel, updateParent) {
genFiles := findGenFiles(wc, f)
wf(dir, rel, c, update, f, subdirs, regularFiles, genFiles)
shouldUpdate := updateRels.shouldUpdate(rel, updateParent)
for _, sub := range subdirs {
if subRel := path.Join(rel, sub); updateRels.shouldVisit(subRel, shouldUpdate) {
visit(c, cexts, isBazelIgnored, knownDirectives, updateRels, wf, filepath.Join(dir, sub), subRel, shouldUpdate)
}
}
visit(c, c.RepoRoot, "", false)

update := !haveError && !wc.ignore && shouldUpdate
if updateRels.shouldCall(rel, updateParent) {
genFiles := findGenFiles(wc, f)
wf(dir, rel, c, update, f, subdirs, regularFiles, genFiles)
}
}

// An UpdateFilter tracks which directories need to be updated
Expand Down