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

feat(transpile): allow extra root dirs #21

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
10 changes: 9 additions & 1 deletion gnovm/cmd/gno/transpile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
type transpileCfg struct {
verbose bool
rootDir string
extraDirs string
skipImports bool
gobuild bool
goBinary string
Expand Down Expand Up @@ -94,6 +95,13 @@ func (c *transpileCfg) RegisterFlags(fs *flag.FlagSet) {
"clone location of github.com/gnolang/gno (gno tries to guess it)",
)

fs.StringVar(
&c.extraDirs,
"extra-dirs",
"",
"extra directories to look for packages in",
)

fs.BoolVar(
&c.skipImports,
"skip-imports",
Expand Down Expand Up @@ -251,7 +259,7 @@ func transpileFile(srcPath string, opts *transpileOptions) error {
targetFilename, tags := transpiler.TranspiledFilenameAndTags(srcPath)

// preprocess.
transpileRes, err := transpiler.Transpile(string(source), tags, srcPath)
transpileRes, err := transpiler.Transpile(string(source), tags, srcPath, strings.Split(opts.cfg.extraDirs, ","))
if err != nil {
return fmt.Errorf("transpile: %w", err)
}
Expand Down
46 changes: 43 additions & 3 deletions gnovm/pkg/transpiler/transpiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"go/parser"
goscanner "go/scanner"
"go/token"
"io/fs"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -75,7 +77,7 @@ func TranspiledFilenameAndTags(gnoFilePath string) (targetFilename, tags string)
// Transpile performs transpilation on the given source code. tags can be used
// to specify build tags; and filename helps generate useful error messages and
// discriminate between test and normal source files.
func Transpile(source, tags, filename string) (*Result, error) {
func Transpile(source, tags, filename string, extraDirs []string) (*Result, error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, source,
// SkipObjectResolution -- unused here.
Expand All @@ -87,7 +89,8 @@ func Transpile(source, tags, filename string) (*Result, error) {

isTestFile := strings.HasSuffix(filename, "_test.gno") || strings.HasSuffix(filename, "_filetest.gno")
ctx := &transpileCtx{
rootDir: gnoenv.RootDir(),
rootDir: gnoenv.RootDir(),
extraDirs: extraDirs,
}
stdlibPrefix := filepath.Join(ctx.rootDir, "gnovm", "stdlibs")
if isTestFile {
Expand Down Expand Up @@ -142,6 +145,9 @@ type transpileCtx struct {
// If rootDir is given, we will check that the directory of the import path
// exists (using rootDir/packageDirLocation()).
rootDir string
// If extraDirs is given, we will check that the directory of the import path
// exists (using rootDir/packageDirLocation()).
extraDirs []string
// This should be set if we're working with a file from a standard library.
// This allows us to easily check if a function has a native binding, and as
// such modify its call expressions appropriately.
Expand All @@ -165,7 +171,41 @@ func (ctx *transpileCtx) transformFile(fset *token.FileSet, f *ast.File) (*ast.F
continue
}

if ctx.rootDir != "" {
foundInExtra := false
for _, extraDir := range ctx.extraDirs {
// TODO: find packages once
modFiles := []string{}
if err := filepath.Walk(extraDir, func(path string, info fs.FileInfo, err error) error {
if !strings.HasSuffix(path, "/gno.mod") {
return nil
}
modFiles = append(modFiles, path)
return nil
}); err != nil {
return nil, err
}
packageNames := []string{}
for _, modFile := range modFiles {
modData, err := os.ReadFile(modFile)
if err != nil {
continue
}
re := regexp.MustCompile(`^module (.+)`)
res := re.FindSubmatch(modData)
if len(res) != 2 {
continue
}
packageNames = append(packageNames, string(res[1]))
}

for _, pn := range packageNames {
if pn == importPath {
foundInExtra = true
}
}
}

if !foundInExtra && ctx.rootDir != "" {
dirPath := filepath.Join(ctx.rootDir, PackageDirLocation(importPath))
if _, err := os.Stat(dirPath); err != nil {
if !os.IsNotExist(err) {
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/transpiler/transpiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func testfunc() {
if filename == "" {
filename = "foo.gno"
}
res, err := Transpile(source, c.tags, filename)
res, err := Transpile(source, c.tags, filename, nil)

if c.expectedError != "" {
require.EqualError(t, err, c.expectedError)
Expand Down
Loading