fix(34): WR-02 undo a failed plugin:install or plugin:update
A failure after the first change left the application half-modified: a checkout moved into plugins/<vendor>/<name> but not registered, go.mod edited, submodules without summer.yaml entries (and the next install refused the leftover directory), or some submodules updated and others not. Both commands now snapshot go.mod, go.sum, go.work, go.work.sum, summer.yaml and .gitmodules (with its index entry) before applying; a failure restores them, removes the submodules the run added (index entry, submodule config, absorbed git dir, checkout; registered right after the move) and checks moved submodules out where they were. What cannot be undone is named in the error.
This commit is contained in:
@@ -70,6 +70,8 @@ type InstallPlan struct {
|
||||
// whatever is left of them is removed at the end.
|
||||
temps []string
|
||||
interactive bool
|
||||
// snap is what ApplyInstall restores when it fails.
|
||||
snap *appSnapshot
|
||||
}
|
||||
|
||||
// InstallStep is one plugin to add.
|
||||
@@ -667,7 +669,10 @@ func displayVersion(v string) string {
|
||||
// overrides the workspace needs. Module steps go through one go get, so
|
||||
// minimal version selection sees the whole set. The summer.yaml entries
|
||||
// are appended last, in plan order. It does not run go mod tidy: until
|
||||
// summer build imports the new packages, tidy would drop them.
|
||||
// summer build imports the new packages, tidy would drop them. When a
|
||||
// step fails, the files it edits (go.mod, go.sum, go.work, summer.yaml,
|
||||
// .gitmodules) are restored and the submodules this run added are
|
||||
// removed again; what cannot be undone is named in the error.
|
||||
func ApplyInstall(ctx context.Context, plan *InstallPlan, out io.Writer) error {
|
||||
if plan == nil || len(plan.Steps) == 0 {
|
||||
return fmt.Errorf("build: nothing to install")
|
||||
@@ -696,6 +701,13 @@ func ApplyInstall(ctx context.Context, plan *InstallPlan, out io.Writer) error {
|
||||
submodules = true
|
||||
}
|
||||
}
|
||||
snap, err := snapshotApp(ctx, plan.AppDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
plan.snap = snap
|
||||
// A failure from here on puts the application back (WR-02).
|
||||
fail := func(err error) error { return snap.undo(ctx, out, err) }
|
||||
if submodules {
|
||||
if err := requireGitRepo(ctx, plan.AppDir); err != nil {
|
||||
return err
|
||||
@@ -707,22 +719,22 @@ func ApplyInstall(ctx context.Context, plan *InstallPlan, out io.Writer) error {
|
||||
continue
|
||||
}
|
||||
if err := applySubmodule(ctx, plan, s, plan.interactive, out); err != nil {
|
||||
return err
|
||||
return fail(err)
|
||||
}
|
||||
done[s.Module] = true
|
||||
}
|
||||
if err := refreshWorkOverrides(ctx, plan.AppDir, out); err != nil {
|
||||
return err
|
||||
return fail(err)
|
||||
}
|
||||
}
|
||||
if len(getArgs) > 0 {
|
||||
fmt.Fprintf(out, "go get %s\n", strings.Join(getArgs, " "))
|
||||
if err := runGo(ctx, plan.AppDir, append([]string{"get", "--"}, getArgs...)...); err != nil {
|
||||
return err
|
||||
return fail(err)
|
||||
}
|
||||
}
|
||||
if err := appendManifestPlugins(filepath.Join(plan.AppDir, manifestName), entries); err != nil {
|
||||
return err
|
||||
return fail(err)
|
||||
}
|
||||
ids := make([]string, len(plan.Steps))
|
||||
for i, s := range plan.Steps {
|
||||
|
||||
Reference in New Issue
Block a user