feat(build): plugin:install --source and git URLs as submodules

- --source clones each registry repository (https only) at the chosen tag,
  adds it as a submodule under plugins/<vendor>/<name>, absorbs its git
  dir and registers it with the tag as the go.mod require
- git URLs (https, ssh, user@host:path) are cloned with the user's git
  setup and never sent to the registry; the newest tag whose plugin.yaml
  framework minimum fits is checked out, else the default branch head
- go.work replace overrides for every local replace in workspace modules'
  go.mod files (app target, versioned workspace module, or version pin)
- GIT_ALLOW_PROTOCOL=https:ssh, '--' before URLs and paths; non-git apps
  are refused before cloning; plugin:add keeps require v0.0.0
This commit is contained in:
Jakub Zych
2026-10-29 18:39:22 +01:00
parent a462bf12c9
commit 8093587696
8 changed files with 1191 additions and 29 deletions

View File

@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"sort"
@@ -64,6 +65,11 @@ type InstallPlan struct {
Framework string
// Steps are in dependency order: a plugin's requirements come first.
Steps []InstallStep
// temps are checkouts under plugins/ made while planning or applying;
// whatever is left of them is removed at the end.
temps []string
interactive bool
}
// InstallStep is one plugin to add.
@@ -74,6 +80,9 @@ type InstallStep struct {
Package string
Repository string
Mode string
// clone is the checkout a git URL step was planned from.
clone string
}
// installer carries the state of one PlanInstall run.
@@ -93,6 +102,9 @@ type installer struct {
planned map[string]InstallStep
visiting []string
moduleVersion map[string]string
// source installs registry plugins as submodules: --source, or the
// requirements of a git URL install.
source bool
}
// PlanInstall resolves what plugin:install would add: the requested
@@ -129,27 +141,57 @@ func PlanInstall(ctx context.Context, opts InstallOptions) (*InstallPlan, error)
in.planned = map[string]InstallStep{}
in.moduleVersion = map[string]string{}
in.plan.interactive = opts.Interactive
in.source = opts.Source
ok := false
defer func() {
if !ok {
in.plan.removeTemps()
}
}()
id, pin, err := parseInstallArg(opts.Arg)
if err != nil {
return nil, err
}
if err := in.requireAbsent(id); err != nil {
return nil, err
}
if err := in.requireRegistry(id); err != nil {
return nil, err
}
if err := in.resolve(id, pin, ""); err != nil {
return nil, err
arg := strings.TrimSpace(opts.Arg)
if isGitURL(arg) {
in.source = true
if err := in.planGitURL(arg); err != nil {
return nil, err
}
} else {
id, pin, err := parseInstallArg(arg)
if err != nil {
return nil, err
}
if err := in.requireAbsent(id); err != nil {
return nil, err
}
if err := in.requireRegistry(id); err != nil {
return nil, err
}
if in.source {
if err := requireGitRepo(ctx, appDir); err != nil {
return nil, err
}
}
if err := in.resolve(id, pin, ""); err != nil {
return nil, err
}
}
if err := in.confirm(); err != nil {
return nil, err
}
ok = true
return in.plan, nil
}
// removeTemps deletes the leftover temporary checkouts of the plan.
func (p *InstallPlan) removeTemps() {
for _, dir := range p.temps {
_ = os.RemoveAll(dir)
}
p.temps = nil
}
// parseInstallArg splits vendor.name[@vX.Y.Z].
func parseInstallArg(arg string) (id, pin string, err error) {
arg = strings.TrimSpace(arg)
@@ -222,17 +264,26 @@ func (in *installer) resolve(id, pin, min string) error {
}
in.moduleVersion[step.Module] = step.Version
in.visiting = append(in.visiting, id)
if err := in.resolveRequires(id, step.Version, requires); err != nil {
return err
}
in.visiting = in.visiting[:len(in.visiting)-1]
in.planned[id] = step
in.plan.Steps = append(in.plan.Steps, step)
return nil
}
// resolveRequires resolves the requirements of id (at version), in ID
// order.
func (in *installer) resolveRequires(id, version string, requires kiosk.Requires) error {
for _, dep := range sortedKeys(requires) {
if err := ValidatePluginID(dep); err != nil {
return fmt.Errorf("build: registry entry %s %s requires %w; refused", id, step.Version, err)
return fmt.Errorf("build: %s %s requires %w; refused", id, displayRef(version), err)
}
if err := in.resolve(dep, "", requires[dep]); err != nil {
return err
}
}
in.visiting = in.visiting[:len(in.visiting)-1]
in.planned[id] = step
in.plan.Steps = append(in.plan.Steps, step)
return nil
}
@@ -416,6 +467,19 @@ func (in *installer) choose(id, pin, min string) (InstallStep, kiosk.Requires, e
Package: chosen.Package,
Mode: ModeModule,
}
if in.source {
detail, err := in.opts.Registry.Plugin(in.ctx, id)
if err != nil {
return InstallStep{}, nil, fmt.Errorf("build: %w", err)
}
if err := validateRepositoryURL(id, detail.Repository); err != nil {
return InstallStep{}, nil, err
}
step.Mode, step.Repository = ModeSubmodule, detail.Repository
if dir := filepath.Join(in.appDir, "plugins", pluginVendor(id), pluginName(id)); dirExists(dir) {
return InstallStep{}, nil, fmt.Errorf("build: %s already exists; remove it or register it with plugin:add", dir)
}
}
if err := validateStep(step); err != nil {
return InstallStep{}, nil, err
}
@@ -451,7 +515,10 @@ func newestOf(versions []kiosk.RegistryVersion) kiosk.RegistryVersion {
// the version must be a strict vX.Y.Z and the module and package must be
// plain module paths, the package inside the module.
func validateStep(s InstallStep) error {
if !kiosk.ValidVersion(s.Version) {
// A git URL install without a fitting release tag takes the default
// branch head, which has no version.
headOfClone := s.Version == "" && s.clone != ""
if !kiosk.ValidVersion(s.Version) && !headOfClone {
return fmt.Errorf("build: registry entry %s: version %q is not a vX.Y.Z release; refused", s.ID, s.Version)
}
if err := validateModulePath(s.Module); err != nil {
@@ -463,6 +530,15 @@ func validateStep(s InstallStep) error {
if s.Package != s.Module && !strings.HasPrefix(s.Package, s.Module+"/") {
return fmt.Errorf("build: registry entry %s: package %s is not inside module %s; refused", s.ID, s.Package, s.Module)
}
switch s.Mode {
case ModeModule:
case ModeSubmodule:
if err := validateGitURL(s.Repository); err != nil {
return fmt.Errorf("build: %s: %w; refused", s.ID, err)
}
default:
return fmt.Errorf("build: %s: unknown install mode %q", s.ID, s.Mode)
}
return nil
}
@@ -470,7 +546,7 @@ func validateStep(s InstallStep) error {
func (p *InstallPlan) summary() string {
var b strings.Builder
for _, s := range p.Steps {
fmt.Fprintf(&b, " %s@%s (%s)\n", s.ID, s.Version, s.Mode)
fmt.Fprintf(&b, " %s@%s (%s)\n", s.ID, displayRef(s.Version), s.Mode)
}
return b.String()
}
@@ -508,6 +584,11 @@ func askToProceed(yes, interactive bool, confirm func(string) (bool, error), que
return nil
}
func dirExists(dir string) bool {
_, err := os.Stat(dir)
return err == nil
}
func displayVersion(v string) string {
if v == "" {
return "unknown"
@@ -515,28 +596,58 @@ func displayVersion(v string) string {
return v
}
// ApplyInstall carries out a plan from PlanInstall: one go get for every
// module step, so minimal version selection sees the whole set, then the
// summer.yaml entries in plan order. It does not run go mod tidy: until
// ApplyInstall carries out a plan from PlanInstall. Submodule steps are
// cloned at their tag, added as git submodules under
// plugins/<vendor>/<name> and registered in go.mod (require at the tag
// plus a local replace) and go.work, followed by the go.work replace
// 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.
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")
}
defer plan.removeTemps()
if out == nil {
out = io.Discard
}
var getArgs []string
var entries []Plugin
submodules := false
for _, s := range plan.Steps {
if err := validateStep(s); err != nil {
return err
}
if s.Mode == ModeModule && !slices.Contains(getArgs, s.Module+"@"+s.Version) {
getArgs = append(getArgs, s.Module+"@"+s.Version)
switch s.Mode {
case ModeModule:
if !slices.Contains(getArgs, s.Module+"@"+s.Version) {
getArgs = append(getArgs, s.Module+"@"+s.Version)
}
case ModeSubmodule:
submodules = true
}
entries = append(entries, Plugin{ID: s.ID, Module: s.Package})
}
if submodules {
if err := requireGitRepo(ctx, plan.AppDir); err != nil {
return err
}
done := map[string]bool{}
for i := range plan.Steps {
s := &plan.Steps[i]
if s.Mode != ModeSubmodule || done[s.Module] {
continue
}
if err := applySubmodule(ctx, plan, s, plan.interactive, out); err != nil {
return err
}
done[s.Module] = true
}
if err := refreshWorkOverrides(ctx, plan.AppDir, out); err != nil {
return 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 {
@@ -548,7 +659,7 @@ func ApplyInstall(ctx context.Context, plan *InstallPlan, out io.Writer) error {
}
ids := make([]string, len(plan.Steps))
for i, s := range plan.Steps {
ids[i] = s.ID + "@" + s.Version
ids[i] = s.ID + "@" + displayRef(s.Version)
}
fmt.Fprintf(out, "Installed %s; run summer build\n", strings.Join(ids, ", "))
return nil