diff --git a/docs/tools/plugin_config_tool/plugin_config_tool.go b/docs/tools/plugin_config_tool/plugin_config_tool.go index 6f482f9d5d..b8f67c4199 100644 --- a/docs/tools/plugin_config_tool/plugin_config_tool.go +++ b/docs/tools/plugin_config_tool/plugin_config_tool.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io/fs" "os" "os/exec" "path/filepath" @@ -72,17 +73,19 @@ func getRules(buildDefsDirs []string) *rules.Rules { var defs []string for _, dir := range buildDefsDirs { - entries, err := os.ReadDir(dir) + err := filepath.WalkDir(dir, func(path string, de fs.DirEntry, err error) error { + if err != nil { + return err + } + if !de.IsDir() && filepath.Ext(path) == ".build_defs" { + defs = append(defs, path) + } + return nil + }) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } - - for _, e := range entries { - if filepath.Ext(e.Name()) == ".build_defs" { - defs = append(defs, filepath.Join("build_defs", e.Name())) - } - } } args := []string{"--noupdate", "query", "rules"} diff --git a/src/help/help.go b/src/help/help.go index 49ccc8a822..326f87eddb 100644 --- a/src/help/help.go +++ b/src/help/help.go @@ -5,7 +5,6 @@ import ( "fmt" iofs "io/fs" "os" - "path/filepath" "regexp" "sort" "strings" @@ -254,27 +253,32 @@ func getPluginBuildDefs(subrepo *core.Subrepo) map[string]*asp.Statement { ret := make(map[string]*asp.Statement) for _, dir := range dirs { fs := subrepo.FS() - dirEntries, err := iofs.ReadDir(fs, dir) - if err != nil { - log.Warningf("Failed to read %s: %s", dir, err) - } - for _, entry := range dirEntries { - if entry.IsDir() { - continue + err := iofs.WalkDir(fs, dir, func(path string, de iofs.DirEntry, err error) error { + if err != nil { + return err + } + + if de.IsDir() { + return nil } - path := filepath.Join(dir, entry.Name()) bs, err := iofs.ReadFile(fs, path) if err != nil { log.Warningf("Failed to read %s: %s", path, err) + return nil } stmts, err := p.ParseData(bs, path) if err != nil { log.Warningf("Failed to parse %s: %s", path, err) + return nil } addAllFunctions(ret, stmts, false) + return nil + }) + if err != nil { + log.Warningf("Failed to read %s: %s", dir, err) } } diff --git a/src/help/rules.go b/src/help/rules.go index c3f5d79505..2054ac61cd 100644 --- a/src/help/rules.go +++ b/src/help/rules.go @@ -2,6 +2,7 @@ package help import ( "encoding/json" + iofs "io/fs" "os" "path/filepath" "regexp" @@ -74,15 +75,17 @@ func AllBuiltinFunctions(state *core.BuildState) map[string]*asp.Statement { } } for _, dir := range state.Config.Parse.BuildDefsDir { - if files, err := os.ReadDir(dir); err == nil { - for _, file := range files { - if !file.IsDir() { - if stmts, err := p.ParseFileOnly(filepath.Join(dir, file.Name())); err == nil { - addAllFunctions(m, stmts, false) - } + filepath.WalkDir(dir, func(path string, de iofs.DirEntry, err error) error { + if err != nil { + return err + } + if !de.IsDir() { + if stmts, err := p.ParseFileOnly(path); err == nil { + addAllFunctions(m, stmts, false) } } - } + return nil + }) } return m } diff --git a/src/parse/asp/main/main.go b/src/parse/asp/main/main.go index 82dcbb0da8..285feb63d2 100644 --- a/src/parse/asp/main/main.go +++ b/src/parse/asp/main/main.go @@ -5,6 +5,7 @@ package main import ( "fmt" + iofs "io/fs" "os" "path/filepath" "reflect" @@ -191,17 +192,20 @@ func cleanup(ast string) string { } func mustLoadBuildDefsDir(state *core.BuildState, dirname string) { - dir, err := os.ReadDir(dirname) - if err != nil { - log.Fatalf("%s", err) - } - for _, fi := range dir { - if strings.HasSuffix(fi.Name(), ".build_defs") { - t := core.NewBuildTarget(core.NewBuildLabel(dirname, strings.TrimSuffix(fi.Name(), ".build_defs"))) - t.AddOutput(fi.Name()) + err := filepath.WalkDir(dirname, func(path string, de iofs.DirEntry, err error) error { + if err != nil { + return err + } + if !de.IsDir() && filepath.Ext(path) == ".build_defs" { + t := core.NewBuildTarget(core.NewBuildLabel(filepath.Dir(path), strings.TrimSuffix(de.Name(), ".build_defs"))) + t.AddOutput(de.Name()) t.SetState(core.Built) state.Graph.AddTarget(t) } + return nil + }) + if err != nil { + log.Fatalf("%s", err) } }