Skip to content

Commit

Permalink
chore: revert pkg change
Browse files Browse the repository at this point in the history
Signed-off-by: Norman <[email protected]>
  • Loading branch information
Norman authored and Norman committed Dec 25, 2024
1 parent 1f7d1fc commit 7c11caa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
27 changes: 11 additions & 16 deletions gnovm/pkg/gnomod/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/fs"
"os"
"path/filepath"
"slices"
"strings"

"github.com/gnolang/gno/gnovm"
Expand Down Expand Up @@ -39,45 +38,39 @@ type (
// sortPkgs sorts the given packages by their dependencies.
func (pl PkgList) Sort() (SortedPkgList, error) {
visited := make(map[string]bool)
stack := []string{}
onStack := make(map[string]bool)
sortedPkgs := make([]Pkg, 0, len(pl))

// Visit all packages
for _, p := range pl {
if err := visitPackage(p, pl, visited, stack, &sortedPkgs); err != nil {
if err := visitPackage(p, pl, visited, onStack, &sortedPkgs); err != nil {
return nil, err
}
}

return sortedPkgs, nil
}

var injectedTestingLibs = []string{"encoding/json", "fmt", "os", "internal/os_test"}

// visitNode visits a package's and its dependencies dependencies and adds them to the sorted list.
func visitPackage(pkg Pkg, pkgs []Pkg, visited map[string]bool, stack []string, sortedPkgs *[]Pkg) error {
if slices.Contains(stack, pkg.Name) {
return fmt.Errorf("cycle detected: %s -> %s", strings.Join(stack, " -> "), pkg.Name)
func visitPackage(pkg Pkg, pkgs []Pkg, visited, onStack map[string]bool, sortedPkgs *[]Pkg) error {
if onStack[pkg.Name] {
return fmt.Errorf("cycle detected: %s", pkg.Name)
}
if visited[pkg.Name] {
return nil
}

visited[pkg.Name] = true
stack = append(stack, pkg.Name)
onStack[pkg.Name] = true

// Visit package's dependencies
for _, imp := range pkg.Imports {
if slices.Contains(injectedTestingLibs, imp) {
continue
}

found := false
for _, p := range pkgs {
if p.Name != imp {
continue
}
if err := visitPackage(p, pkgs, visited, stack, sortedPkgs); err != nil {
if err := visitPackage(p, pkgs, visited, onStack, sortedPkgs); err != nil {
return err
}
found = true
Expand All @@ -88,6 +81,7 @@ func visitPackage(pkg Pkg, pkgs []Pkg, visited map[string]bool, stack []string,
}
}

onStack[pkg.Name] = false
*sortedPkgs = append(*sortedPkgs, pkg)
return nil
}
Expand Down Expand Up @@ -136,8 +130,9 @@ func ListPkgs(root string) (PkgList, error) {

imports := make([]string, 0, len(importsRaw))
for _, imp := range importsRaw {
// remove standard libraries from imports
if !gnolang.IsStdlib(imp.PkgPath) {
// remove self and standard libraries from imports
if imp.PkgPath != gnoMod.Module.Mod.Path &&
!gnolang.IsStdlib(imp.PkgPath) {
imports = append(imports, imp.PkgPath)
}
}
Expand Down
6 changes: 0 additions & 6 deletions gnovm/pkg/gnomod/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ func TestSortPkgs(t *testing.T) {
{Name: "pkg2", Dir: "/path/to/pkg2", Imports: []string{"pkg1"}},
},
shouldErr: true,
}, {
desc: "self_import",
in: []Pkg{
{Name: "pkg1", Dir: "/path/to/pkg1", Imports: []string{"pkg1"}},
},
shouldErr: true,
}, {
desc: "missing_dependencies",
in: []Pkg{
Expand Down

0 comments on commit 7c11caa

Please sign in to comment.