H-A-202: the narrowed tree keeps the repository's packages a required package requires, transitively

A package artifact's go.mod may require a package of its repository that
requires another of it at the zero version; the narrowing kept only the
first level, so the build of the second found no module of that path. The
entry's paths keep the manifest's form; the Go build module reads each kept
package's go.mod in turn, each directory once, and keeps what it requires.

Builds: design 47 H H-A-202, H-A-198
This commit is contained in:
2026-11-20 22:28:28 +01:00
parent 3fd2e58072
commit b889075e21
2 changed files with 74 additions and 15 deletions
@@ -2,6 +2,7 @@ package main
import (
"io/fs"
"maps"
"os"
"path"
"path/filepath"
@@ -127,11 +128,20 @@ func generatedFromDirs(tree, dir string) []string {
}
// ownZeroPackageDirs are the directories (from the tree's root) of the packages of the checked repository that a go.mod
// under the artifact's named paths requires at the zero version (H-A-136; ADR 0573 §2). Each is found by the manifests
// of the repository's modules: a module's directory or the directory of one of its artifacts' paths whose go.mod declares
// the required module path. One no manifest leads to is left to the fetch's resolution, which fails naming it.
// under the artifact's named paths requires at the zero version (H-A-136; ADR 0573 §2), transitively: the go.mod of each
// such directory is read in turn for the packages of the repository it requires at the zero version, each directory
// once (design 47 H, H-A-202, H-A-198). Each is found by the manifests of the repository's modules: a module's directory
// or the directory of one of its artifacts' paths whose go.mod declares the required module path. One no manifest leads
// to is left to the fetch's resolution, which fails naming it.
func ownZeroPackageDirs(tree, repository, dir string, paths []string) []string {
want := map[string]bool{}
addReqs := func(raw []byte) {
for _, pn := range forgePins(string(raw)) {
if pn.short == zeroShort && "novox/"+pn.repo == repository {
want[pn.module] = true
}
}
}
for _, p := range paths {
root := filepath.Join(tree, filepath.FromSlash(path.Join(dir, p)))
filepath.WalkDir(root, func(file string, e fs.DirEntry, err error) error {
@@ -147,14 +157,8 @@ func ownZeroPackageDirs(tree, repository, dir string, paths []string) []string {
if e.Name() != "go.mod" {
return nil
}
raw, err := os.ReadFile(file)
if err != nil {
return nil
}
for _, pn := range forgePins(string(raw)) {
if pn.short == zeroShort && "novox/"+pn.repo == repository {
want[pn.module] = true
}
if raw, err := os.ReadFile(file); err == nil {
addReqs(raw)
}
return nil
})
@@ -162,10 +166,29 @@ func ownZeroPackageDirs(tree, repository, dir string, paths []string) []string {
if len(want) == 0 {
return nil
}
var out []string
byModule := map[string][]string{} // a module path and the manifest-named directories whose go.mod declares it
for _, cand := range manifestDirs(tree) {
if mod := goModulePath(filepath.Join(tree, filepath.FromSlash(cand), "go.mod")); want[mod] && !slices.Contains(out, cand) {
out = append(out, cand)
if mod := goModulePath(filepath.Join(tree, filepath.FromSlash(cand), "go.mod")); mod != "" && !slices.Contains(byModule[mod], cand) {
byModule[mod] = append(byModule[mod], cand)
}
}
var out []string
done := map[string]bool{}
for len(want) > len(done) {
for _, mod := range slices.Sorted(maps.Keys(want)) {
if done[mod] {
continue
}
done[mod] = true
for _, cand := range byModule[mod] {
if slices.Contains(out, cand) {
continue
}
out = append(out, cand)
if raw, err := os.ReadFile(filepath.Join(tree, filepath.FromSlash(cand), "go.mod")); err == nil {
addReqs(raw) // what this package requires of the repository comes along (H-A-202)
}
}
}
}
slices.Sort(out)
@@ -29,6 +29,7 @@ func writeFile(t *testing.T, file, body string) {
// modules stay the artifact's own (A-447a): neither the generated-from directory's nor the sibling package's.
func TestANarrowedWorkspaceHoldsTheGeneratedFromDirectoriesAndItsRepositorysZeroVersionPackages(t *testing.T) {
const sdk = "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk"
const repo = "git.novox.be/novox/mesh-delivery"
zero := " v0.0.0-00010101000000-" + zeroShort
for _, tc := range []struct {
name, dir string
@@ -37,6 +38,7 @@ func TestANarrowedWorkspaceHoldsTheGeneratedFromDirectoriesAndItsRepositorysZero
kept, gone []string
modules []string
seats []string // generated into the tree by the seat generator, its out-of-step check then finding nothing
refused string // the narrowing fails naming this
}{
{name: "a generated-from directory", dir: "modules/c", paths: []string{"go/seats/a"},
files: map[string]string{
@@ -92,6 +94,33 @@ func TestANarrowedWorkspaceHoldsTheGeneratedFromDirectoriesAndItsRepositorysZero
"modules/mesh-delivery-sdk/go.mod", "modules/mesh-delivery-sdk/steprun/s.go"},
gone: []string{"module.json", "go.sum", "modules/mesh-delivery-view", "modules/mesh-delivery-contracts/go/clients/other"},
modules: []string{"modules/mesh-delivery-contracts/go/clients/meshcatalogclient"}},
// H-A-202, H-A-198: the packages of the repository a required one requires at the zero version come along too,
// transitively (state -> builds -> client), each found by its manifest.
{name: "the repository's packages a required package requires, transitively", dir: "modules/a", paths: []string{"state"},
files: map[string]string{
"modules/a/module.json": `{"module":"a","build":{"artifacts":[{"name":"state","kind":"package","language":"go","paths":["state"]}]}}`,
"modules/a/state/go.mod": "module " + repo + "/modules/a/state\n\ngo 1.26\n\nrequire " + repo + "/modules/b/builds" + zero + "\n",
"modules/a/state/s.go": "package state\n",
"modules/b/module.json": `{"module":"b","build":{"artifacts":[{"name":"builds","kind":"package","language":"go","paths":["builds"]}]}}`,
"modules/b/builds/go.mod": "module " + repo + "/modules/b/builds\n\ngo 1.26\n\nrequire (\n\t" + repo + "/modules/c/client" + zero + "\n\tgit.novox.be/novox/mesh-sdk/go" + zero + "\n)\n",
"modules/b/builds/b.go": "package builds\n",
"modules/b/other/o.go": "package other\n",
"modules/c/module.json": `{"module":"c","build":{"artifacts":[{"name":"client","kind":"package","language":"go","paths":["client"]}]}}`,
"modules/c/client/go.mod": "module " + repo + "/modules/c/client\n\ngo 1.26\n\nrequire " + repo + "/modules/b/builds" + zero + "\n",
"modules/c/client/c.go": "package client\n",
"modules/c/other/o.go": "package other\n",
"modules/d/module.json": `{"module":"d"}`,
},
kept: []string{"modules/a/state/s.go", "modules/b/builds/b.go", "modules/c/client/c.go"},
gone: []string{"modules/b/other", "modules/c/other", "modules/d"},
modules: []string{"modules/a/state"}},
// A named path that leaves the repository is refused.
{name: "a named path that leaves the repository", dir: "mesh-grants", paths: []string{".", "../../outside"},
files: map[string]string{"mesh-grants/module.json": `{"module":"mesh-grants"}`, "mesh-grants/go.mod": "module mesh-grants\n\ngo 1.26\n"},
refused: `"../../outside" leaves`},
{name: "a named path that leaves the repository from a nested module", dir: "modules/mesh-grants", paths: []string{"../../../outside"},
files: map[string]string{"modules/mesh-grants/module.json": `{"module":"mesh-grants"}`, "modules/mesh-grants/go.mod": "module mesh-grants\n\ngo 1.26\n"},
refused: `"../../../outside" leaves`},
} {
t.Run(tc.name, func(t *testing.T) {
tree := t.TempDir()
@@ -125,7 +154,14 @@ func TestANarrowedWorkspaceHoldsTheGeneratedFromDirectoriesAndItsRepositorysZero
}
}
req := sdkstep.FetchRequest{Repository: "novox/mesh-delivery", Commit: commitSHA, Dir: tc.dir, Paths: tc.paths}
if err := narrowTree(tree, req.Repository, req); err != nil {
err := narrowTree(tree, req.Repository, req)
if tc.refused != "" {
if err == nil || !strings.Contains(err.Error(), tc.refused) {
t.Fatalf("the narrowing answered %v, want it refused naming %s", err, tc.refused)
}
return
}
if err != nil {
t.Fatal(err)
}
for _, k := range tc.kept {