mesh-build-node: narrow a step's workspace as H-A-136 and H-A-137 say; a narrowed-out package is no module of the step #716

Merged
mesh-tools merged 2 commits from fix/47-H1-mesh-contracts-publish into main 2026-11-20 15:12:44 +00:00
6 changed files with 255 additions and 26 deletions
Showing only changes of commit f4cd0e89fd - Show all commits
@@ -44,30 +44,37 @@ type artifact struct {
Paths []string `json:"paths"`
}
// packageDir is the directory of a package artifact relative to its module's directory in dir: the one path it names
// when that is a directory of the module holding a package.json (the contracts folders: ts/seats/<seat>/, each with its
// own lock, ADR 0634 section 8), else the module's directory itself (a package that names no path, or source paths
// beside the module's package.json: today's form).
// narrowedAway says whether the package's one named path is absent from the module's directory dir: in a workspace
// narrowed to another artifact's paths, it is not this build's.
func (a artifact) narrowedAway(dir string) bool {
// namedPath is the one path a package artifact names, cleaned, relative to its module's directory; false when it names
// none, several, or one outside the module.
func (a artifact) namedPath() (string, bool) {
if len(a.Paths) != 1 {
return false
return "", false
}
p := path.Clean(strings.TrimSuffix(a.Paths[0], "/"))
if p == "." || path.IsAbs(p) || p == ".." || strings.HasPrefix(p, "../") {
return "", false
}
return p, true
}
// narrowedAway says whether the package's one named path is absent from the module's directory dir: in a workspace
// narrowed to another artifact's paths, it is not this build's.
func (a artifact) narrowedAway(dir string) bool {
p, ok := a.namedPath()
if !ok {
return false
}
_, err := os.Lstat(filepath.Join(dir, filepath.FromSlash(p)))
return os.IsNotExist(err)
}
// packageDir is the directory of a package artifact relative to its module's directory in dir: the one path it names
// when that is a directory of the module holding a package.json (the contracts folders: ts/seats/<seat>/, each with its
// own lock, ADR 0634 section 8), else the module's directory itself (a package that names no path, or source paths
// beside the module's package.json: today's form).
func (a artifact) packageDir(dir string) string {
if len(a.Paths) != 1 {
return "."
}
p := path.Clean(strings.TrimSuffix(a.Paths[0], "/"))
if p == "." || path.IsAbs(p) || p == ".." || strings.HasPrefix(p, "../") {
p, ok := a.namedPath()
if !ok {
return "."
}
if _, err := os.Stat(filepath.Join(dir, filepath.FromSlash(p), "package.json")); err != nil {
@@ -175,7 +175,7 @@ func (m *Module) fetchInto(ctx context.Context, req sdkstep.FetchRequest, group
}
}
if len(req.Paths) > 0 {
if err := steprun.Narrow(tree, req); err != nil {
if err := narrowTree(tree, req); err != nil { // with the generated-from directories H-A-136 and H-A-137 keep
return nil, err
}
// Said beside the tree, so the listing of the tree's modules leaves out those narrowed away.
@@ -0,0 +1,135 @@
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
}
@@ -0,0 +1,74 @@
package main
import (
"os"
"path/filepath"
"strings"
"testing"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/seatgen"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
)
// A root contracts module (mesh-contracts at 87eab81, pipeline main.3 of 2026-11-20): two seats, each generated as a Go
// and a TypeScript package naming generated-from ".", in step with the seat generator, beside what no step reads.
func rootContracts(t *testing.T) string {
t.Helper()
tree := t.TempDir()
put := func(p, body string) {
write(t, filepath.Join(tree, filepath.FromSlash(p)), body, 0o644)
}
var arts []string
for _, s := range []string{"one-store", "two-store"} {
put("seats/"+s+"/seat.json", `{"format":1,"seat":"`+s+`","scope":"mesh","grain":"whole","domain":"Module","decision":"ADR 1","verbs":[{"name":"query","input":{"type":"object"},"answer":{"type":"object"}}]}`)
pkg := strings.ReplaceAll(s, "-", "")
arts = append(arts,
`{"name":"go-`+pkg+`","kind":"package","language":"go","paths":["go/seats/`+pkg+`"],"generated-from":"."}`,
`{"name":"ts-`+pkg+`","kind":"package","language":"typescript","paths":["ts/seats/`+pkg+`"],"generated-from":"."}`)
}
put("module.json", `{"module":"c","version":"0.1","build":{"artifacts":[`+strings.Join(arts, ",")+`]}}`)
put("README.md", "the repository\n")
g, err := seatgen.Load(tree, "example.org/c/go", "@example/c")
if err != nil {
t.Fatal(err)
}
files, err := seatgen.Generate(g)
if err != nil {
t.Fatal(err)
}
for p, body := range files {
put(p, string(body))
}
return tree
}
// Design 47 H, H-A-136 and H-A-137: a step's workspace of one package of a root module keeps, beside the package's path
// and module.json, the seats/ directory and the paths of every artifact naming generated-from, so the unit level's
// generator check finds nothing out of step; nothing else is kept. Narrowed bare (steprun.Narrow), the check names the
// generated files left out.
func TestANarrowedRootContractsModuleIsInStepWithTheGenerator(t *testing.T) {
req := sdkstep.FetchRequest{Repository: "novox/c", Commit: commitSHA, Dir: ".", Paths: []string{"ts/seats/onestore"}}
bare := rootContracts(t)
if err := steprun.Narrow(bare, req); err != nil {
t.Fatal(err)
}
if out, err := steprun.OutOfStep(bare, steprun.GeneratedRootTS); err != nil || len(out) == 0 {
t.Fatalf("narrowed bare: out of step %v (%v), want the left-out packages named", out, err)
}
tree := rootContracts(t)
if err := narrowTree(tree, req); err != nil {
t.Fatal(err)
}
if out, err := steprun.OutOfStep(tree, steprun.GeneratedRootTS); err != nil || len(out) != 0 {
t.Errorf("narrowed: out of step %v (%v), want none", out, err)
}
for _, kept := range []string{"module.json", "seats/two-store/seat.json", "ts/seats/twostore/package.json", "go/seats/twostore/go.mod"} {
if _, err := os.Stat(filepath.Join(tree, filepath.FromSlash(kept))); err != nil {
t.Errorf("%s is not kept: %v", kept, err)
}
}
if _, err := os.Stat(filepath.Join(tree, "README.md")); err == nil {
t.Error("README.md is still in the workspace")
}
}
@@ -43,9 +43,10 @@ type StepsAnswer struct {
Error string `json:"error,omitempty"`
}
// detectSteps finds the steps of the package directories under dir.
func detectSteps(dir string) ([]DetectedStep, error) {
targets, err := testTargets(dir, ".", false)
// detectSteps finds the steps of the package directories under dir; narrowedTree says the tree was narrowed to an
// artifact's paths, as testTargets reads it.
func detectSteps(dir string, narrowedTree bool) ([]DetectedStep, error) {
targets, err := testTargets(dir, ".", narrowedTree)
if err != nil {
return nil, err
}
@@ -176,7 +177,8 @@ func (m *Module) stepsOfTree(ctx context.Context, req sdkstep.FetchRequest) ([]D
return nil, err
}
if len(req.Paths) > 0 {
if err := steprun.Narrow(tree, req); err != nil {
// Narrowed as the fetch of the step's workspace narrows (narrowTree), so the steps answered are those the levels run.
if err := narrowTree(tree, req); err != nil {
return nil, err
}
}
@@ -184,5 +186,5 @@ func (m *Module) stepsOfTree(ctx context.Context, req sdkstep.FetchRequest) ([]D
if req.Dir != "" && req.Dir != "." {
dir = filepath.Join(tree, filepath.FromSlash(req.Dir))
}
return detectSteps(dir)
return detectSteps(dir, len(req.Paths) > 0)
}
@@ -22,17 +22,28 @@ func TestTheStepsOfATypeScriptArtifactAreFoundByItsScripts(t *testing.T) {
}
}
for name, c := range map[string]struct {
pkg string
want []DetectedStep
pkg string
narrowed bool
want []DetectedStep
}{
"both levels": {`{"scripts":{"test":"node t.js","test:integration":"node i.js"}}`,
"both levels": {`{"scripts":{"test":"node t.js","test:integration":"node i.js"}}`, false,
[]DetectedStep{{Step: "lint"}, {Step: "unit", Packages: []string{"."}}, {Step: "integration", Packages: []string{"."}}}},
"unit only": {`{"scripts":{"test":"node t.js"}}`, []DetectedStep{{Step: "lint"}, {Step: "unit", Packages: []string{"."}}}},
"no script": {`{"scripts":{}}`, []DetectedStep{{Step: "lint"}}},
"unit only": {`{"scripts":{"test":"node t.js"}}`, false, []DetectedStep{{Step: "lint"}, {Step: "unit", Packages: []string{"."}}}},
"no script": {`{"scripts":{}}`, false, []DetectedStep{{Step: "lint"}}},
// A folder module's tree narrowed to ts/seats/a (H-A-136): the steps are those the narrowed levels run there.
"narrowed folder": {"", true, []DetectedStep{{Step: "lint"}, {Step: "unit", Packages: []string{"ts/seats/a"}}}},
} {
dir := t.TempDir()
put(dir, "package.json", c.pkg)
got, err := detectSteps(dir)
if c.narrowed {
for p, body := range folderFiles("") {
if !strings.HasPrefix(p, "ts/seats/b/") {
put(filepath.Join(dir, filepath.Dir(p)), filepath.Base(p), body)
}
}
} else {
put(dir, "package.json", c.pkg)
}
got, err := detectSteps(dir, c.narrowed)
if err != nil || !reflect.DeepEqual(got, c.want) {
t.Errorf("%s: steps = %+v (%v), want %+v", name, got, err, c.want)
}