Files
mesh-catalog/modules/mesh-build-node/cmd/mesh-build-node/narrow.go
T
jschoubben f4cd0e89fd mesh-build-node-narrowed-packages: narrow a step's workspace as H-A-136 and H-A-137 say
Review round 1. The node build module runs the seat generator's check at its unit level
(steprun.OutOfStep), so its narrowed workspace keeps what H-A-136 and H-A-137 name: every
generated-from directory of the module, and for a root module the seats/ directory and the
paths of every artifact naming generated-from. Narrowed bare, a step of one mesh-contracts
package named the generated files left out as out of step. The fetch and the steps verb now
narrow the same way, and the steps verb reads its narrowed tree as the levels do.

narrowTree and generatedFromDirs are copied from mesh-build-go without its Go part, not moved
into mesh-delivery-sdk's steprun: the move is a change to two more repositories and a group
delivery for some sixty lines, in a fix that unblocks the build steps now; the copy says so.
packageDir and narrowedAway share one path helper.

Builds: design 47 H H-A-136, H-A-137 (hq design/47-H1-answers-59), ADR 0499 clause 4
2026-11-20 16:10:08 +01:00

136 lines
4.8 KiB
Go

package main
// A copy of mesh-build-go's narrowTree and generatedFromDirs (mesh-build-go/cmd/mesh-build-go/narrow.go) without its Go
// part (the directories of the repository's packages a go.mod requires at the zero version). Copied, not moved to
// mesh-delivery-sdk's steprun beside Narrow: the move is a change to two more repositories (mesh-delivery and back
// into mesh-build-go) and a group delivery for some sixty lines, in a fix that unblocks the build modules' steps now;
// the copy is said here so the move is not forgotten.
import (
"os"
"path"
"path/filepath"
"slices"
"strings"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// narrowedKeptAtRoot are what steprun.Narrow keeps beside the named paths, read from the request's directory: the
// manifest and its lock files. A request narrowed from the repository's root keeps them at the root too, where the
// asked module names none of them; they are removed again.
var narrowedKeptAtRoot = []string{"module.json", "go.sum", "package-lock.json"}
// narrowTree narrows the fetched tree for a step of an artifact that names its paths (ADR 0499 clause 4,
// steprun.Narrow). Beside the artifact's paths and the module's manifest, the workspace holds every generated-from
// directory an artifact of the module names, so the unit level's generator check (steprun.OutOfStep) finds the seat
// definitions and every generated package; nothing else (design 47 H, lead answers H-A-136 and H-A-137).
func narrowTree(tree string, req sdkstep.FetchRequest) error {
dir := cleanDir(req.Dir)
if dir == "" {
return steprun.Narrow(tree, req) // refused there: named paths without the module's directory
}
var outside []string
inside := slices.Clone(req.Paths)
for _, e := range generatedFromDirs(tree, dir) {
if rel, ok := under(dir, e); ok {
if !slices.Contains(inside, rel) {
inside = append(inside, rel)
}
} else if !slices.Contains(outside, e) {
outside = append(outside, e)
}
}
if len(outside) == 0 {
r := req
r.Paths = inside
return steprun.Narrow(tree, r)
}
// A directory outside the module's own: the request is narrowed from the root, with every path the module's
// narrowing keeps named by its path from the root (the manifest and lock files only where the module has them).
all := []string{}
for _, p := range inside {
all = append(all, path.Join(dir, p))
}
for _, f := range narrowedKeptAtRoot {
if _, err := os.Lstat(filepath.Join(tree, filepath.FromSlash(path.Join(dir, f)))); err == nil {
all = append(all, path.Join(dir, f))
}
}
all = append(all, outside...)
if err := steprun.Narrow(tree, sdkstep.FetchRequest{Repository: req.Repository, Commit: req.Commit, Paths: all}); err != nil {
return err
}
for _, f := range narrowedKeptAtRoot {
if !slices.Contains(all, f) {
if err := os.RemoveAll(filepath.Join(tree, f)); err != nil {
return err
}
}
}
return nil
}
func cleanDir(dir string) string {
if strings.TrimSpace(dir) == "" {
return ""
}
return strings.Trim(filepath.ToSlash(filepath.Clean(dir)), "/")
}
// under answers p's path relative to dir when p is dir or lies under it.
func under(dir, p string) (string, bool) {
if dir == "." {
return p, true
}
if p == dir {
return ".", true
}
if rel, ok := strings.CutPrefix(p, dir+"/"); ok {
return rel, true
}
return "", false
}
// generatedFromDirs are the generated-from directories (from the tree's root) the artifacts of the module in dir name
// (H-A-136). A module at the repository's root keeps not its generated-from directory, which would be the whole
// repository, but the generator's inputs and outputs: the seats/ directory of each generated-from directory and the
// paths of every artifact of the module naming generated-from (H-A-137). A manifest that is absent or unreadable names
// none; the steps that read it say so.
func generatedFromDirs(tree, dir string) []string {
raw, err := os.ReadFile(filepath.Join(tree, filepath.FromSlash(dir), "module.json"))
if err != nil {
return nil
}
man, err := catalogue.Decode(raw)
if err != nil || man.Build == nil {
return nil
}
var out []string
add := func(p string) {
if p == ".." || strings.HasPrefix(p, "../") || slices.Contains(out, p) {
return
}
if _, err := os.Lstat(filepath.Join(tree, filepath.FromSlash(p))); err == nil { // steprun.Narrow refuses a path the tree lacks
out = append(out, p)
}
}
for _, a := range man.Build.Artifacts {
g := strings.TrimSpace(a.GeneratedFrom)
if g == "" {
continue
}
if dir != "." {
add(path.Join(dir, filepath.ToSlash(g)))
continue
}
add(path.Join(filepath.ToSlash(g), "seats"))
for _, p := range a.Paths {
add(path.Clean(filepath.ToSlash(p)))
}
}
return out
}