Skip to content

Commit

Permalink
fix(sync): do not update vcs info of failed packages (Jguer#1901)
Browse files Browse the repository at this point in the history
* extract srcinfo service to pkg

* take into account failed installs for vcs update. Fixes Jguer#1892

* fix tests
  • Loading branch information
Jguer authored Jan 23, 2023
1 parent 04c4b0a commit 1bfbd01
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 131 deletions.
50 changes: 17 additions & 33 deletions aur_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"sync"

"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/dep"
Expand All @@ -15,41 +14,40 @@ import (
"github.com/Jguer/yay/v11/pkg/text"
"github.com/Jguer/yay/v11/pkg/vcs"

gosrc "github.com/Morganamilo/go-srcinfo"
mapset "github.com/deckarep/golang-set/v2"
"github.com/leonelquinteros/gotext"
)

type (
PostInstallHookFunc func(ctx context.Context) error
Installer struct {
dbExecutor db.Executor
postInstallHooks []PostInstallHookFunc
failedAndIngnored map[string]error
exeCmd exe.ICmdBuilder
vcsStore vcs.Store
targetMode parser.TargetMode
dbExecutor db.Executor
postInstallHooks []PostInstallHookFunc
failedAndIgnored map[string]error
exeCmd exe.ICmdBuilder
vcsStore vcs.Store
targetMode parser.TargetMode
}
)

func NewInstaller(dbExecutor db.Executor, exeCmd exe.ICmdBuilder, vcsStore vcs.Store, targetMode parser.TargetMode) *Installer {
return &Installer{
dbExecutor: dbExecutor,
postInstallHooks: []PostInstallHookFunc{},
failedAndIngnored: map[string]error{},
exeCmd: exeCmd,
vcsStore: vcsStore,
targetMode: targetMode,
dbExecutor: dbExecutor,
postInstallHooks: []PostInstallHookFunc{},
failedAndIgnored: map[string]error{},
exeCmd: exeCmd,
vcsStore: vcsStore,
targetMode: targetMode,
}
}

func (installer *Installer) CompileFailedAndIgnored() error {
if len(installer.failedAndIngnored) == 0 {
if len(installer.failedAndIgnored) == 0 {
return nil
}

return &FailedIgnoredPkgError{
pkgErrors: installer.failedAndIngnored,
pkgErrors: installer.failedAndIgnored,
}
}

Expand Down Expand Up @@ -77,11 +75,10 @@ func (installer *Installer) Install(ctx context.Context,
cmdArgs *parser.Arguments,
targets []map[string]*dep.InstallInfo,
pkgBuildDirs map[string]string,
srcinfos map[string]*gosrc.Srcinfo,
) error {
// Reorganize targets into layers of dependencies
for i := len(targets) - 1; i >= 0; i-- {
err := installer.handleLayer(ctx, cmdArgs, targets[i], pkgBuildDirs, srcinfos, i == 0)
err := installer.handleLayer(ctx, cmdArgs, targets[i], pkgBuildDirs, i == 0)
if err != nil {
// rollback
return err
Expand All @@ -95,7 +92,6 @@ func (installer *Installer) handleLayer(ctx context.Context,
cmdArgs *parser.Arguments,
layer map[string]*dep.InstallInfo,
pkgBuildDirs map[string]string,
srcinfos map[string]*gosrc.Srcinfo,
lastLayer bool,
) error {
// Install layer
Expand Down Expand Up @@ -142,7 +138,7 @@ func (installer *Installer) handleLayer(ctx context.Context,
}

errAur := installer.installAURPackages(ctx, cmdArgs, aurDeps, aurExp,
nameToBaseMap, pkgBuildDirs, true, srcinfos, lastLayer)
nameToBaseMap, pkgBuildDirs, true, lastLayer)

return errAur
}
Expand All @@ -152,7 +148,6 @@ func (installer *Installer) installAURPackages(ctx context.Context,
aurDepNames, aurExpNames mapset.Set[string],
nameToBase, pkgBuildDirsByBase map[string]string,
installIncompatible bool,
srcinfos map[string]*gosrc.Srcinfo,
lastLayer bool,
) error {
all := aurDepNames.Union(aurExpNames).ToSlice()
Expand All @@ -163,8 +158,6 @@ func (installer *Installer) installAURPackages(ctx context.Context,
deps, exps := make([]string, 0, aurDepNames.Cardinality()), make([]string, 0, aurExpNames.Cardinality())
pkgArchives := make([]string, 0, len(exps)+len(deps))

var wg sync.WaitGroup

for _, name := range all {
base := nameToBase[name]
dir := pkgBuildDirsByBase[base]
Expand All @@ -175,7 +168,7 @@ func (installer *Installer) installAURPackages(ctx context.Context,
return fmt.Errorf("%s - %w", gotext.Get("error making: %s", base), errMake)
}

installer.failedAndIngnored[name] = errMake
installer.failedAndIgnored[name] = errMake
text.Errorln(gotext.Get("error making: %s", base), "-", errMake)
continue
}
Expand All @@ -201,17 +194,8 @@ func (installer *Installer) installAURPackages(ctx context.Context,
if hasDebug {
deps = append(deps, name+"-debug")
}

srcinfo := srcinfos[base]
wg.Add(1)
go func(name string) {
installer.vcsStore.Update(ctx, name, srcinfo.Source)
wg.Done()
}(name)
}

wg.Wait()

if err := installPkgArchive(ctx, installer.exeCmd, installer.targetMode, installer.vcsStore, cmdArgs, pkgArchives); err != nil {
return fmt.Errorf("%s - %w", fmt.Sprintf(gotext.Get("error installing:")+" %v", pkgArchives), err)
}
Expand Down
16 changes: 4 additions & 12 deletions aur_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"testing"

gosrc "github.com/Morganamilo/go-srcinfo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -138,8 +137,6 @@ func TestInstaller_InstallNeeded(t *testing.T) {
"yay": tmpDir,
}

srcInfos := map[string]*gosrc.Srcinfo{"yay": {}}

targets := []map[string]*dep.InstallInfo{
{
"yay": {
Expand All @@ -152,7 +149,7 @@ func TestInstaller_InstallNeeded(t *testing.T) {
},
}

errI := installer.Install(context.Background(), cmdArgs, targets, pkgBuildDirs, srcInfos)
errI := installer.Install(context.Background(), cmdArgs, targets, pkgBuildDirs)
require.NoError(td, errI)

require.Len(td, mockRunner.ShowCalls, len(tc.wantShow))
Expand Down Expand Up @@ -414,9 +411,7 @@ func TestInstaller_InstallMixedSourcesAndLayers(t *testing.T) {
"jellyfin": tmpDirJfin,
}

srcInfos := map[string]*gosrc.Srcinfo{"yay": {}, "jellyfin": {}}

errI := installer.Install(context.Background(), cmdArgs, tc.targets, pkgBuildDirs, srcInfos)
errI := installer.Install(context.Background(), cmdArgs, tc.targets, pkgBuildDirs)
require.NoError(td, errI)

require.Len(td, mockRunner.ShowCalls, len(tc.wantShow))
Expand Down Expand Up @@ -570,8 +565,7 @@ func TestInstaller_CompileFailed(t *testing.T) {
"yay": tmpDir,
}

srcInfos := map[string]*gosrc.Srcinfo{"yay": {}}
errI := installer.Install(context.Background(), cmdArgs, tc.targets, pkgBuildDirs, srcInfos)
errI := installer.Install(context.Background(), cmdArgs, tc.targets, pkgBuildDirs)
if tc.lastLayer {
require.NoError(td, errI) // last layer error
} else {
Expand Down Expand Up @@ -728,9 +722,7 @@ func TestInstaller_InstallSplitPackage(t *testing.T) {
"jellyfin": tmpDir,
}

srcInfos := map[string]*gosrc.Srcinfo{"jellyfin": {}}

errI := installer.Install(context.Background(), cmdArgs, tc.targets, pkgBuildDirs, srcInfos)
errI := installer.Install(context.Background(), cmdArgs, tc.targets, pkgBuildDirs)
require.NoError(td, errI)

require.Len(td, mockRunner.ShowCalls, len(tc.wantShow))
Expand Down
33 changes: 5 additions & 28 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/Jguer/yay/v11/pkg/settings"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/srcinfo"
"github.com/Jguer/yay/v11/pkg/stringset"
"github.com/Jguer/yay/v11/pkg/text"
"github.com/Jguer/yay/v11/pkg/vcs"
Expand Down Expand Up @@ -282,7 +283,7 @@ func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Execu
return errM
}

srcinfos, err = parseSrcinfoFiles(pkgbuildDirs, true)
srcinfos, err = srcinfo.ParseSrcinfoFilesByBase(pkgbuildDirs, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -537,30 +538,6 @@ func parsePackageList(ctx context.Context, cmdBuilder exe.ICmdBuilder,
return pkgdests, pkgVersion, nil
}

func parseSrcinfoFiles(pkgBuildDirs map[string]string, errIsFatal bool) (map[string]*gosrc.Srcinfo, error) {
srcinfos := make(map[string]*gosrc.Srcinfo)

k := 0
for base, dir := range pkgBuildDirs {
text.OperationInfoln(gotext.Get("(%d/%d) Parsing SRCINFO: %s", k+1, len(pkgBuildDirs), text.Cyan(base)))

pkgbuild, err := gosrc.ParseFile(filepath.Join(dir, ".SRCINFO"))
if err != nil {
if !errIsFatal {
text.Warnln(gotext.Get("failed to parse %s -- skipping: %s", base, err))
continue
}

return nil, errors.New(gotext.Get("failed to parse %s: %s", base, err))
}

srcinfos[base] = pkgbuild
k++
}

return srcinfos, nil
}

func pkgbuildsToSkip(bases []dep.Base, targets stringset.StringSet) stringset.StringSet {
toSkip := make(stringset.StringSet)

Expand Down Expand Up @@ -682,7 +659,7 @@ func buildInstallPkgbuilds(
}
}

srcinfo := srcinfos[pkg]
srcInfo := srcinfos[pkg]

args := []string{"--nobuild", "-fC"}

Expand Down Expand Up @@ -797,7 +774,7 @@ func buildInstallPkgbuilds(
var wg sync.WaitGroup

for _, pkg := range base {
if srcinfo == nil {
if srcInfo == nil {
text.Errorln(gotext.Get("could not find srcinfo for: %s", pkg.Name))
break
}
Expand All @@ -806,7 +783,7 @@ func buildInstallPkgbuilds(

text.Debugln("checking vcs store for:", pkg.Name)
go func(name string) {
config.Runtime.VCSStore.Update(ctx, name, srcinfo.Source)
config.Runtime.VCSStore.Update(ctx, name, srcInfo.Source)
wg.Done()
}(pkg.Name)
}
Expand Down
1 change: 1 addition & 0 deletions local_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestIntegrationLocalInstall(t *testing.T) {
}

config := &settings.Configuration{
RemoveMake: "no",
Runtime: &settings.Runtime{
CmdBuilder: cmdBuilder,
VCSStore: &vcs.Mock{},
Expand Down
Loading

0 comments on commit 1bfbd01

Please sign in to comment.