From 63b90e1b5b9c6cea18823357c38327a22e5e86cc Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Wed, 16 Oct 2024 16:39:16 +0200 Subject: [PATCH] Use filepath.WalkDir instead of filepath.Walk (#1210) This has the benefit of not having to stat all files along the way. Ideally, we'd be able to build the data bundle in the same function more efficiently too, but that's a more complex operation. Signed-off-by: Anders Eknert --- internal/lsp/bundles/cache.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/lsp/bundles/cache.go b/internal/lsp/bundles/cache.go index ec1ff31a..eb0b9ebc 100644 --- a/internal/lsp/bundles/cache.go +++ b/internal/lsp/bundles/cache.go @@ -9,6 +9,7 @@ import ( "io" "os" "path/filepath" + "slices" "strings" "github.com/open-policy-agent/opa/bundle" @@ -144,14 +145,20 @@ type cacheBundle struct { func (c *cacheBundle) Refresh(path string) (bool, error) { onDiskSourceDigests := make(map[string][]byte) + filter := []string{".manifest", "data.json", "data.yml", "data.yaml"} + // walk the bundle path and calculate the MD5 hash of each file on disk // at the moment - err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + err := filepath.WalkDir(path, func(path string, entry os.DirEntry, err error) error { if err != nil { return err } - if info.IsDir() || dataFileLoaderFilter(path, info, 0) { + if rio.IsSkipWalkDirectory(entry) { + return filepath.SkipDir + } + + if entry.IsDir() || !slices.Contains(filter, entry.Name()) { return nil }