Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/importset.go
T
jschoubben e5a250d774 I6: a take-in moves another repository's importer by its import set, in either language
Review r1 finding 1: a module of another repository was recomputed only when
a pin's Go module path mapped to the moved repository, so a TypeScript
importer of @novox/mesh-sdk never moved on a mesh-sdk take-in. It now moves
when its reported import set holds a package the moved repository publishes
(the module graph's entries), or, with no set, when a pin names one.

Builds: design 47 H2 H-A-520, H-A-523, H-A-528, H-A-539
2026-11-23 13:17:15 +01:00

263 lines
11 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// ImportsState is the indexer's state imports (design 47 H2 H-A-527, H-A-532 DQ-3): one key per module, the module's
// modules key, holding the import set main's last build of the module reported (H-A-517, H-A-519) with each package's
// digest at that build (DQ-4). It is written only when an imports-reported is kept, and its key is deleted with the
// module's modules key; the tool indexer_imports reads it.
const ImportsState = "imports"
// ImportsTool reads one module's import set (H-A-527, H-A-532 DQ-3).
const ImportsTool = "indexer_imports"
// ImportSet is a value of the state imports: the module, the fingerprint and commit of the build that reported the set,
// and each imported mesh package with its digest; a package no published module held when it was kept has none.
type ImportSet struct {
Module string `json:"module"`
Fingerprint string `json:"fingerprint"`
Commit string `json:"commit"`
Imports []ImportedPackage `json:"imports"`
}
// ImportedPackage is one package of an import set, by import path, with its digest.
type ImportedPackage struct {
Package string `json:"package"`
Digest string `json:"digest,omitempty"`
}
// ErrImportsUnreadable says an imports-reported does not fit its shape: the message is said and acknowledged, never
// given again.
var ErrImportsUnreadable = errors.New("not an imports-reported the indexer can keep")
// HearImportsReported keeps the import set a build module reported at a build's end (design 47 H2 H-A-517), when its
// fingerprint is the one main's modules state holds for the module (H-A-532 DMC-2); one of a fingerprint the module no
// longer holds is left (H-A-519). A body naming no module or fingerprint is ErrImportsUnreadable.
func (ix *Indexer) HearImportsReported(ctx context.Context, e nodebuildgo.ImportsReported) error {
if e.Module == "" || e.Fingerprint == "" {
return fmt.Errorf("%w: it names no module or no fingerprint", ErrImportsUnreadable)
}
_, err := ix.keepImports(ctx, reported{module: e.Module, fp: e.Fingerprint, commit: e.Commit, imports: slices.Clone(e.Imports)})
return err
}
// ImportSetOf is the import set the state imports keeps for the module key; found is false when it keeps none.
func (ix *Indexer) ImportSetOf(ctx context.Context, key string) (set ImportSet, found bool, err error) {
if ix.Imports == nil {
return ImportSet{}, false, nil
}
v, _, found, err := ix.Imports.Get(ctx, key)
if err != nil || !found {
return ImportSet{}, false, err
}
if err := json.Unmarshal(v, &set); err != nil {
return ImportSet{}, false, fmt.Errorf("%s.%s is not an import set: %w", ImportsState, key, err)
}
return set, true, nil
}
// importSetHeld says whether the state imports already holds e for key, the same fingerprint and packages; a store that
// keeps none holds it, so nothing is written for it.
func (ix *Indexer) importSetHeld(ctx context.Context, key string, e reported) bool {
if ix.Imports == nil {
return true
}
set, found, err := ix.ImportSetOf(ctx, key)
if err != nil || !found || set.Fingerprint != e.fp {
return false
}
return slices.EqualFunc(set.Imports, e.imports, func(p ImportedPackage, imp string) bool { return p.Package == imp })
}
// writeImportSet writes e with its packages' digests into the state imports at key.
func (ix *Indexer) writeImportSet(ctx context.Context, key string, e reported, digests map[string]string) error {
if ix.Imports == nil {
return nil
}
set := ImportSet{Module: e.module, Fingerprint: e.fp, Commit: e.commit, Imports: []ImportedPackage{}}
for _, imp := range e.imports {
set.Imports = append(set.Imports, ImportedPackage{Package: imp, Digest: digests[imp]})
}
data, err := json.Marshal(set)
if err != nil {
return err
}
if err := write(ctx, ix.Imports, key, func([]byte, bool) ([]byte, bool, error) { return data, false, nil }); err != nil {
return fmt.Errorf("%s.%s: %w", ImportsState, key, err)
}
return nil
}
// dropImportSet deletes the module key's import set, as its modules key is deleted (H-A-532 DQ-3).
func (ix *Indexer) dropImportSet(ctx context.Context, key string) error {
if ix.Imports == nil {
return nil
}
if err := write(ctx, ix.Imports, key, func(_ []byte, found bool) ([]byte, bool, error) { return nil, found, nil }); err != nil {
return fmt.Errorf("%s.%s: %w", ImportsState, key, err)
}
return nil
}
// ImportsReadTool is indexer_imports (H-A-527): one module's import set from the state imports, each package with its
// digest and the fingerprint and commit of the build that reported it, or that none is kept, so it moves by its go.mod's
// requirements (H-A-528).
func ImportsReadTool(st Store) stdio.Tool {
return stdio.Tool{Name: ImportsTool,
Description: "One module's import set: the mesh packages its programs import, as the build of main's fingerprint reported " +
"them, each with its digest at that build, and that build's fingerprint and commit (design 47 H2 H-A-527); a module " +
"with none kept moves by its go.mod's requirements until its first report (H-A-528).",
Input: map[string]any{"type": "object", "required": []string{"module"}, "properties": map[string]any{
"module": map[string]any{"type": "string", "description": "the module, such as avahi"}}},
Run: func(args map[string]any) (any, error) {
module, _ := args["module"].(string)
key, err := meshcatalogclient.ModulesKey(module)
if module == "" || err != nil {
return nil, fmt.Errorf("%s names a module: %q is none", ImportsTool, module)
}
set, found, err := (&Indexer{Imports: st}).ImportSetOf(context.Background(), key)
if err != nil {
return nil, fmt.Errorf("the state %s cannot be read: %w", ImportsState, err)
}
if !found {
return map[string]any{"module": module, "kept": false,
"said": "no import set is kept for " + module + ": it moves by its go.mod's requirements until its first report"}, nil
}
return map[string]any{"module": module, "kept": true, "set": set}, nil
}}
}
// withReportedImports is cur with the import set the state imports keeps for key, the set main's last build reported,
// which every take-in computes with, a watched branch's too (design 47 H2 H-A-519, H-A-532 DMC-6); a value holding
// another set takes it with main's import form while main's value holds that set (so the form costs no rebuild of its
// own, H-A-529), else with none, so its next computation writes the import form. cur is left as it is when the state
// keeps no set for key, or it holds no inputs.
//
// reported says whether the module's import set is reported (H-A-536, H-A-537): the state imports keeps a key for it,
// whatever the set's length, or, until its next report replaces it, its value already holds a set (one kept before the
// state imports was), read as it is.
func (ix *Indexer) withReportedImports(ctx context.Context, key string, cur meshcatalog.ModulesByModuleValue) (_ meshcatalog.ModulesByModuleValue, reported bool) {
if cur.Inputs2 == nil {
return cur, false
}
set, found, err := ix.ImportSetOf(ctx, key)
if err != nil {
ix.logf("%s.%s cannot be read, so %s keeps the import set its value holds: %v", ImportsState, key, key, err)
return cur, len(cur.Inputs2.Imports) > 0
}
if !found {
return cur, len(cur.Inputs2.Imports) > 0
}
imports := make([]string, 0, len(set.Imports))
for _, p := range set.Imports {
imports = append(imports, p.Package)
}
if slices.Equal(cur.Inputs2.Imports, imports) {
return cur, true
}
in := *cur.Inputs2
in.Imports, in.ImportForm = imports, ix.trunkImportForm(ctx, key, imports)
if len(imports) == 0 {
in.Imports = nil
}
cur.Inputs2 = &in
return cur, true
}
// trunkImportForm is the import form main's modules value of key holds with imports, "" when it holds another set.
func (ix *Indexer) trunkImportForm(ctx context.Context, key string, imports []string) string {
st := ix.trunkModules
if st == nil {
st = ix.Modules
}
if st == nil {
return ""
}
v, _, found, err := st.Get(ctx, key)
if err != nil || !found {
return ""
}
m, err := meshcatalogclient.DecodeModule(v)
if err != nil || m.Inputs2 == nil || !slices.Equal(m.Inputs2.Imports, imports) {
return ""
}
return m.Inputs2.ImportForm
}
// meshPackages are the names of the packages the entries of the module graph being computed publish (its held modules,
// as viewEntries gives each its artifacts; the graph's nodes name no package, so a graph state that cannot be listed
// keeps nothing from it, ADR 0603 §2), a Go module path or an npm name each: what a reported import set is held against
// at every recompute (design 47 H2 H-A-539, H-A-540). held nil reads the indexer's modules.
func (ix *Indexer) meshPackages(ctx context.Context, held map[string]meshcatalog.ModulesByModuleValue) (map[string]bool, error) {
by, err := ix.packagesPublishedBy(ctx, held)
if err != nil {
return nil, err
}
names := map[string]bool{}
for name := range by {
names[name] = true
}
return names, nil
}
// packagesPublishedBy are the packages meshPackages names, each with the repository of the module that publishes it, so a
// take-in tells the packages its repository publishes (design 47 H2 H-A-520, H-A-539).
func (ix *Indexer) packagesPublishedBy(ctx context.Context, held map[string]meshcatalog.ModulesByModuleValue) (map[string]string, error) {
if held == nil {
var err error
if held, err = ix.heldModules(ctx); err != nil {
return nil, err
}
}
names := map[string]string{}
for name, cur := range held {
if cur.Manifest == "" {
continue
}
raw, m, err := ix.manifestRaw(ctx, cur.Manifest)
if err != nil {
continue // a manifest that cannot be read publishes nothing this pass; the recompute says it of the module
}
for _, a := range ix.withArtifacts(ctx, CatalogEntryOf(name, cur, m, raw, graph.Node{}), m).Artifacts {
if p := a.Package; p != nil && p.Name != "" && (p.Ecosystem == EcosystemGo || p.Ecosystem == EcosystemNpm) {
names[p.Name] = cur.Repository
}
}
}
return names, nil
}
// inTheMesh are the imports a package of names holds, the package itself or a path under it (I9's reading); any other
// import is outside the mesh, and never sends the module to the version line (design 47 H2 H-A-539, H-A-540). The state
// imports keeps the set whole; only what the fingerprint takes is held against names.
func inTheMesh(imports []string, names map[string]bool) []string {
var out []string
for _, imp := range imports {
for p := imp; p != ""; {
if names[p] {
out = append(out, imp)
break
}
i := strings.LastIndex(p, "/")
if i < 0 {
break
}
p = p[:i]
}
}
return out
}