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
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
gfanton marked this conversation as resolved.
Show resolved Hide resolved
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 {
gfanton marked this conversation as resolved.
Show resolved Hide resolved
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))
gfanton marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading