Skip to content

Commit 5c4208f

Browse files
committed
chore: improve explainers
Signed-off-by: Norman Meier <[email protected]>
1 parent 22f1589 commit 5c4208f

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

contribs/nocycles/main.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/gnolang/gno/gnovm/pkg/packages"
1616
)
1717

18-
// TODO: only allow injected testing libs in tests
1918
var injectedTestingLibs = []string{"encoding/json", "fmt", "os", "internal/os_test"}
2019

2120
func main() {
@@ -50,9 +49,10 @@ type testPkg struct {
5049
Imports packages.ImportsMap
5150
}
5251

53-
func listPkgs(rootPkg gnomod.Pkg) []testPkg {
52+
// listPkgs lists all packages in rootMod
53+
func listPkgs(rootMod gnomod.Pkg) []testPkg {
5454
res := []testPkg{}
55-
rootDir := rootPkg.Dir
55+
rootDir := rootMod.Dir
5656
visited := map[string]struct{}{}
5757
if err := fs.WalkDir(os.DirFS(rootDir), ".", func(path string, d fs.DirEntry, err error) error {
5858
if d.IsDir() {
@@ -68,7 +68,7 @@ func listPkgs(rootPkg gnomod.Pkg) []testPkg {
6868
}
6969
visited[dir] = struct{}{}
7070

71-
subPkgPath := pathlib.Join(rootPkg.Name, subPath)
71+
subPkgPath := pathlib.Join(rootMod.Name, subPath)
7272

7373
pkg := testPkg{
7474
Dir: dir,
@@ -100,26 +100,34 @@ func fileImportsToStrings(fis []packages.FileImport) []string {
100100
return res
101101
}
102102

103-
// detectCycles detect import cycles
103+
// detectCycles detects import cycles
104+
//
105+
// We need to check
106+
// 3 kinds of nodes
104107
//
105-
// We need to check 3 kinds of nodes
106108
// - normal pkg: compiled source
109+
//
107110
// - xtest pkg: external test source (include xtests and filetests), can be treated as their own package
108-
// - test pkg: embedded test sources, these should not have their corresponding normal package in the stack
109111
//
110-
// the tricky thing is that we need to split test sources and normal source
112+
// - test pkg: embedded test sources,
113+
// these should not have their corresponding normal package in their dependencies tree
114+
//
115+
// The tricky thing is that we need to split test sources and normal source
111116
// while not considering them as distincitive packages.
112-
// Because if we don't we will have false positive if for example we have these edges:
117+
// Because if we don't we will have false positive for example if we have these edges:
118+
//
119+
// - foo_pkg/foo_test.go imports bar_pkg
120+
//
121+
// - bar_pkg/bar_test.go import foo_pkg
113122
//
114-
// foo_pkg/foo_test.go -> bar_pkg/bar.go
115-
// bar_pkg/bar_test.go -> foo_pkg/foo.go
123+
// In go, the above example is allowed
124+
// but the following is not
116125
//
117-
// # The above example if allowed but the following is not
126+
// - foo_pkg/foo.go imports bar_pkg
118127
//
119-
// foo_pkg/foo.go -> bar_pkg/bar.go
120-
// bar_pkg/bar_test.go -> foo_pkg/foo.go
128+
// - bar_pkg/bar_test.go imports foo_pkg
121129
//
122-
// This implementation can probably be optimized with better graph theory
130+
// This implementation can be optimized with better graph theory
123131
func detectCycles(root testPkg, pkgs []testPkg) {
124132
// check normal cycles
125133
{
@@ -218,6 +226,7 @@ func visitPackage(pkg testPkg, pkgs []testPkg, visited map[string]bool, stack []
218226
return nil
219227
}
220228

229+
// readPkg reads the sources of a package. It includes all .gno files but ignore the package name
221230
func readPkg(dir string, pkgPath string) (*gnovm.MemPackage, error) {
222231
list, err := os.ReadDir(dir)
223232
if err != nil {

0 commit comments

Comments
 (0)