Skip to content

Commit 8f8d433

Browse files
authored
fix(packages): continue past failed installs; write MigrationReport on restore (#115)
Closes #103. Squash-merged per CONTRIBUTING.md.
1 parent 1b137e6 commit 8f8d433

6 files changed

Lines changed: 580 additions & 31 deletions

File tree

‎internal/cli/packages.go‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,52 @@ func runRestore(cmd *cobra.Command, args []string) error {
160160
}
161161
}
162162

163+
// writeReport persists a MigrationReport so the user has a record
164+
// of what migrated. We only write when at least one package was
165+
// attempted — skipping the write on an empty results slice avoids
166+
// producing a useless "0 packages attempted" file for fresh installs
167+
// with no globals. The snapshot's recorded Manager / NodeVersion
168+
// are preferred over the CLI's inferred values when present, so the
169+
// --from branch (which has no CLI-side manager argument) gets an
170+
// accurate report.
171+
writeReport := func(snapshot packages.SnapshotData, fallbackMgr, fallbackFrom string, results []packages.PackageResult) {
172+
if len(results) == 0 {
173+
return
174+
}
175+
mgr := snapshot.Manager
176+
if mgr == "" {
177+
mgr = fallbackMgr
178+
}
179+
fromVer := snapshot.NodeVersion
180+
if fromVer == "" {
181+
fromVer = fallbackFrom
182+
}
183+
report := packages.NewMigrationReport(mgr, fromVer, "")
184+
for _, r := range results {
185+
report.AddResult(r)
186+
}
187+
if err := report.Save(); err != nil {
188+
cmd.Printf("Warning: failed to write migration report: %v\n", err)
189+
return
190+
}
191+
if p, perr := report.Path(); perr == nil {
192+
cmd.Printf("Migration report: %s\n", p)
193+
}
194+
}
195+
163196
// --from branch: read the path straight off disk, no manager or
164197
// version parsing required.
165198
if fromPath != "" {
166-
if err := packages.RestoreFromSnapshot(ctx, fromPath); err != nil {
199+
outcome, err := packages.RestoreFromSnapshot(ctx, fromPath)
200+
if err != nil {
201+
// Even on partial failure, persist whatever results we
202+
// collected so the user has a file to inspect — the
203+
// returned slice still contains entries for every package
204+
// attempted, including failures (status="failed").
205+
writeReport(outcome.Snapshot, "", "", outcome.Results)
167206
return fmt.Errorf("restore failed: %w", err)
168207
}
208+
writeReport(outcome.Snapshot, "", "", outcome.Results)
169209
clearSentinel()
170210
cmd.Printf("Restored packages from %s\n", fromPath)
171211
return nil
@@ -192,9 +232,15 @@ func runRestore(cmd *cobra.Command, args []string) error {
192232
return fmt.Errorf("invalid version: %w", err)
193233
}
194234

195-
if err := packages.Restore(ctx, managerName, *v); err != nil {
235+
outcome, err := packages.Restore(ctx, managerName, *v)
236+
if err != nil {
237+
// Partial failure: persist a report (with the manager name
238+
// and source version so the user can correlate) so they know
239+
// which packages to retry.
240+
writeReport(outcome.Snapshot, managerName, versionStr, outcome.Results)
196241
return fmt.Errorf("restore failed: %w", err)
197242
}
243+
writeReport(outcome.Snapshot, managerName, versionStr, outcome.Results)
198244
clearSentinel()
199245

200246
cmd.Printf("Restored packages for %s %s\n", managerName, versionStr)

‎internal/cli/upgrade.go‎

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ func runUpgrade(cmd *cobra.Command, args []string) error {
9696
// on disk so a follow-up `nodeup packages restore --from <path>`
9797
// (the command printed by PersistentPreRunE's hint) can still find
9898
// the resume breadcrumb. See issue #46.
99+
//
100+
// "Failure" here includes PARTIAL failure: if one or more packages
101+
// failed to install but the loop did complete (which it now does —
102+
// see #103), restoreSucceeded stays false. The MigrationReport file
103+
// is written either way; the sentinel only clears on a fully-successful
104+
// migration so a follow-up `nodeup packages restore --from <path>`
105+
// remains available to retry just the failed packages.
99106
sentinelArmed := false
100107
restoreSucceeded := false
101108
defer func() {
@@ -306,13 +313,63 @@ func runUpgrade(cmd *cobra.Command, args []string) error {
306313
// sentinel already records this snapshot path for
307314
// replay-after-interrupt (`nodeup packages restore --from
308315
// <sentinel path>`); here we read the same path directly.
316+
//
317+
// installPackages now LOOP-ALL (see issue #103): a single failing
318+
// package no longer aborts the rest of the migration. We collect
319+
// the per-package results, persist a MigrationReport to
320+
// <DataDir>/reports/ on both success and partial failure, and
321+
// surface the report path so the user knows what to re-install
322+
// by hand if anything failed. Partial failure keeps the sentinel
323+
// armed (restoreSucceeded stays false) so the user can re-run
324+
// `nodeup packages restore --from <snapshot>` to retry just the
325+
// failures.
309326
if !skipMigrate && len(toInstall) > 0 {
310327
if restoreSnapshotPath == "" {
311328
cmd.Printf("Warning: no snapshot path available to restore from; skipping package migration\n")
312-
} else if err := packages.RestoreFromSnapshot(cmd.Context(), restoreSnapshotPath); err != nil {
313-
cmd.Printf("Warning: restore failed: %v\n", err)
314329
} else {
315-
restoreSucceeded = true
330+
outcome, rerr := packages.RestoreFromSnapshot(cmd.Context(), restoreSnapshotPath)
331+
332+
// Write a MigrationReport whenever we actually attempted at
333+
// least one package. Skipping the write on empty results
334+
// avoids producing a useless "0 packages attempted" file
335+
// for a fresh install with no globals. On a context
336+
// cancellation, results may be truncated; we still record
337+
// whatever we managed to install, with Interrupted=true so
338+
// the report is self-describing.
339+
if len(outcome.Results) > 0 {
340+
// Prefer the snapshot's recorded manager / from-version
341+
// if present (the upgrade flow wrote the snapshot with
342+
// these fields populated, so this is more accurate than
343+
// the CLI's inferred values).
344+
reportMgr := outcome.Snapshot.Manager
345+
if reportMgr == "" {
346+
reportMgr = m.Name()
347+
}
348+
reportFrom := outcome.Snapshot.NodeVersion
349+
if reportFrom == "" {
350+
reportFrom = oldVersion
351+
}
352+
report := packages.NewMigrationReport(reportMgr, reportFrom, toInstall[len(toInstall)-1].String())
353+
report.Interrupted = errors.Is(rerr, context.Canceled) || errors.Is(rerr, context.DeadlineExceeded)
354+
for _, res := range outcome.Results {
355+
report.AddResult(res)
356+
}
357+
if saveErr := report.Save(); saveErr != nil {
358+
cmd.Printf("Warning: failed to write migration report: %v\n", saveErr)
359+
} else if reportPath, perr := report.Path(); perr == nil {
360+
if rerr != nil {
361+
cmd.Printf("Migration report: %s (partial — see file for failures)\n", reportPath)
362+
} else {
363+
cmd.Printf("Migration report: %s\n", reportPath)
364+
}
365+
}
366+
}
367+
368+
if rerr != nil {
369+
cmd.Printf("Warning: restore failed: %v\n", rerr)
370+
} else {
371+
restoreSucceeded = true
372+
}
316373
}
317374
}
318375

‎internal/packages/report.go‎

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package packages
22

33
import (
4+
"crypto/rand"
5+
"encoding/hex"
46
"encoding/json"
57
"fmt"
68
"os"
@@ -27,9 +29,17 @@ type MigrationReport struct {
2729
ToVersion string `json:"to_version"`
2830
Interrupted bool `json:"interrupted"`
2931
Results []PackageResult `json:"results"`
32+
33+
// tag is the on-disk-filename discriminator; assigned lazily on
34+
// the first Path()/Save() call. Not serialized — it's purely a
35+
// filename concern, not part of the report's content.
36+
tag string
3037
}
3138

32-
// NewMigrationReport creates a fresh report for a migration.
39+
// NewMigrationReport creates a fresh report for a migration. The
40+
// returned report has a unique 4-byte hex tag in its Timestamp-derived
41+
// filename (see Path/Save) so two upgrades running within the same
42+
// wall-clock second can't accidentally clobber each other's report.
3343
func NewMigrationReport(manager, fromVersion, toVersion string) *MigrationReport {
3444
return &MigrationReport{
3545
Timestamp: time.Now(),
@@ -39,24 +49,80 @@ func NewMigrationReport(manager, fromVersion, toVersion string) *MigrationReport
3949
}
4050
}
4151

42-
// Save writes the report to ~/.nodeup/reports/<timestamp>.json.
52+
// Save writes the report to <DataDir>/reports/migration-<ts>-<tag>.json.
53+
// The first call computes a stable 4-byte hex tag and caches it on the
54+
// report; subsequent calls (Path and Save) reuse the same tag, so the
55+
// CLI can call Path() before Save() to print the path and have it
56+
// match the file that Save() actually writes.
57+
//
58+
// The tag uses crypto/rand entropy, so two reports produced within the
59+
// same wall-clock second get distinct filenames. 32 bits of entropy is
60+
// plenty to make accidental collisions vanishingly unlikely across the
61+
// lifetime of a single machine's DataDir (~1 report per upgrade,
62+
// upgrades are minutes-to-hours apart for a real user).
63+
//
64+
// Save is NOT safe to call concurrently on the same report instance:
65+
// the tag is assigned once and shared across Path/Save, and two
66+
// parallel Save calls would race to write the same file. Callers
67+
// don't actually need concurrent saves — a single upgrade writes at
68+
// most one report.
4369
func (r *MigrationReport) Save() error {
44-
dir, err := platform.ReportsDir()
70+
path, err := r.Path()
4571
if err != nil {
4672
return err
4773
}
48-
49-
filename := fmt.Sprintf("migration-%s.json", r.Timestamp.Format("20060102-150405"))
50-
path := filepath.Join(dir, filename)
51-
5274
data, err := json.MarshalIndent(r, "", " ")
5375
if err != nil {
5476
return fmt.Errorf("marshal report: %w", err)
5577
}
56-
5778
return os.WriteFile(path, data, 0o644)
5879
}
5980

81+
// Path returns the on-disk path Save() would write to. The first call
82+
// assigns a stable random suffix; subsequent calls return the same
83+
// filename. Exposed so the CLI can print the path to the user without
84+
// re-deriving the timestamp-formatting rule.
85+
//
86+
// Two different MigrationReport instances (i.e., two upgrades) get
87+
// independent random suffixes — Path() called on each one resolves to
88+
// a unique filename even within the same second.
89+
func (r *MigrationReport) Path() (string, error) {
90+
if r.tag == "" {
91+
tag, err := reportTag()
92+
if err != nil {
93+
return "", fmt.Errorf("generate report tag: %w", err)
94+
}
95+
r.tag = tag
96+
}
97+
dir, err := platform.ReportsDir()
98+
if err != nil {
99+
return "", err
100+
}
101+
return filepath.Join(dir, r.filename()), nil
102+
}
103+
104+
// filename is the basename portion of the on-disk path. Centralized
105+
// so Path() and any future tests stay in lockstep with the format.
106+
func (r *MigrationReport) filename() string {
107+
return fmt.Sprintf("migration-%s-%s.json", r.Timestamp.Format("20060102-150405"), r.tag)
108+
}
109+
110+
// reportTag returns 4 hex bytes (8 chars) of crypto-random entropy
111+
// sourced from crypto/rand. 32 bits of entropy is plenty to make
112+
// accidental collisions vanishingly unlikely across the lifetime of a
113+
// single machine's DataDir (~1 report per upgrade, upgrades are
114+
// minutes-to-hours apart for a real user). We deliberately do NOT use
115+
// math/rand here — that would couple report-naming to Go's seeded PRNG
116+
// and could produce identical tags in two different processes
117+
// initialized identically.
118+
func reportTag() (string, error) {
119+
var b [4]byte
120+
if _, err := rand.Read(b[:]); err != nil {
121+
return "", err
122+
}
123+
return hex.EncodeToString(b[:]), nil
124+
}
125+
60126
// AddResult appends a package result to the report.
61127
func (r *MigrationReport) AddResult(pr PackageResult) {
62128
r.Results = append(r.Results, pr)

‎internal/packages/sentinel_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func TestRestoreFromSnapshot_ValidFile(t *testing.T) {
380380
// With an empty Packages list, RestoreFromSnapshot runs the
381381
// install loop zero times and returns nil — no need for a fake
382382
// `npm install` binary on PATH.
383-
if err := RestoreFromSnapshot(t.Context(), snapPath); err != nil {
383+
if _, err := RestoreFromSnapshot(t.Context(), snapPath); err != nil {
384384
t.Errorf("RestoreFromSnapshot: %v", err)
385385
}
386386
}
@@ -389,7 +389,7 @@ func TestRestoreFromSnapshot_ValidFile(t *testing.T) {
389389
// includes both the path they typed AND a "read snapshot" hint so they
390390
// don't have to read source to figure out what went wrong.
391391
func TestRestoreFromSnapshot_MissingFile(t *testing.T) {
392-
err := RestoreFromSnapshot(t.Context(), "/nonexistent/snapshot.json")
392+
_, err := RestoreFromSnapshot(t.Context(), "/nonexistent/snapshot.json")
393393
if err == nil {
394394
t.Fatal("expected error for missing snapshot file")
395395
}
@@ -409,7 +409,7 @@ func TestRestoreFromSnapshot_CorruptFile(t *testing.T) {
409409
if err := os.WriteFile(snapPath, []byte("{not valid json"), 0o644); err != nil {
410410
t.Fatalf("write: %v", err)
411411
}
412-
err := RestoreFromSnapshot(t.Context(), snapPath)
412+
_, err := RestoreFromSnapshot(t.Context(), snapPath)
413413
if err == nil {
414414
t.Fatal("expected error for corrupt JSON")
415415
}

0 commit comments

Comments
 (0)