From 3d17082c1371e4f0c55a4a25399323bcbfa7d658 Mon Sep 17 00:00:00 2001 From: Rohan Singh Date: Fri, 26 Apr 2024 15:31:38 +0000 Subject: [PATCH] Speed up app loading and checks * We were loading an applet by walking its entire source FS, but skipping any files outside of the root directory. Instead, just list and process the files in the root directory explicitly. * Move buildifier-based checks to the end of `pixlet check`, since they walk the entire directory tree. This means they won't run if there are any other errors, which are quicker to find. --- cmd/check.go | 56 +++++++++++++++++++++++------------------------ runtime/applet.go | 27 ++++++++++------------- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/cmd/check.go b/cmd/check.go index 5f9bc55682..61f8a48476 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -59,34 +59,6 @@ func checkCmd(cmd *cobra.Command, args []string) error { baseDir = filepath.Dir(path) } - // run format and lint on *.star files in the fs - fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - - if d.IsDir() || !strings.HasSuffix(p, ".star") { - return nil - } - - realPath := filepath.Join(baseDir, p) - - dryRunFlag = true - if err := formatCmd(cmd, []string{realPath}); err != nil { - foundIssue = true - failure(p, fmt.Errorf("app is not formatted correctly: %w", err), fmt.Sprintf("try `pixlet format %s`", realPath)) - } - - outputFormat = "off" - err = lintCmd(cmd, []string{realPath}) - if err != nil { - foundIssue = true - failure(p, fmt.Errorf("app has lint warnings: %w", err), fmt.Sprintf("try `pixlet lint --fix %s`", realPath)) - } - - return nil - }) - // Check if an app can load. err = community.LoadApp(cmd, []string{path}) if err != nil { @@ -152,6 +124,34 @@ func checkCmd(cmd *cobra.Command, args []string) error { continue } + // run format and lint on *.star files in the fs + fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if d.IsDir() || !strings.HasSuffix(p, ".star") { + return nil + } + + realPath := filepath.Join(baseDir, p) + + dryRunFlag = true + if err := formatCmd(cmd, []string{realPath}); err != nil { + foundIssue = true + failure(p, fmt.Errorf("app is not formatted correctly: %w", err), fmt.Sprintf("try `pixlet format %s`", realPath)) + } + + outputFormat = "off" + err = lintCmd(cmd, []string{realPath}) + if err != nil { + foundIssue = true + failure(p, fmt.Errorf("app has lint warnings: %w", err), fmt.Sprintf("try `pixlet lint --fix %s`", realPath)) + } + + return nil + }) + // If we're here, the app and manifest are good to go! success(path) } diff --git a/runtime/applet.go b/runtime/applet.go index 2bac67fe01..0c4ddddc5c 100644 --- a/runtime/applet.go +++ b/runtime/applet.go @@ -11,9 +11,9 @@ import ( "testing" "testing/fstest" + starlibbsoup "github.com/qri-io/starlib/bsoup" starlibgzip "github.com/qri-io/starlib/compress/gzip" starlibbase64 "github.com/qri-io/starlib/encoding/base64" - starlibbsoup "github.com/qri-io/starlib/bsoup" starlibcsv "github.com/qri-io/starlib/encoding/csv" starlibhash "github.com/qri-io/starlib/hash" starlibhtml "github.com/qri-io/starlib/html" @@ -311,24 +311,21 @@ func (a *Applet) PathsForBundle() []string { } func (a *Applet) load(fsys fs.FS) (err error) { - if err := fs.WalkDir(fsys, ".", func(pathToLoad string, d fs.DirEntry, walkDirErr error) error { - if walkDirErr != nil { - return walkDirErr - } + // list files in the root directory of fsys + rootDir, err := fs.ReadDir(fsys, ".") + if err != nil { + return fmt.Errorf("reading root directory: %v", err) + } - if d.IsDir() || path.Dir(pathToLoad) != "." { - // only process files in the root directory - return nil + for _, d := range rootDir { + if d.IsDir() || !strings.HasSuffix(d.Name(), ".star") { + // only process Starlark files + continue } - if !strings.HasSuffix(pathToLoad, ".star") { - // not a starlark file - return nil + if err := a.ensureLoaded(fsys, d.Name()); err != nil { + return err } - - return a.ensureLoaded(fsys, pathToLoad) - }); err != nil { - return err } if a.mainFun == nil {