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!: forbid importing '/r' from '/p' #1393

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 1 addition & 4 deletions examples/gno.land/p/demo/tests/gno.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
module gno.land/p/demo/tests

require (
gno.land/p/demo/tests/subtests v0.0.0-latest
gno.land/r/demo/tests v0.0.0-latest
)
require gno.land/p/demo/tests/subtests v0.0.0-latest
17 changes: 2 additions & 15 deletions examples/gno.land/p/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@ package tests
import (
"std"

psubtests "gno.land/p/demo/tests/subtests"
"gno.land/r/demo/tests"
rtests "gno.land/r/demo/tests"
"gno.land/p/demo/tests/subtests"
)

const World = "world"

// IncCounter demonstrates that it's possible to call a realm function from
// a package. So a package can potentially write into the store, by calling
// an other realm.
func IncCounter() {
tests.IncCounter()
}

func CurrentRealmPath() string {
return std.CurrentRealmPath()
}
Expand Down Expand Up @@ -61,11 +52,7 @@ func GetPrevRealm() std.Realm {
}

func GetPSubtestsPrevRealm() std.Realm {
return psubtests.GetPrevRealm()
}

func GetRTestsGetPrevRealm() std.Realm {
return rtests.GetPrevRealm()
return subtests.GetPrevRealm()
}

// Warning: unsafe pattern.
Expand Down
19 changes: 19 additions & 0 deletions tm2/pkg/std/memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import (
"fmt"
"go/parser"
"go/token"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -80,6 +82,23 @@
prev = file.Name
}

// If its a package, check if it imports realm
if strings.HasPrefix(mempkg.Path, "gno.land/p/") {
for _, file := range mempkg.Files {
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, file.Name, file.Body, parser.ImportsOnly)
if err != nil {
return fmt.Errorf("unable to parse %q", file.Name)

Check warning on line 91 in tm2/pkg/std/memfile.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/std/memfile.go#L91

Added line #L91 was not covered by tests
}
for _, imp := range astFile.Imports {
importPath := strings.TrimPrefix(strings.TrimSuffix(imp.Path.Value, `"`), `"`)
if strings.HasPrefix(importPath, "gno.land/r/") {
return fmt.Errorf("package %q imports realm %q", mempkg.Path, importPath)
}
}
}
}

return nil
}

Expand Down
92 changes: 83 additions & 9 deletions tm2/pkg/std/memfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,104 @@ func TestMemPackage_Validate(t *testing.T) {
{
"Correct",
&MemPackage{
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{{Name: "a.gno"}},
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: "package hey",
},
},
},
"",
},
{
"Unsorted",
&MemPackage{
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{{Name: "b.gno"}, {Name: "a.gno"}},
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{
{
Name: "b.gno",
Body: "package hey",
},
{
Name: "a.gno",
Body: "package hey",
},
},
},
`mempackage "gno.land/r/demo/hey" has unsorted files`,
},
{
"Duplicate",
&MemPackage{
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{{Name: "a.gno"}, {Name: "a.gno"}},
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: "package hey",
},
{
Name: "a.gno",
Body: "package hey",
},
},
},
`duplicate file name "a.gno"`,
},
{
"Package Imports Package",
&MemPackage{
Name: "hey",
Path: "gno.land/p/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: `package hey

import "gno.land/p/demo/hello"
`,
},
},
},
"",
},
{
"Realm Imports Realm",
&MemPackage{
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: `package hey

import "gno.land/r/demo/hello"
`,
},
},
},
"",
},
{
"Package Imports Realm",
&MemPackage{
Name: "hey",
Path: "gno.land/p/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: `package hey

import "gno.land/r/demo/heyrealm"
`,
},
},
},
`package "gno.land/p/demo/hey" imports realm "gno.land/r/demo/heyrealm"`,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading