Skip to content

Commit

Permalink
Speed up app loading and checks
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
rohansingh committed Apr 26, 2024
1 parent 1cbc303 commit 3d17082
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 43 deletions.
56 changes: 28 additions & 28 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
27 changes: 12 additions & 15 deletions runtime/applet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3d17082

Please sign in to comment.