Skip to content

Commit

Permalink
fix(mempkg): don't attempt to validate name of an empty package (no g…
Browse files Browse the repository at this point in the history
…no files)

Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Aug 28, 2023
1 parent aa7c1df commit 5441415
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
4 changes: 4 additions & 0 deletions gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ func makeGenesisDoc(
for _, pkg := range nonDraftPkgs {
// open files in directory as MemPackage.
memPkg := gno.ReadMemPackage(pkg.Dir, pkg.Name)
if len(memPkg.Files) == 0 { // skip empty package
continue
}

var tx std.Tx
tx.Msgs = []std.Msg{
vmm.MsgAddPackage{
Expand Down
9 changes: 7 additions & 2 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,8 +1132,13 @@ func ReadMemPackage(dir string, pkgPath string) *std.MemPackage {
Body: string(bz),
})
}
validatePkgName(string(pkgName))
memPkg.Name = string(pkgName)

// If no .gno files are present, package simply does not exist.
if len(memPkg.Files) > 0 {
validatePkgName(string(pkgName))
memPkg.Name = string(pkgName)
}

return memPkg
}

Expand Down
31 changes: 22 additions & 9 deletions gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,20 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri
stdlibPath := filepath.Join(rootDir, "gnovm", "stdlibs", pkgPath)
if osm.DirExists(stdlibPath) {
memPkg := gno.ReadMemPackage(stdlibPath, pkgPath)
m2 := gno.NewMachineWithOptions(gno.MachineOptions{
// NOTE: see also pkgs/sdk/vm/builtins.go
// XXX: why does this fail when just pkgPath?
PkgPath: "gno.land/r/stdlibs/" + pkgPath,
Output: stdout,
Store: store,
})
save := pkgPath != "testing" // never save the "testing" package
return m2.RunMemPackage(memPkg, save)
if len(memPkg.Files) > 0 {
m2 := gno.NewMachineWithOptions(gno.MachineOptions{
// NOTE: see also pkgs/sdk/vm/builtins.go
// XXX: why does this fail when just pkgPath?
PkgPath: "gno.land/r/stdlibs/" + pkgPath,
Output: stdout,
Store: store,
})
save := pkgPath != "testing" // never save the "testing" package
return m2.RunMemPackage(memPkg, save)
}

// There is no package there, but maybe we have a
// native counterpart below.
}
}

Expand Down Expand Up @@ -413,6 +418,10 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri
stdlibPath := filepath.Join(rootDir, "gnovm", "stdlibs", pkgPath)
if osm.DirExists(stdlibPath) {
memPkg := gno.ReadMemPackage(stdlibPath, pkgPath)
if len(memPkg.Files) == 0 {
panic(fmt.Sprintf("found an empty package `%s`", pkgPath))
}

m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Expand All @@ -427,6 +436,10 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri
examplePath := filepath.Join(rootDir, "examples", pkgPath)
if osm.DirExists(examplePath) {
memPkg := gno.ReadMemPackage(examplePath, pkgPath)
if len(memPkg.Files) == 0 {
panic(fmt.Sprintf("found an empty package `%s`", pkgPath))
}

m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Expand Down
3 changes: 3 additions & 0 deletions gnovm/tests/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func runPackageTest(t *testing.T, dir string, path string) {
t.Helper()

memPkg := gno.ReadMemPackage(dir, path)
if len(memPkg.Files) == 0 {
panic(fmt.Sprintf("found an empty package `%s`", path))
}

stdin := new(bytes.Buffer)
// stdout := new(bytes.Buffer)
Expand Down
3 changes: 3 additions & 0 deletions tm2/pkg/crypto/keys/client/addpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func execAddPkg(cfg *addPkgCfg, args []string, io *commands.IO) error {

// open files in directory as MemPackage.
memPkg := gno.ReadMemPackage(cfg.pkgDir, cfg.pkgPath)
if len(memPkg.Files) == 0 {
panic(fmt.Sprintf("found an empty package `%s`", cfg.pkgPath))
}

// precompile and validate syntax
err = gno.PrecompileAndCheckMempkg(memPkg)
Expand Down
5 changes: 5 additions & 0 deletions tm2/pkg/sdk/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) {
return nil, nil
}
memPkg := gno.ReadMemPackage(stdlibPath, pkgPath)
if len(memPkg.Files) == 0 {
// no gno files are present, skip this package
return nil, nil
}

m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "gno.land/r/stdlibs/" + pkgPath,
// PkgPath: pkgPath,
Expand Down

0 comments on commit 5441415

Please sign in to comment.