Add safety check before package pruning
Verifies all state packages are installed locally before running markAllAsDeps and orphan cleanup.
This commit is contained in:
@@ -84,6 +84,12 @@ func Sync(packages []string, noCheck bool, prune bool) (*output.Result, error) {
|
|||||||
|
|
||||||
var removed int
|
var removed int
|
||||||
if prune {
|
if prune {
|
||||||
|
log.Debug("Sync: running prune sanity check...")
|
||||||
|
if err := pruneSanityCheck(packages); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
log.Debug("Sync: prune sanity check passed (%.2fs)", time.Since(start).Seconds())
|
||||||
|
|
||||||
log.Debug("Sync: marking all as deps...")
|
log.Debug("Sync: marking all as deps...")
|
||||||
if err := markAllAsDeps(); err != nil {
|
if err := markAllAsDeps(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -183,3 +189,29 @@ func markAllAsDeps() error {
|
|||||||
log.Debug("markAllAsDeps: done (%.2fs)", time.Since(start).Seconds())
|
log.Debug("markAllAsDeps: done (%.2fs)", time.Since(start).Seconds())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pruneSanityCheck checks if the installation of all state packages succeeded,
|
||||||
|
// before attempting to do package marking and orphan cleanup.
|
||||||
|
func pruneSanityCheck(statePackages []string) error {
|
||||||
|
start := time.Now()
|
||||||
|
log.Debug("pruneSanityCheck: starting...")
|
||||||
|
|
||||||
|
localPackages, err := read.List()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to list local packages: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var missing []string
|
||||||
|
for _, pkg := range statePackages {
|
||||||
|
if !slices.Contains(localPackages, pkg) {
|
||||||
|
missing = append(missing, pkg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(missing) > 0 {
|
||||||
|
return fmt.Errorf("safety check: missing state packages: %v", missing)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug("pruneSanityCheck: done (%.2fs)", time.Since(start).Seconds())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user