The Go build module's step workspace holds the generated-from directories and the same-repository zero-version packages (design 47 H, H-A-136, H-A-137) #710

Merged
mesh-tools merged 2 commits from fix/47-H1-zero-version-tree into main 2026-11-20 12:56:52 +00:00
6 changed files with 509 additions and 39 deletions
@@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"slices"
@@ -157,7 +158,7 @@ func (m *Module) fetchInto(ctx context.Context, req sdkstep.FetchRequest, id str
return nil, err
}
if (base == "" || res.Merge != "") && len(req.Paths) > 0 {
if err := steprun.Narrow(tree, req); err != nil {
if err := narrowTree(tree, repository, req); err != nil { // with what H-A-136 adds
return nil, err
}
// Said beside the tree, so the steps that list the tree's go.mod files leave out those narrowed away.
@@ -173,7 +174,7 @@ func (m *Module) fetchInto(ctx context.Context, req sdkstep.FetchRequest, id str
// A fetch asked with a module's directory is of that Go module and what its go.mod replaces with a local path,
// never another Go module of the tree (design 47 G, A-447). Said beside the tree, so every later step lists the
// same modules.
if dirs, err = scopedDirs(tree, dir, dirs); err != nil {
if dirs, err = scopedDirs(tree, dir, req.Paths, dirs); err != nil {
return nil, err
}
if err := os.WriteFile(filepath.Join(root, scopeMark), []byte(strings.Join(dirs, "\n")), 0o600); err != nil {
@@ -687,7 +688,7 @@ func cleanDir(dir string) string {
// go.mod replaces with a local path, followed through their own replaces (design 47 G, A-447), in order. A directory
// the tree holds no go.mod for is left out. A directory holding no go.mod chooses every go.mod under it, and one that
// chooses no Go module at all is an error naming it (A-447a).
func scopedDirs(tree, dir string, all []string) ([]string, error) {
func scopedDirs(tree, dir string, paths, all []string) ([]string, error) {
have := map[string]bool{}
for _, d := range all {
have[d] = true
@@ -696,11 +697,23 @@ func scopedDirs(tree, dir string, all []string) ([]string, error) {
todo := []string{dir}
if !have[dir] {
// A directory with no go.mod of its own (a module whose Go code lies in go/, a contracts module with a go.mod per
// seat) is every go.mod under it; the tree is already narrowed to the module's paths (A-447a).
// seat) is every go.mod under it, in a narrowed tree under its named paths (A-447a): the narrowed tree also holds
// the module's generated-from directories and its repository's zero-version packages (H-A-136), which are not
// the artifact's own Go modules.
roots := []string{dir}
if len(paths) > 0 {
roots = nil
for _, p := range paths {
roots = append(roots, path.Join(dir, filepath.ToSlash(p)))
}
}
todo = nil
for _, d := range all {
if dir == "." || strings.HasPrefix(d, dir+"/") {
todo = append(todo, d)
for _, r := range roots {
if r == "." || d == r || strings.HasPrefix(d, r+"/") {
todo = append(todo, d)
break
}
}
}
}
@@ -69,7 +69,7 @@ func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *test
t.Fatal(err)
}
}
got, err := scopedDirs(tree, cleanDir(c.dir), all)
got, err := scopedDirs(tree, cleanDir(c.dir), nil, all)
if c.want == nil {
if err == nil || !strings.Contains(err.Error(), c.dir) {
t.Errorf("dir %q chose %v, %v; want an error naming it", c.dir, got, err)
@@ -0,0 +1,228 @@
package main
import (
"io/fs"
"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 (narrowTree) keeps them at the root too,
// where the asked module's build 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 build 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, and the directory (with its go.mod) of every package of the same repository
// the artifact's go.mod requires at the zero version, found by the manifests of that repository's modules; nothing else
// (design 47 H, lead answer H-A-136). A package of another repository comes published, as before (ADR 0573 §4).
func narrowTree(tree, repository 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
}
extra := generatedFromDirs(tree, dir)
extra = append(extra, ownZeroPackageDirs(tree, repository, dir, req.Paths)...)
var outside []string
inside := slices.Clone(req.Paths)
for _, e := range extra {
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
}
// 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): the seat definitions a generated package is compared against. 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, as
// the generator's check reads every generated package (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
}
// 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.
func ownZeroPackageDirs(tree, repository, dir string, paths []string) []string {
want := map[string]bool{}
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 {
if err != nil {
return nil
}
if e.IsDir() {
if n := e.Name(); n == ".git" || n == "vendor" || n == "node_modules" || n == "testdata" {
return filepath.SkipDir
}
return nil
}
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
}
}
return nil
})
}
if len(want) == 0 {
return nil
}
var out []string
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)
}
}
slices.Sort(out)
return out
}
// manifestDirs are the directories the manifests of the tree name, from its root: each module's directory and the
// directory of each of its artifacts' named paths.
func manifestDirs(tree string) []string {
var out []string
filepath.WalkDir(tree, func(file string, e fs.DirEntry, err error) error {
if err != nil {
return nil
}
if e.IsDir() {
if n := e.Name(); n == ".git" || n == "vendor" || n == "node_modules" || n == "testdata" {
return filepath.SkipDir
}
return nil
}
if e.Name() != "module.json" {
return nil
}
rel, err := filepath.Rel(tree, filepath.Dir(file))
if err != nil {
return nil
}
mdir := filepath.ToSlash(rel)
out = append(out, mdir)
raw, err := os.ReadFile(file)
if err != nil {
return nil
}
man, err := catalogue.Decode(raw)
if err != nil || man.Build == nil {
return nil
}
for _, a := range man.Build.Artifacts {
for _, p := range a.Paths {
out = append(out, path.Join(mdir, filepath.ToSlash(p)))
}
}
return nil
})
return out
}
// goModulePath is the module path the go.mod file declares, "" when there is none.
func goModulePath(file string) string {
raw, err := os.ReadFile(file)
if err != nil {
return ""
}
for _, line := range strings.Split(string(raw), "\n") {
if f := strings.Fields(line); len(f) == 2 && f[0] == "module" {
return strings.Trim(f[1], `"`)
}
}
return ""
}
@@ -45,6 +45,28 @@ func TestAFetchWithNamedPathsNarrowsTheWorkspace(t *testing.T) {
if strings.Join(res.Modules, ",") != "lib" {
t.Errorf("the fetch answered the modules %v, want lib alone", res.Modules)
}
// A tree shaped like mesh-delivery's: the narrowed artifact requires another module of its repository at the zero
// version: the narrowed workspace keeps its directory, found by its manifest, and the fetch resolves it there (design
// 47 H, H-A-136; live 2026-11-20, pipeline main.3).
const sdk = "git.novox.be/novox/narrowown/modules/sdk"
_, ownHead := f.repo("novox/narrowown", map[string]string{
"modules/sdk/go.mod": "module " + sdk + "\n\ngo 1.25.0\n",
"modules/sdk/module.json": `{"module":"sdk","version":"1"}`,
"modules/sdk/s.go": "package sdk\n\nconst S = 1\n",
"modules/c/module.json": `{"module":"c","version":"1"}`,
"modules/c/go/x/go.mod": "module git.novox.be/novox/narrowown/modules/c/go/x\n\ngo 1.25.0\n\nrequire " + sdk + " v0.0.0-00010101000000-000000000000\n",
"modules/c/go/x/x.go": "package x\n\nimport \"" + sdk + "\"\n\nconst X = sdk.S\n",
})
res, st = fetched(t, m, states, map[string]any{"repository": "novox/narrowown", "commit": ownHead, "dir": "modules/c", "paths": []any{"go/x"}})
if st.State != sdkstep.StepPassed {
t.Fatalf("the fetch of an artifact requiring its repository's other module ended %s: %s", st.State, st.Reason)
}
if _, err := os.Stat(filepath.Join(m.Work, res.Workspace, "tree", "modules", "sdk", "s.go")); err != nil {
t.Errorf("the narrowed workspace lacks the required package of its repository: %v", err)
}
if strings.Join(res.Modules, ",") != "modules/c/go/x" {
t.Errorf("the fetch answered the modules %v, want modules/c/go/x alone", res.Modules)
}
_, st = fetched(t, m, states, map[string]any{"repository": "novox/narrow", "commit": head, "dir": "", "paths": []any{"cmd"}})
if st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, "no module directory") {
t.Errorf("paths without a directory: %s %q", st.State, st.Reason)
@@ -0,0 +1,161 @@
package main
import (
"os"
"path/filepath"
"slices"
"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"
)
func writeFile(t *testing.T, file, body string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(file, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
// Design 47 H, lead answer H-A-136 (live 2026-11-20, the first pipeline main.3): a workspace narrowed to an artifact's
// named paths also holds every generated-from directory an artifact of its module names (the seat definitions the
// generated package is compared against) and the directory, with its go.mod, of every package of the same repository the
// artifact's go.mod requires at the zero version, found by that repository's manifests; nothing else. The fetch's Go
// 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"
zero := " v0.0.0-00010101000000-" + zeroShort
for _, tc := range []struct {
name, dir string
paths []string
files map[string]string
kept, gone []string
modules []string
seats []string // generated into the tree by the seat generator, its out-of-step check then finding nothing
}{
{name: "a generated-from directory", dir: "modules/c", paths: []string{"go/seats/a"},
files: map[string]string{
"modules/c/module.json": `{"module":"c","build":{"artifacts":[{"name":"go-a","kind":"package","language":"go","paths":["go/seats/a"],"generated-from":"seats"},{"name":"go-b","kind":"package","language":"go","paths":["go/seats/b"],"generated-from":"seats"}]}}`,
"modules/c/seats/a.json": "{}\n",
"modules/c/go/seats/a/go.mod": "module git.novox.be/novox/mesh-delivery/modules/c/go/seats/a\n\ngo 1.26\n",
"modules/c/go/seats/b/go.mod": "module git.novox.be/novox/mesh-delivery/modules/c/go/seats/b\n\ngo 1.26\n",
"modules/c/docs/x.md": "not named\n",
"modules/d/module.json": `{"module":"d"}`,
},
kept: []string{"modules/c/module.json", "modules/c/seats/a.json", "modules/c/go/seats/a/go.mod"},
gone: []string{"modules/c/go/seats/b", "modules/c/docs", "modules/d"},
modules: []string{"modules/c/go/seats/a"}},
{name: "the module's own directory as generated-from", dir: "modules/c", paths: []string{"go/seats/a"},
files: map[string]string{
"modules/c/module.json": `{"module":"c","build":{"artifacts":[{"name":"go-a","kind":"package","language":"go","paths":["go/seats/a"],"generated-from":"."}]}}`,
"modules/c/seats/a.json": "{}\n",
"modules/c/go/seats/a/go.mod": "module git.novox.be/novox/mesh-delivery/modules/c/go/seats/a\n\ngo 1.26\n",
"modules/c/go/seats/b/go.mod": "module git.novox.be/novox/mesh-delivery/modules/c/go/seats/b\n\ngo 1.26\n",
"modules/d/module.json": `{"module":"d"}`,
},
kept: []string{"modules/c/seats/a.json", "modules/c/go/seats/b/go.mod"},
gone: []string{"modules/d"},
modules: []string{"modules/c/go/seats/a"}},
{name: "a module at the repository's root with generated-from \".\"", dir: ".", paths: []string{"go/seats/onestore"},
files: map[string]string{
"module.json": `{"module":"mesh-contracts","build":{"artifacts":[{"name":"go-onestore","kind":"package","language":"go","paths":["go/seats/onestore"],"generated-from":"."},{"name":"go-twostore","kind":"package","language":"go","paths":["go/seats/twostore"],"generated-from":"."},{"name":"ts-onestore","kind":"package","language":"typescript","paths":["ts/seats/onestore"],"generated-from":"."},{"name":"ts-twostore","kind":"package","language":"typescript","paths":["ts/seats/twostore"],"generated-from":"."},{"name":"go-other","kind":"package","language":"go","paths":["go/other"]}]}}`,
"README.md": "the repository\n",
"docs/a.md": "not named\n",
"go/other/go.mod": "module git.novox.be/novox/mesh-contracts/go/other\n\ngo 1.26\n",
"modules/x/module.json": `{"module":"x"}`,
},
seats: []string{"one-store", "two-store"},
kept: []string{"module.json", "seats/one-store/seat.json", "seats/two-store/seat.json", "go/seats/onestore/go.mod", "go/seats/twostore/contract.go", "ts/seats/twostore/package.json"},
gone: []string{"README.md", "docs", "go/other", "modules"},
modules: []string{"go/seats/onestore"}},
{name: "a package of the same repository at the zero version", dir: "modules/mesh-delivery-contracts",
paths: []string{"go/clients/meshcatalogclient"},
files: map[string]string{
"module.json": `{"module":"root"}`,
"go.sum": "root\n",
"modules/mesh-delivery-contracts/module.json": `{"module":"mesh-delivery-contracts","build":{"artifacts":[{"name":"go-meshcatalogclient","kind":"package","language":"go","paths":["go/clients/meshcatalogclient"]}]}}`,
"modules/mesh-delivery-contracts/go/clients/meshcatalogclient/go.mod": "module git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient\n\ngo 1.26\n\nrequire (\n\t" + sdk + zero + "\n\tgit.novox.be/novox/mesh-sdk/go" + zero + "\n)\n",
"modules/mesh-delivery-contracts/go/clients/meshcatalogclient/c.go": "package meshcatalogclient\n",
"modules/mesh-delivery-contracts/go/clients/other/go.mod": "module git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/other\n\ngo 1.26\n",
"modules/mesh-delivery-sdk/module.json": `{"module":"mesh-delivery-sdk","build":{"artifacts":[{"name":"go-sdk","kind":"package","language":"go"}]}}`,
"modules/mesh-delivery-sdk/go.mod": "module " + sdk + "\n\ngo 1.26\n",
"modules/mesh-delivery-sdk/steprun/s.go": "package steprun\n",
"modules/mesh-delivery-view/module.json": `{"module":"mesh-delivery-view"}`,
"modules/mesh-delivery-view/go.mod": "module git.novox.be/novox/mesh-delivery/modules/mesh-delivery-view\n\ngo 1.26\n",
},
kept: []string{"modules/mesh-delivery-contracts/module.json", "modules/mesh-delivery-contracts/go/clients/meshcatalogclient/c.go",
"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"}},
} {
t.Run(tc.name, func(t *testing.T) {
tree := t.TempDir()
var all []string
for f, body := range tc.files {
os.MkdirAll(filepath.Join(tree, filepath.Dir(f)), 0o755)
if err := os.WriteFile(filepath.Join(tree, f), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
if filepath.Base(f) == "go.mod" {
all = append(all, filepath.Dir(f))
}
}
if len(tc.seats) > 0 {
for _, seat := range tc.seats {
writeFile(t, filepath.Join(tree, "seats", seat, "seat.json"), `{"format":1,"seat":"`+seat+`","scope":"mesh","grain":"whole","domain":"Module","decision":"ADR 1","verbs":[{"name":"query","input":{"type":"object"},"answer":{"type":"object"}}]}`)
}
g, err := seatgen.Load(tree, "git.novox.be/novox/mesh-contracts/go", "@novox/mesh-contracts")
if err != nil {
t.Fatal(err)
}
files, err := seatgen.Generate(g)
if err != nil {
t.Fatal(err)
}
for f, b := range files {
writeFile(t, filepath.Join(tree, f), string(b))
if filepath.Base(f) == "go.mod" {
all = append(all, filepath.Dir(f))
}
}
}
req := sdkstep.FetchRequest{Repository: "novox/mesh-delivery", Commit: commitSHA, Dir: tc.dir, Paths: tc.paths}
if err := narrowTree(tree, req.Repository, req); err != nil {
t.Fatal(err)
}
for _, k := range tc.kept {
if _, err := os.Stat(filepath.Join(tree, k)); err != nil {
t.Errorf("%s was narrowed away: %v", k, err)
}
}
for _, g := range tc.gone {
if _, err := os.Stat(filepath.Join(tree, g)); err == nil {
t.Errorf("%s is still in the workspace", g)
}
}
if len(tc.seats) > 0 {
for _, root := range []string{steprun.GeneratedRootGo, steprun.GeneratedRootTS} {
if out, err := steprun.OutOfStep(filepath.Join(tree, tc.dir), root); err != nil || len(out) != 0 {
t.Errorf("the seat generator's check in the narrowed workspace, root %s: %v %v, want nothing out of step", root, out, err)
}
}
}
var left []string
for _, d := range all {
if _, err := os.Stat(filepath.Join(tree, d, "go.mod")); err == nil {
left = append(left, d)
}
}
slices.Sort(left)
got, err := scopedDirs(tree, tc.dir, tc.paths, left)
if err != nil || strings.Join(got, ",") != strings.Join(tc.modules, ",") {
t.Errorf("the fetch's Go modules = %v (%v), want %v", got, err, tc.modules)
}
})
}
}
@@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
@@ -400,39 +401,84 @@ func TestARequirementAtTheZeroVersionIsTakenFromTheAskOrTheTrunk(t *testing.T) {
// A requirement at the zero version of a module of the checked repository itself (a nested module not yet published, which
// another module of the repository requires) is built from the fetched tree, and never asked of Go or the forge (issue 1921).
// Narrowed: a fetch of one artifact of a tree shaped like mesh-delivery's (modules/mesh-delivery-contracts/go/clients/<c>
// requiring modules/mesh-delivery-sdk at the zero version), asked with the module's directory and the artifact's paths,
// keeps the sdk's directory, found by its manifest, and resolves it there (design 47 H, H-A-136; live 2026-11-20,
// pipeline main.3: "no module of that path is in the fetched tree of novox/mesh-delivery").
func TestARequirementAtTheZeroVersionOfTheCheckedRepositorysOwnModuleIsTakenFromItsTree(t *testing.T) {
const own = "git.novox.be/novox/mesh-delivery/nested/go"
zeroMod := "module fixture/member\n\ngo 1.26\n\nrequire (\n\t" + own + " v0.0.0-00010101000000-" + zeroShort + "\n)\n"
h, states := newPinHarness(t, zeroMod, false, pullList)
base := h.m.Run
h.m.Run = func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
line := name + " " + strings.Join(args, " ")
if name == "go" && strings.Contains(line, " mod download") && !strings.Contains(strings.Join(env, " "), "-modfile=") {
trees, _ := filepath.Glob(filepath.Join(h.m.Work, "*", "tree"))
for _, tree := range trees {
os.MkdirAll(filepath.Join(tree, "nested", "go"), 0o755)
os.WriteFile(filepath.Join(tree, "nested", "go", "go.mod"), []byte("module "+own+"\n\ngo 1.26\n"), 0o644)
for _, tc := range []struct {
name, own, ownDir, gomods string
args map[string]any
narrowed bool
}{
{name: "whole tree", own: "git.novox.be/novox/mesh-delivery/nested/go", ownDir: "nested/go", gomods: "go.mod\n",
args: map[string]any{"repository": "novox/mesh-delivery", "commit": commitSHA}},
{name: "narrowed to an artifact's paths", own: "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk",
ownDir: "modules/mesh-delivery-sdk", gomods: "modules/mesh-delivery-contracts/go/clients/meshcatalogclient/go.mod\n",
args: map[string]any{"repository": "novox/mesh-delivery", "commit": commitSHA, "dir": "modules/mesh-delivery-contracts",
"paths": []any{"go/clients/meshcatalogclient"}}, narrowed: true},
} {
t.Run(tc.name, func(t *testing.T) {
zeroMod := "module fixture/member\n\ngo 1.26\n\nrequire (\n\t" + tc.own + " v0.0.0-00010101000000-" + zeroShort + "\n)\n"
h, states := newPinHarness(t, zeroMod, false, pullList)
h.f.gomods = tc.gomods
base := h.m.Run
h.m.Run = func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
line := name + " " + strings.Join(args, " ")
if name == "git" && strings.Contains(line, " checkout ") && tc.narrowed {
r := base(ctx, env, name, args...)
tree := args[slices.Index(args, "-C")+1]
for f, body := range map[string]string{
"modules/mesh-delivery-sdk/go.mod": "module " + tc.own + "\n\ngo 1.26\n",
"modules/mesh-delivery-sdk/module.json": `{"module":"mesh-delivery-sdk","build":{"artifacts":[{"name":"go-sdk","kind":"package","language":"go"}]}}`,
"modules/mesh-delivery-contracts/module.json": `{"module":"mesh-delivery-contracts","build":{"artifacts":[{"name":"go-meshcatalogclient","kind":"package","language":"go","paths":["go/clients/meshcatalogclient"]}]}}`,
"modules/mesh-delivery-view/go.mod": "module git.novox.be/novox/mesh-delivery/modules/mesh-delivery-view\n\ngo 1.26\n",
} {
os.MkdirAll(filepath.Join(tree, filepath.Dir(f)), 0o755)
os.WriteFile(filepath.Join(tree, f), []byte(body), 0o644)
}
return r
}
if name == "go" && strings.Contains(line, " mod download") && !strings.Contains(strings.Join(env, " "), "-modfile=") {
if !tc.narrowed {
trees, _ := filepath.Glob(filepath.Join(h.m.Work, "*", "tree"))
for _, tree := range trees {
os.MkdirAll(filepath.Join(tree, tc.ownDir), 0o755)
os.WriteFile(filepath.Join(tree, tc.ownDir, "go.mod"), []byte("module "+tc.own+"\n\ngo 1.26\n"), 0o644)
}
}
return forgekey.Ran{Status: 1, Stderr: "go: " + tc.own + "@v0.0.0-00010101000000-" + zeroShort + ": invalid version: unknown revision " + zeroShort + "\n"}
}
return base(ctx, env, name, args...)
}
return forgekey.Ran{Status: 1, Stderr: "go: " + own + "@v0.0.0-00010101000000-" + zeroShort + ": invalid version: unknown revision " + zeroShort + "\n"}
}
return base(ctx, env, name, args...)
}
if _, err := fetchTool(t, h.m)(map[string]any{"repository": "novox/mesh-delivery", "commit": commitSHA}); err != nil {
t.Fatal(err)
}
st := states.waitEnd(t)
if st.State != sdkstep.StepPassed {
t.Fatalf("the step ended %s: %s", st.State, st.Reason)
}
modfiles, _ := filepath.Glob(filepath.Join(h.m.Work, "*", "pins", "*", "go.mod"))
if len(modfiles) != 1 {
t.Fatalf("modfiles = %v", modfiles)
}
body, _ := os.ReadFile(modfiles[0])
if !strings.Contains(string(body), "replace "+own+" => "+filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(modfiles[0]))), "tree", "nested", "go")) {
t.Errorf("the module of the tree was not replaced into the modfile:\n%s", body)
}
if h.ran(" mod download -json ") || h.ran("ls-remote") {
t.Error("a module of the checked repository was asked of Go or looked for among the pull request heads")
if _, err := fetchTool(t, h.m)(tc.args); err != nil {
t.Fatal(err)
}
st := states.waitEnd(t)
if st.State != sdkstep.StepPassed {
t.Fatalf("the step ended %s: %s", st.State, st.Reason)
}
modfiles, _ := filepath.Glob(filepath.Join(h.m.Work, "*", "pins", "*", "go.mod"))
if len(modfiles) != 1 {
t.Fatalf("modfiles = %v", modfiles)
}
root := filepath.Dir(filepath.Dir(filepath.Dir(modfiles[0])))
where := filepath.Join(root, "tree", filepath.FromSlash(tc.ownDir))
if tc.narrowed {
if _, err := os.Stat(filepath.Join(root, "tree", "modules", "mesh-delivery-view")); err == nil {
t.Error("the narrowed tree holds a module the artifact does not require")
}
}
body, _ := os.ReadFile(modfiles[0])
if !strings.Contains(string(body), "replace "+tc.own+" => "+where+"\n") {
t.Errorf("the module of the repository was not replaced into the modfile by %s:\n%s", where, body)
}
if strings.Count(string(body), "replace ") != 1 {
t.Errorf("the modfile replaces more than the one other module (its own directory again?):\n%s", body)
}
if h.ran(" mod download -json ") || h.ran("ls-remote") {
t.Error("a module of the checked repository was asked of Go or looked for among the pull request heads")
}
})
}
}