Skip to content

Commit

Permalink
chore: remove old pkg loader
Browse files Browse the repository at this point in the history
Signed-off-by: Norman <[email protected]>
  • Loading branch information
n0izn0iz committed Jan 8, 2025
1 parent edba346 commit 78b60ed
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 202 deletions.
13 changes: 8 additions & 5 deletions contribs/gnodev/pkg/dev/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type NodeConfig struct {
MaxGasPerBlock int64
ChainID string
ChainDomain string
LoadConfig *packages.LoadConfig
}

func DefaultNodeConfig(rootdir, domain string) *NodeConfig {
Expand All @@ -70,6 +71,7 @@ func DefaultNodeConfig(rootdir, domain string) *NodeConfig {
TMConfig: tmc,
SkipFailingGenesisTxs: true,
MaxGasPerBlock: 10_000_000_000,
LoadConfig: &packages.LoadConfig{Deps: true},
}
}

Expand Down Expand Up @@ -98,7 +100,7 @@ type Node struct {
var DefaultFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))

func NewDevNode(ctx context.Context, cfg *NodeConfig) (*Node, error) {
mpkgs, err := NewPackagesMap(cfg.PackagesPathList)
mpkgs, err := NewPackagesMap(cfg.LoadConfig, cfg.PackagesPathList)
if err != nil {
return nil, fmt.Errorf("unable map pkgs list: %w", err)
}
Expand Down Expand Up @@ -140,7 +142,7 @@ func (n *Node) Close() error {
return n.Node.Stop()
}

func (n *Node) ListPkgs() []packages.Pkg {
func (n *Node) ListPkgs() []*packages.Package {
n.muNode.RLock()
defer n.muNode.RUnlock()

Expand Down Expand Up @@ -240,20 +242,21 @@ func (n *Node) updatePackages(paths ...string) error {
}

// List all packages from target path
pkgslist, err := packages.ListPkgs(abspath)
loadCfg := &packages.LoadConfig{Deps: true}
pkgslist, err := packages.Load(loadCfg, filepath.Join(abspath, "..."))
if err != nil {
return fmt.Errorf("failed to list gno packages for %q: %w", path, err)
}

// Update or add package in the current known list.
for _, pkg := range pkgslist {
n.pkgs[pkg.Dir] = Package{
Pkg: pkg,
Package: pkg,
Creator: deployer,
Deposit: deposit,
}

n.logger.Debug("pkgs update", "name", pkg.Name, "path", pkg.Dir)
n.logger.Debug("pkgs update", "name", pkg.ImportPath, "path", pkg.Dir)
}

pkgsUpdated += len(pkgslist)
Expand Down
14 changes: 7 additions & 7 deletions contribs/gnodev/pkg/dev/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ResolvePackagePathQuery(bk *address.Book, path string) (PackagePath, error)
}

type Package struct {
packages.Pkg
*packages.Package
Creator crypto.Address
Deposit std.Coins
}
Expand All @@ -75,7 +75,7 @@ var (
ErrEmptyDepositPackage = errors.New("no deposit specified for package")
)

func NewPackagesMap(ppaths []PackagePath) (PackagesMap, error) {
func NewPackagesMap(cfg *packages.LoadConfig, ppaths []PackagePath) (PackagesMap, error) {
pkgs := make(map[string]Package)
for _, ppath := range ppaths {
if ppath.Creator.IsZero() {
Expand All @@ -88,7 +88,7 @@ func NewPackagesMap(ppaths []PackagePath) (PackagesMap, error) {
}

// list all packages from target path
pkgslist, err := packages.ListPkgs(abspath)
pkgslist, err := packages.Load(cfg, filepath.Join(abspath, "..."))
if err != nil {
return nil, fmt.Errorf("listing gno packages: %w", err)
}
Expand All @@ -102,7 +102,7 @@ func NewPackagesMap(ppaths []PackagePath) (PackagesMap, error) {
continue // skip
}
pkgs[pkg.Dir] = Package{
Pkg: pkg,
Package: pkg,
Creator: ppath.Creator,
Deposit: ppath.Deposit,
}
Expand All @@ -113,9 +113,9 @@ func NewPackagesMap(ppaths []PackagePath) (PackagesMap, error) {
}

func (pm PackagesMap) toList() packages.PkgList {
list := make([]packages.Pkg, 0, len(pm))
list := make([]*packages.Package, 0, len(pm))
for _, pkg := range pm {
list = append(list, pkg.Pkg)
list = append(list, pkg.Package)
}
return list
}
Expand All @@ -138,7 +138,7 @@ func (pm PackagesMap) Load(fee std.Fee, start time.Time) ([]gnoland.TxWithMetada
}

// Open files in directory as MemPackage.
memPkg := gno.MustReadMemPackage(modPkg.Dir, modPkg.Name)
memPkg := gno.MustReadMemPackage(modPkg.Dir, modPkg.ImportPath)
if err := memPkg.Validate(); err != nil {
return nil, fmt.Errorf("invalid package: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion contribs/gnodev/pkg/watcher/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (p *PackageWatcher) Stop() {
// Packages are sorted by their length in descending order to facilitate easier
// and more efficient matching with corresponding paths. The longest paths are
// compared first.
func (p *PackageWatcher) AddPackages(pkgs ...packages.Pkg) error {
func (p *PackageWatcher) AddPackages(pkgs ...*packages.Package) error {
for _, pkg := range pkgs {
dir := pkg.Dir

Expand Down
8 changes: 5 additions & 3 deletions gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot"
"github.com/gnolang/gno/gno.land/pkg/log"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/gnovm/pkg/packages"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/bft/node"
Expand Down Expand Up @@ -340,7 +341,7 @@ func lazyInitGenesis(
}

// Generate the new genesis.json file
if err := generateGenesisFile(genesisPath, publicKey, c); err != nil {
if err := generateGenesisFile(io, genesisPath, publicKey, c); err != nil {
return fmt.Errorf("unable to generate genesis file, %w", err)
}

Expand All @@ -365,7 +366,7 @@ func initializeLogger(io io.WriteCloser, logLevel, logFormat string) (*zap.Logge
return log.GetZapLoggerFn(format)(io, level), nil
}

func generateGenesisFile(genesisFile string, pk crypto.PubKey, c *startCfg) error {
func generateGenesisFile(io commands.IO, genesisFile string, pk crypto.PubKey, c *startCfg) error {
gen := &bft.GenesisDoc{}
gen.GenesisTime = time.Now()
gen.ChainID = c.chainID
Expand Down Expand Up @@ -396,7 +397,8 @@ func generateGenesisFile(genesisFile string, pk crypto.PubKey, c *startCfg) erro

// Load examples folder
examplesDir := filepath.Join(c.gnoRootDir, "examples")
pkgsTxs, err := gnoland.LoadPackagesFromDir(examplesDir, genesisDeployAddress, genesisDeployFee)
loadCfg := &packages.LoadConfig{IO: io}
pkgsTxs, err := gnoland.LoadPackagesFromDir(loadCfg, examplesDir, genesisDeployAddress, genesisDeployFee)
if err != nil {
return fmt.Errorf("unable to load examples folder: %w", err)
}
Expand Down
9 changes: 5 additions & 4 deletions gno.land/pkg/gnoland/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gnoland
import (
"errors"
"fmt"
"path/filepath"
"strings"

vmm "github.com/gnolang/gno/gno.land/pkg/sdk/vm"
Expand Down Expand Up @@ -136,9 +137,9 @@ func LoadGenesisTxsFile(path string, chainID string, genesisRemote string) ([]Tx

// LoadPackagesFromDir loads gno packages from a directory.
// It creates and returns a list of transactions based on these packages.
func LoadPackagesFromDir(dir string, creator bft.Address, fee std.Fee) ([]TxWithMetadata, error) {
func LoadPackagesFromDir(cfg *packages.LoadConfig, dir string, creator bft.Address, fee std.Fee) ([]TxWithMetadata, error) {
// list all packages from target path
pkgs, err := packages.ListPkgs(dir)
pkgs, err := packages.Load(cfg, filepath.Join(dir, "..."))
if err != nil {
return nil, fmt.Errorf("listing gno packages: %w", err)
}
Expand Down Expand Up @@ -167,11 +168,11 @@ func LoadPackagesFromDir(dir string, creator bft.Address, fee std.Fee) ([]TxWith
}

// LoadPackage loads a single package into a `std.Tx`
func LoadPackage(pkg packages.Pkg, creator bft.Address, fee std.Fee, deposit std.Coins) (std.Tx, error) {
func LoadPackage(pkg *packages.Package, creator bft.Address, fee std.Fee, deposit std.Coins) (std.Tx, error) {
var tx std.Tx

// Open files in directory as MemPackage.
memPkg := gno.MustReadMemPackage(pkg.Dir, pkg.Name)
memPkg := gno.MustReadMemPackage(pkg.Dir, pkg.ImportPath)
err := memPkg.Validate()
if err != nil {
return tx, fmt.Errorf("invalid package: %w", err)
Expand Down
4 changes: 3 additions & 1 deletion gno.land/pkg/integration/node_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot"
"github.com/gnolang/gno/gnovm/pkg/packages"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
tmcfg "github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/bft/node"
Expand Down Expand Up @@ -134,7 +135,8 @@ func LoadDefaultPackages(t TestingTS, creator bft.Address, gnoroot string) []gno
examplesDir := filepath.Join(gnoroot, "examples")

defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
txs, err := gnoland.LoadPackagesFromDir(examplesDir, creator, defaultFee)
cfg := &packages.LoadConfig{SelfContained: true}
txs, err := gnoland.LoadPackagesFromDir(cfg, examplesDir, creator, defaultFee)
require.NoError(t, err)

return txs
Expand Down
84 changes: 20 additions & 64 deletions gno.land/pkg/integration/pkgloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/gnovm/pkg/packages"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/std"
)

type PkgsLoader struct {
pkgs []packages.Pkg
pkgs []*packages.Package
visited map[string]struct{}

// list of occurrences to patchs with the given value
Expand All @@ -25,7 +23,7 @@ type PkgsLoader struct {

func NewPkgsLoader() *PkgsLoader {
return &PkgsLoader{
pkgs: make([]packages.Pkg, 0),
pkgs: make([]*packages.Package, 0),
visited: make(map[string]struct{}),
patchs: make(map[string]string),
}
Expand All @@ -49,7 +47,7 @@ func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std
for i, pkg := range pkgslist {
tx, err := gnoland.LoadPackage(pkg, creator, fee, deposit)
if err != nil {
return nil, fmt.Errorf("unable to load pkg %q: %w", pkg.Name, err)
return nil, fmt.Errorf("unable to load pkg %q: %w", pkg.ImportPath, err)
}

// If any replace value is specified, apply them
Expand Down Expand Up @@ -82,7 +80,8 @@ func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std

func (pl *PkgsLoader) LoadAllPackagesFromDir(path string) error {
// list all packages from target path
pkgslist, err := packages.ListPkgs(path)
cfg := &packages.LoadConfig{SelfContained: true}
pkgslist, err := packages.Load(cfg, filepath.Join(path, "..."))
if err != nil {
return fmt.Errorf("listing gno packages: %w", err)
}
Expand All @@ -96,74 +95,31 @@ func (pl *PkgsLoader) LoadAllPackagesFromDir(path string) error {
return nil
}

func (pl *PkgsLoader) LoadPackage(modroot string, path, name string) error {
// Initialize a queue with the root package
queue := []packages.Pkg{{Dir: path, Name: name}}

for len(queue) > 0 {
// Dequeue the first package
currentPkg := queue[0]
queue = queue[1:]

if currentPkg.Dir == "" {
return fmt.Errorf("no path specified for package")
}

if currentPkg.Name == "" {
// Load `gno.mod` information
gnoModPath := filepath.Join(currentPkg.Dir, "gno.mod")
gm, err := gnomod.ParseGnoMod(gnoModPath)
if err != nil {
return fmt.Errorf("unable to load %q: %w", gnoModPath, err)
}
gm.Sanitize()

// Override package info with mod infos
currentPkg.Name = gm.Module.Mod.Path
currentPkg.Draft = gm.Draft

pkg, err := gnolang.ReadMemPackage(currentPkg.Dir, currentPkg.Name)
if err != nil {
return fmt.Errorf("unable to read package at %q: %w", currentPkg.Dir, err)
}
importsMap, err := packages.Imports(pkg, nil)
if err != nil {
return fmt.Errorf("unable to load package imports in %q: %w", currentPkg.Dir, err)
}
imports := importsMap.Merge(packages.FileKindPackageSource, packages.FileKindTest, packages.FileKindFiletest)
for _, imp := range imports {
if imp == currentPkg.Name || gnolang.IsStdlib(imp) {
continue
}
currentPkg.Imports = append(currentPkg.Imports, imp)
}
}

if currentPkg.Draft {
continue // Skip draft package
}
func (pl *PkgsLoader) LoadPackage(modroot string, path string, name string) error {
// FIXME: name override
cfg := &packages.LoadConfig{Deps: true, SelfContained: true}
pkgDir := filepath.Join(modroot, path)
pkgs, err := packages.Load(cfg, pkgDir)
if err != nil {
return fmt.Errorf("%q: loading: %w", pkgDir, err)
}

if pl.exist(currentPkg) {
for _, pkg := range pkgs {
if pl.exist(pkg) {
continue
}
pl.add(currentPkg)

// Add requirements to the queue
for _, pkgPath := range currentPkg.Imports {
fullPath := filepath.Join(modroot, pkgPath)
queue = append(queue, packages.Pkg{Dir: fullPath})
}
pl.add(pkg)
}

return nil
}

func (pl *PkgsLoader) add(pkg packages.Pkg) {
pl.visited[pkg.Name] = struct{}{}
func (pl *PkgsLoader) add(pkg *packages.Package) {
pl.visited[pkg.ImportPath] = struct{}{}
pl.pkgs = append(pl.pkgs, pkg)
}

func (pl *PkgsLoader) exist(pkg packages.Pkg) (ok bool) {
_, ok = pl.visited[pkg.Name]
func (pl *PkgsLoader) exist(pkg *packages.Package) (ok bool) {
_, ok = pl.visited[pkg.ImportPath]
return
}
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ func execModTidy(cfg *modTidyCfg, args []string, io commands.IO) error {
}

if cfg.recursive {
pkgs, err := packages.ListPkgs(wd)
loadCfg := packages.LoadConfig{IO: io, Fetcher: testPackageFetcher}
pkgs, err := packages.Load(&loadCfg, filepath.Join(wd, "..."))
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions gnovm/pkg/packages/expand_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func expandPatterns(conf *LoadConfig, patterns ...string) ([]*pkgMatch, error) {

case patternKindRecursiveRemote:
return nil, fmt.Errorf("%s: recursive remote patterns are not supported", match)

case patternKindRemote:
if conf.SelfContained {
return nil, fmt.Errorf("%s: remote patterns are not supported in self-contained mode", match)
}
}

pat, err := cleanPattern(match, patKind)
Expand Down
9 changes: 8 additions & 1 deletion gnovm/pkg/packages/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type LoadConfig struct {
Deps bool
Cache map[string]*Package
GnorootExamples bool // allow loading packages from gnoroot examples if not found in the set
SelfContained bool
}

func (conf *LoadConfig) applyDefaults() {
Expand All @@ -36,7 +37,7 @@ func (conf *LoadConfig) applyDefaults() {
}
}

func Load(conf *LoadConfig, patterns ...string) ([]*Package, error) {
func Load(conf *LoadConfig, patterns ...string) (PkgList, error) {
conf.applyDefaults()

expanded, err := expandPatterns(conf, patterns...)
Expand Down Expand Up @@ -115,6 +116,12 @@ func Load(conf *LoadConfig, patterns ...string) ([]*Package, error) {
}
}

if conf.SelfContained {
pkg.Error = errors.Join(pkg.Error, fmt.Errorf("self-contained: package %q not found", imp))
byPkgPath[imp] = nil // stop trying to get this lib, we can't
continue
}

dir := gnomod.PackageDir("", module.Version{Path: imp})
if err := downloadPackage(conf, imp, dir); err != nil {
pkg.Error = errors.Join(pkg.Error, err)
Expand Down
Loading

0 comments on commit 78b60ed

Please sign in to comment.