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(mempkg): don't attempt to validate name of an empty package #1076

Merged
merged 9 commits into from
Sep 5, 2023
1 change: 1 addition & 0 deletions gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func makeGenesisDoc(
for _, pkg := range nonDraftPkgs {
// open files in directory as MemPackage.
memPkg := gno.ReadMemPackage(pkg.Dir, pkg.Name)

var tx std.Tx
tx.Msgs = []std.Msg{
vmm.MsgAddPackage{
Expand Down
5 changes: 5 additions & 0 deletions gno.land/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 memPkg.IsEmpty() {
// 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
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 !memPkg.IsEmpty() {
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 !memPkg.IsEmpty() {
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 memPkg.IsEmpty() {
panic(fmt.Sprintf("found an empty package %q", pkgPath))
}
gfanton marked this conversation as resolved.
Show resolved Hide resolved

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 memPkg.IsEmpty() {
panic(fmt.Sprintf("found an empty package %q", pkgPath))
}

m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Expand Down
2 changes: 2 additions & 0 deletions gnovm/tests/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// "go/build"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/jaekwon/testify/require"
)

func TestPackages(t *testing.T) {
Expand Down Expand Up @@ -60,6 +61,7 @@ func runPackageTest(t *testing.T, dir string, path string) {
t.Helper()

memPkg := gno.ReadMemPackage(dir, path)
require.False(t, memPkg.IsEmpty())

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 memPkg.IsEmpty() {
panic(fmt.Sprintf("found an empty package %q", cfg.pkgPath))
}

// precompile and validate syntax
err = gno.PrecompileAndCheckMempkg(memPkg)
Expand Down
4 changes: 4 additions & 0 deletions tm2/pkg/std/memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (mempkg *MemPackage) GetFile(name string) *MemFile {
return nil
}

func (mempkg *MemPackage) IsEmpty() bool {
return len(mempkg.Files) == 0
}

const (
reDomainPart = `gno\.land`
rePathPart = `[a-z][a-z0-9_]*`
Expand Down
Loading