Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmd/gno): support new examples gno.mods #915

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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