Skip to content

Commit

Permalink
fix(cmd/gno): support new examples gno.mods (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl authored Jun 19, 2023
1 parent fb0c2a1 commit 0b4d4e5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
30 changes: 28 additions & 2 deletions gnovm/cmd/gno/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"

Expand Down Expand Up @@ -84,14 +85,25 @@ func execDoc(cfg *docCfg, args []string, io *commands.IO) error {
return fmt.Errorf("could not determine working directory: %w", err)
}

var modDirs []string

rd, err := gnomod.FindRootDir(wd)
if err != nil && !errors.Is(err, gnomod.ErrGnoModNotFound) {
return fmt.Errorf("error determining root gno.mod file: %w", err)
}
modDirs := []string{rd}
if !errors.Is(err, gnomod.ErrGnoModNotFound) {
modDirs = append(modDirs, rd)
}

examplesModules, err := findGnomodExamples(filepath.Join(cfg.rootDir, "examples"))
if err != nil {
io.Printfln("warning: error scanning examples directory for modules: %v", err)
} else {
modDirs = append(modDirs, examplesModules...)
}

// select dirs from which to gather directories
dirs := []string{filepath.Join(cfg.rootDir, "gnovm/stdlibs"), filepath.Join(cfg.rootDir, "examples")}
dirs := []string{filepath.Join(cfg.rootDir, "gnovm/stdlibs")}
res, err := doc.ResolveDocumentable(dirs, modDirs, args, cfg.unexported)
if res == nil {
return err
Expand All @@ -109,3 +121,17 @@ func execDoc(cfg *docCfg, args []string, io *commands.IO) error {
},
)
}

func findGnomodExamples(dir string) ([]string, error) {
dirs := make([]string, 0, 64) // "hint" about the size
err := filepath.WalkDir(dir, func(path string, e fs.DirEntry, err error) error {
if err != nil {
return err
}
if !e.IsDir() && e.Name() == "gno.mod" {
dirs = append(dirs, filepath.Dir(path))
}
return nil
})
return dirs, err
}
11 changes: 10 additions & 1 deletion gnovm/pkg/doc/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type bfsDirs struct {
}

// newDirs begins scanning the given stdlibs directory.
// dirs are "gopath-like" directories, such as @/gnovm/stdlibs and @/examples.
// dirs are "gopath-like" directories, such as @/gnovm/stdlibs, whose path
// relative to the root specify the import path.
// modDirs are user directories, expected to have gno.mod files
func newDirs(dirs []string, modDirs []string) *bfsDirs {
d := &bfsDirs{
Expand Down Expand Up @@ -99,6 +100,14 @@ func getGnoModDirs(gm *gnomod.File) []bfsDir {
for _, r := range gm.Require {
mv := gm.Resolve(r)
path := gnomod.PackageDir("", mv)
if _, err := os.Stat(path); err != nil {
// only give directories which actually exist and don't give
// an error when accessing
if !os.IsNotExist(err) {
log.Println("open source directories from gno.mod:", err)
}
continue
}
dirs = append(dirs, bfsDir{
importPath: mv.Path,
dir: path,
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func resolveDocumentable(dirs *bfsDirs, parsed docArgs, unexported bool) (Docume
parsed = docArgs{pkg: ".", sym: parsed.pkg, acc: parsed.sym}
return resolveDocumentable(dirs, parsed, unexported)
}
// we wanted documentabfsDirn about a package, and we found one!
// we wanted documentation about a package, and we found one!
if parsed.sym == "" {
return &documentable{bfsDir: candidates[0]}, nil
}
Expand Down

0 comments on commit 0b4d4e5

Please sign in to comment.