From 1168376930251c30bb6dda287c80ec73a319e0ed Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 16 Nov 2026 11:09:26 +0100 Subject: [PATCH 1/6] Go build module: a fetch asked with dir chooses that module and its local replaces alone (A-447) Builds: design 47 G (G-22 lead answers 2) A-447 --- .../mesh-build-go/cmd/mesh-build-go/fetch.go | 72 +++++++++++++++++++ .../cmd/mesh-build-go/gomoddirs_test.go | 53 ++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go index b4d5ab854..ffffd85ed 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go @@ -20,6 +20,8 @@ import ( "sync" "time" + "golang.org/x/mod/modfile" + sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" dforgekey "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/forgekey" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" @@ -165,6 +167,17 @@ func (m *Module) fetchInto(ctx context.Context, req sdkstep.FetchRequest, id str if err != nil { return nil, err } + if dir := cleanDir(req.Dir); dir != "" { + // 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 { + return nil, err + } + if err := os.WriteFile(filepath.Join(root, scopeMark), []byte(strings.Join(dirs, "\n")), 0o600); err != nil { + return nil, err + } + } res.Modules = dirs if base != "" && res.Merge == "" { return res, nil // a conflict: the tree is at the trunk, nothing of the head is built or tested @@ -434,7 +447,11 @@ func (m *Module) downloadAndVerify(ctx context.Context, dir, d string) error { } // goModDirs are the directories of the tree that hold a go.mod, relative to it, in order. +// When the fetch was asked with a module's directory (scopeMark), the steps after it see only the directories it chose. func (m *Module) goModDirs(ctx context.Context, tree string) ([]string, error) { + if b, err := os.ReadFile(filepath.Join(filepath.Dir(tree), scopeMark)); err == nil { + return strings.Fields(string(b)), nil + } r := m.Run(ctx, nil, "git", "-C", tree, "ls-files", "--", "go.mod", "*/go.mod") if r.Status != 0 || r.Err != "" { return nil, fmt.Errorf("git ls-files: %s", dforgekey.Said(r.Err+"\n"+r.Stderr)) @@ -608,3 +625,58 @@ func narrowed(tree string) bool { _, err := os.Stat(filepath.Join(filepath.Dir(tree), narrowedMark)) return err == nil } + +// scopeMark is the file beside a workspace's tree naming the Go module directories a fetch asked with a module's +// directory chose, one to a line (empty: none). +const scopeMark = "gomodules" + +// cleanDir is a request's module directory in the form goModDirs lists, "" for none. +func cleanDir(dir string) string { + if strings.TrimSpace(dir) == "" { + return "" + } + return strings.Trim(filepath.ToSlash(filepath.Clean(dir)), "/") +} + +// scopedDirs are the go.mod directories of all that a module directory dir chooses: dir itself and each directory its +// 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. +func scopedDirs(tree, dir string, all []string) ([]string, error) { + have := map[string]bool{} + for _, d := range all { + have[d] = true + } + chosen := map[string]bool{} + todo := []string{dir} + for len(todo) > 0 { + d := todo[0] + todo = todo[1:] + if chosen[d] || !have[d] { + continue + } + chosen[d] = true + file := filepath.Join(tree, filepath.FromSlash(d), "go.mod") + body, err := os.ReadFile(file) + if err != nil { + return nil, err + } + f, err := modfile.Parse(file, body, nil) // not ParseLax: that one ignores the replaces + if err != nil { + return nil, fmt.Errorf("%s: %w", file, err) + } + for _, r := range f.Replace { + if !modfile.IsDirectoryPath(r.New.Path) { + continue + } + target := filepath.ToSlash(filepath.Clean(filepath.Join(filepath.FromSlash(d), filepath.FromSlash(r.New.Path)))) + todo = append(todo, target) + } + } + out := []string{} + for _, d := range all { + if chosen[d] { + out = append(out, d) + } + } + return out, nil +} diff --git a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go index dfd9bb678..e5d2d2352 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go @@ -2,6 +2,8 @@ package main import ( "context" + "os" + "path/filepath" "reflect" "testing" @@ -25,3 +27,54 @@ func TestGoModDirsLeaveOutTheFixturesUnderTestdata(t *testing.T) { t.Fatalf("go.mod directories = %v, want %v", got, want) } } + +// A-447: a fetch asked with a module's directory chooses that Go module and the directories its go.mod replaces with a +// local path, through their own replaces; the later steps list the same. The takein module of the indexer is no module +// of the assigner's build (live, 2026-11-16). +func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *testing.T) { + all := []string{"modules/assigner", "modules/indexer", "modules/indexer/takein", "modules/lib", "modules/lib/deep", "modules/other"} + mods := map[string]string{ + "modules/assigner": "module a\nreplace x => ../lib\nreplace (\n\ty => ../../outside\n\tz v1.0.0 => z v1.1.0\n)\n", + "modules/indexer": "module i\nreplace t => ./takein\n", + "modules/indexer/takein": "module t\n", + "modules/lib": "module l\nreplace d => ./deep\n", + "modules/lib/deep": "module d\n", + "modules/other": "module o\n", + } + for _, c := range []struct { + dir string + want []string + }{ + {"modules/assigner", []string{"modules/assigner", "modules/lib", "modules/lib/deep"}}, + {"modules/indexer", []string{"modules/indexer", "modules/indexer/takein"}}, + {"modules/other/", []string{"modules/other"}}, + {"modules/none", []string{}}, + } { + tree := t.TempDir() + for d, body := range mods { + if err := os.MkdirAll(filepath.Join(tree, d), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(tree, d, "go.mod"), []byte(body), 0o600); err != nil { + t.Fatal(err) + } + } + got, err := scopedDirs(tree, cleanDir(c.dir), all) + if err != nil || !reflect.DeepEqual(got, c.want) { + t.Errorf("dir %q chooses %v, %v; want %v", c.dir, got, err, c.want) + } + } + // What the steps after the fetch list is what the fetch chose, beside the tree. + root := t.TempDir() + if err := os.WriteFile(filepath.Join(root, scopeMark), []byte("modules/assigner\nmodules/lib"), 0o600); err != nil { + t.Fatal(err) + } + m := &Module{} + got, err := m.goModDirs(context.Background(), filepath.Join(root, "tree")) + if err != nil || !reflect.DeepEqual(got, []string{"modules/assigner", "modules/lib"}) { + t.Errorf("the later steps list %v, %v", got, err) + } + if cleanDir("") != "" || cleanDir(".") != "." { + t.Errorf("cleanDir of nothing = %q, of . = %q", cleanDir(""), cleanDir(".")) + } +} From ccab80d7262a7c96f6b492c1dd85178f2a7f83eb Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 16 Nov 2026 11:16:16 +0100 Subject: [PATCH 2/6] Go fetch with dir: a directory with no go.mod chooses every go.mod under it; none chosen fails naming it (A-447a) Builds: design 47 G (G-22 lead answers 2) A-447a --- .../mesh-build-go/cmd/mesh-build-go/fetch.go | 16 +++++++++++++++- .../cmd/mesh-build-go/gomoddirs_test.go | 19 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go index ffffd85ed..6b0725251 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go @@ -640,7 +640,8 @@ func cleanDir(dir string) string { // scopedDirs are the go.mod directories of all that a module directory dir chooses: dir itself and each directory its // 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. +// 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) { have := map[string]bool{} for _, d := range all { @@ -648,6 +649,16 @@ func scopedDirs(tree, dir string, all []string) ([]string, error) { } chosen := map[string]bool{} 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). + todo = nil + for _, d := range all { + if dir == "." || strings.HasPrefix(d, dir+"/") { + todo = append(todo, d) + } + } + } for len(todo) > 0 { d := todo[0] todo = todo[1:] @@ -678,5 +689,8 @@ func scopedDirs(tree, dir string, all []string) ([]string, error) { out = append(out, d) } } + if len(out) == 0 { + return nil, fmt.Errorf("the module directory %q holds no Go module (no go.mod in it or under it)", dir) + } return out, nil } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go index e5d2d2352..75782a9fa 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "reflect" + "strings" "testing" "git.novox.be/novox/mesh-sdk/go/forgekey" @@ -32,7 +33,7 @@ func TestGoModDirsLeaveOutTheFixturesUnderTestdata(t *testing.T) { // local path, through their own replaces; the later steps list the same. The takein module of the indexer is no module // of the assigner's build (live, 2026-11-16). func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *testing.T) { - all := []string{"modules/assigner", "modules/indexer", "modules/indexer/takein", "modules/lib", "modules/lib/deep", "modules/other"} + all := []string{".", "modules/assigner", "modules/indexer", "modules/indexer/takein", "modules/lib", "modules/lib/deep", "modules/other", "rootless/x/go", "seats/a", "seats/b"} mods := map[string]string{ "modules/assigner": "module a\nreplace x => ../lib\nreplace (\n\ty => ../../outside\n\tz v1.0.0 => z v1.1.0\n)\n", "modules/indexer": "module i\nreplace t => ./takein\n", @@ -40,6 +41,10 @@ func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *test "modules/lib": "module l\nreplace d => ./deep\n", "modules/lib/deep": "module d\n", "modules/other": "module o\n", + ".": "module r\n", + "seats/a": "module sa\nreplace q => ../q\n", + "seats/b": "module sb\n", + "rootless/x/go": "module rx\n", } for _, c := range []struct { dir string @@ -48,7 +53,11 @@ func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *test {"modules/assigner", []string{"modules/assigner", "modules/lib", "modules/lib/deep"}}, {"modules/indexer", []string{"modules/indexer", "modules/indexer/takein"}}, {"modules/other/", []string{"modules/other"}}, - {"modules/none", []string{}}, + {"modules/lib/deep/", []string{"modules/lib/deep"}}, + {"seats", []string{"seats/a", "seats/b"}}, // a go.mod per seat, none in seats itself + {".", []string{"."}}, // the root holds a go.mod + {"rootless/x", []string{"rootless/x/go"}}, // Go code lies in go/ + {"modules/none", nil}, // chooses no Go module: an error naming it } { tree := t.TempDir() for d, body := range mods { @@ -60,6 +69,12 @@ func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *test } } got, err := scopedDirs(tree, cleanDir(c.dir), 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) + } + continue + } if err != nil || !reflect.DeepEqual(got, c.want) { t.Errorf("dir %q chooses %v, %v; want %v", c.dir, got, err, c.want) } From 666be37916afe4e1bd875ab973be72c974c41ce3 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 16 Nov 2026 11:34:17 +0100 Subject: [PATCH 3/6] Go fetch is refused with no dir, naming its asker; the checking program's fetch names its directory (A-447b) Builds: design 47 G (G-22 lead answers 2) A-447b --- .../mesh-build-go/cmd/mesh-build-go/fetch.go | 15 +++++++++ .../cmd/mesh-build-go/fetch_test.go | 31 +++++++++++++++++-- .../cmd/mesh-build-go/gomoddirs_test.go | 21 +++++++++++++ .../cmd/mesh-build-go/mirror_test.go | 4 +++ .../mesh-build-go/narrow_integration_test.go | 2 +- .../cmd/mesh-build-go/pins_test.go | 10 +++++- .../mesh-build-go/cmd/mesh-build-go/run.go | 2 +- .../cmd/mesh-build-go/verify_once_test.go | 6 ++-- 8 files changed, 83 insertions(+), 8 deletions(-) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go index 6b0725251..2ab8fff54 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go @@ -22,6 +22,8 @@ import ( "golang.org/x/mod/modfile" + stdio "git.novox.be/novox/mesh-sdk/go" + sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" dforgekey "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/forgekey" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" @@ -108,6 +110,11 @@ func (m *Module) fetch(ctx context.Context, args map[string]any) (res any, err e if err != nil { return nil, err } + // A build is for one module, never a repository (design 47 G, A-447b): a fetch names its module's directory. + if cleanDir(req.Dir) == "" { + return nil, fmt.Errorf("the fetch of %s at %s names no module directory (dir): a fetch is for one module, never a whole repository; asked by %s", + req.Repository, req.Commit, askerOf(args)) + } siblings, err := siblingsArg(args["beside"]) if err != nil { return nil, err @@ -694,3 +701,11 @@ func scopedDirs(tree, dir string, all []string) ([]string, error) { } return out, nil } + +// askerOf names who asked a tool: the caller the bus proved, else "an unnamed asker". +func askerOf(args map[string]any) string { + if c := stdio.CallerOf(args); c != "" { + return c + } + return "an unnamed asker" +} diff --git a/modules/mesh-build-go/cmd/mesh-build-go/fetch_test.go b/modules/mesh-build-go/cmd/mesh-build-go/fetch_test.go index 75c8823c9..aeaa3e44f 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch_test.go @@ -61,6 +61,13 @@ func (f *fakeStep) run(ctx context.Context, env []string, name string, args ...s os.MkdirAll(filepath.Dir(filepath.Join(args[i+1], name)), 0o755) os.WriteFile(filepath.Join(args[i+1], name), []byte(text), 0o644) } + // Every go.mod the fake lists exists, since a fetch asked with a directory reads its go.mod (A-447). + for _, l := range strings.Fields(f.gomods) { + if _, err := os.Stat(filepath.Join(args[i+1], l)); err != nil && f.files[l] == "" { + os.MkdirAll(filepath.Dir(filepath.Join(args[i+1], l)), 0o755) + os.WriteFile(filepath.Join(args[i+1], l), []byte("module fake\n"), 0o644) + } + } for _, v := range f.vendors { os.MkdirAll(filepath.Join(args[i+1], v, "vendor"), 0o755) os.WriteFile(filepath.Join(args[i+1], v, "vendor", "modules.txt"), nil, 0o644) @@ -91,7 +98,24 @@ func fetchModule(t *testing.T, f *fakeStep) (*Module, *keptStates) { return m, states } +// fetchTool is the fetch tool as a build module is asked, always with its module's directory (A-447b): the root of the +// tree unless the args name one. rawFetchTool is the tool as it is. func fetchTool(t *testing.T, m *Module) func(map[string]any) (any, error) { + t.Helper() + raw := rawFetchTool(t, m) + return func(args map[string]any) (any, error) { + if _, ok := args["dir"]; !ok { + named := map[string]any{"dir": "."} + for k, v := range args { + named[k] = v + } + args = named + } + return raw(args) + } +} + +func rawFetchTool(t *testing.T, m *Module) func(map[string]any) (any, error) { t.Helper() for _, tool := range m.Tools(m.StepTools()) { if tool.Name == "build_go_fetch" { @@ -107,7 +131,9 @@ var fetchArgs = map[string]any{"repository": "novox/mesh-sdk", "commit": commitS // 1485/AC1: git is asked through FetchCommit with the ssh remote, and go mod download and verify run in each go.mod // directory (not in one that vendors) with GOTOOLCHAIN=local, GOFLAGS=-mod=mod and the module's cache paths. func TestFetchAsksGitForTheSshRemoteAndDownloadsEachModule(t *testing.T) { - f := &fakeStep{gomods: "go.mod\nsub/dir/go.mod\nvend/go.mod\n", vendors: []string{"vend"}} + // The root go.mod replaces the other two by directory: a fetch asked with a module's directory chooses what it replaces. + f := &fakeStep{gomods: "go.mod\nsub/dir/go.mod\nvend/go.mod\n", vendors: []string{"vend"}, + files: map[string]string{"go.mod": "module fake\nreplace a => ./sub/dir\nreplace b => ./vend\n"}} m, states := fetchModule(t, f) if _, err := fetchTool(t, m)(fetchArgs); err != nil { t.Fatal(err) @@ -270,7 +296,8 @@ func TestAVerifyThatFailsEndsTheStepNamingTheDirectoryAndTheLine(t *testing.T) { return forgekey.Ran{} }} m, states := fetchModule(t, f) - if _, err := fetchTool(t, m)(fetchArgs); err != nil { + // Asked for the module of tools, as a build is for one module (A-447b). + if _, err := fetchTool(t, m)(map[string]any{"repository": "novox/mesh-sdk", "commit": commitSHA, "dir": "tools"}); err != nil { t.Fatal(err) } st := states.waitEnd(t) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go index 75782a9fa..1001dbb0a 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-sdk/go/forgekey" ) @@ -93,3 +94,23 @@ func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *test t.Errorf("cleanDir of nothing = %q, of . = %q", cleanDir(""), cleanDir(".")) } } + +// A-447b: a build is for one module, never a repository: a fetch with no module directory is refused, naming its asker +// and the repository, and nothing is fetched. +func TestAFetchWithNoDirectoryIsRefusedNamingItsAsker(t *testing.T) { + f := &fakeStep{gomods: "go.mod\n"} + m, states := fetchModule(t, f) + if _, err := rawFetchTool(t, m)(map[string]any{"repository": "novox/mesh-sdk", "commit": commitSHA}); err != nil { + t.Fatal(err) + } + st := states.waitEnd(t) + if st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, "novox/mesh-sdk") || !strings.Contains(st.Reason, "names no module directory") || + !strings.Contains(st.Reason, "asked by") { + t.Fatalf("a fetch with no dir ended %s: %q", st.State, st.Reason) + } + for _, l := range f.lines() { + if strings.HasPrefix(l, "git ") { + t.Errorf("a refused fetch ran %s", l) + } + } +} diff --git a/modules/mesh-build-go/cmd/mesh-build-go/mirror_test.go b/modules/mesh-build-go/cmd/mesh-build-go/mirror_test.go index af3f63d02..a71ca19f0 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/mirror_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/mirror_test.go @@ -176,6 +176,10 @@ func TestTheWorkspaceIsSelfContainedAfterAFetchThroughTheMirror(t *testing.T) { return strings.TrimSpace(string(out)) } git(src, "init", "-q") + // A fetch is for one module (A-447b): the repository holds a module at its root. + if err := os.WriteFile(filepath.Join(src, "go.mod"), []byte("module example.org/x\n\ngo 1.26\n"), 0o644); err != nil { + t.Fatal(err) + } if err := os.WriteFile(filepath.Join(src, "f.txt"), []byte("one\n"), 0o644); err != nil { t.Fatal(err) } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/narrow_integration_test.go b/modules/mesh-build-go/cmd/mesh-build-go/narrow_integration_test.go index 2ce32371e..e7ef09577 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/narrow_integration_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/narrow_integration_test.go @@ -45,7 +45,7 @@ func TestAFetchWithNamedPathsNarrowsTheWorkspace(t *testing.T) { if strings.Join(res.Modules, ",") != "lib" { t.Errorf("the fetch answered the modules %v, want lib alone", res.Modules) } - _, st = fetched(t, m, states, map[string]any{"repository": "novox/narrow", "commit": head, "paths": []any{"cmd"}}) + _, 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) } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/pins_test.go b/modules/mesh-build-go/cmd/mesh-build-go/pins_test.go index 17b5f20e8..5b957bc42 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/pins_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/pins_test.go @@ -50,7 +50,15 @@ func newPinHarness(t *testing.T, gomod string, vendor bool, pulls string) (*pinH for _, g := range strings.Fields(f.gomods) { d := filepath.Join(args[i+1], filepath.Dir(g)) os.MkdirAll(d, 0o755) - os.WriteFile(filepath.Join(d, "go.mod"), []byte(gomod), 0o644) + body := gomod + if g == "go.mod" { // the root replaces the other directories, so a fetch asked with it chooses them (A-447) + for _, o := range strings.Fields(f.gomods) { + if o != "go.mod" { + body += "\nreplace local/" + filepath.Dir(o) + " => ./" + filepath.Dir(o) + "\n" + } + } + } + os.WriteFile(filepath.Join(d, "go.mod"), []byte(body), 0o644) os.WriteFile(filepath.Join(d, "go.sum"), []byte("sum\n"), 0o644) } if vendor { diff --git a/modules/mesh-build-go/cmd/mesh-build-go/run.go b/modules/mesh-build-go/cmd/mesh-build-go/run.go index cdd204725..24c9e50eb 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/run.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/run.go @@ -36,7 +36,7 @@ func (m *Module) run(ctx context.Context, fetch steprun.StepFunc, a sdkstep.Step return nil, err } key := sdkstep.RunKey(job.Program, sdkstep.ToolchainUsed{Version: installed, Recipe: m.Recipe.Version()}, a.Packages) - fetchArgs := sdkstep.FetchRequest{Repository: job.Program.Repository, Commit: job.Program.Commit, Packages: a.Packages}.Args() + fetchArgs := sdkstep.FetchRequest{Repository: job.Program.Repository, Commit: job.Program.Commit, Dir: job.Program.Dir, Packages: a.Packages}.Args() exe, built, err := m.programOf(ctx, fetch, "programs", "checking program", key, fetchArgs, job.Program.Dir, job.Program.Package) if err != nil { return nil, err diff --git a/modules/mesh-build-go/cmd/mesh-build-go/verify_once_test.go b/modules/mesh-build-go/cmd/mesh-build-go/verify_once_test.go index 3305e5cfd..cf7beee85 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/verify_once_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/verify_once_test.go @@ -53,7 +53,7 @@ func TestGoModVerifyRunsOncePerGoModAndGoSumDigest(t *testing.T) { // Without bubblewrap the module cache is not read-only to a step: a pass is never reused. func TestAPassOfVerifyIsNotReusedWhenStepsAreNotShutOff(t *testing.T) { - f := &fakeStep{gomods: "a/go.mod\nb/go.mod\n", files: map[string]string{"a/go.mod": "m\n", "b/go.mod": "m\n"}} + f := &fakeStep{gomods: "a/go.mod\nb/go.mod\n", files: map[string]string{"a/go.mod": "module m\n", "b/go.mod": "module m\n"}} m, states := fetchModule(t, f) // no Bubblewrap fetchOnce(t, m, states, fetchArgs) fetchOnce(t, m, states, fetchArgs) @@ -65,7 +65,7 @@ func TestAPassOfVerifyIsNotReusedWhenStepsAreNotShutOff(t *testing.T) { // A verify that fails leaves no mark: the next fetch verifies again. func TestAFailedVerifyLeavesNoMark(t *testing.T) { fail := true - f := &fakeStep{gomods: "go.mod\n", files: map[string]string{"go.mod": "m\n"}} + f := &fakeStep{gomods: "go.mod\n", files: map[string]string{"go.mod": "module m\n"}} f.goRule = func(line string) forgekey.Ran { if strings.HasSuffix(line, " mod verify") && fail { return forgekey.Ran{Status: 1, Stdout: "x v1: dir has been modified\n"} @@ -88,7 +88,7 @@ func TestDownloadsRunSideBySideUpToTheBound(t *testing.T) { running, most := 0, 0 release := make(chan struct{}) // closed once two run at once: they overlap however slowly the machine starts them f := &fakeStep{gomods: "a/go.mod\nb/go.mod\nc/go.mod\nd/go.mod\n", files: map[string]string{ - "a/go.mod": "a\n", "b/go.mod": "b\n", "c/go.mod": "c\n", "d/go.mod": "d\n"}} + "a/go.mod": "module a\n", "b/go.mod": "module b\n", "c/go.mod": "module c\n", "d/go.mod": "module d\n"}} f.goRule = func(line string) forgekey.Ran { if strings.HasSuffix(line, " mod download") { mu.Lock() From 928de07c431cfb50812c2eedbb3fc179aaf04e2a Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 16 Nov 2026 11:37:23 +0100 Subject: [PATCH 4/6] Go build module: a step for a module compares its own module.json's generated files alone (A-447c) Builds: design 47 G (G-22 lead answers 2) A-447c --- .../mesh-build-go/cmd/mesh-build-go/fetch.go | 16 +++++++++ .../mesh-build-go/cmd/mesh-build-go/test.go | 8 +++-- .../cmd/mesh-build-go/test_test.go | 33 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go index 2ab8fff54..046e9a9b3 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go @@ -184,6 +184,9 @@ func (m *Module) fetchInto(ctx context.Context, req sdkstep.FetchRequest, id str if err := os.WriteFile(filepath.Join(root, scopeMark), []byte(strings.Join(dirs, "\n")), 0o600); err != nil { return nil, err } + if err := os.WriteFile(filepath.Join(root, moduleDirMark), []byte(dir), 0o600); err != nil { + return nil, err + } } res.Modules = dirs if base != "" && res.Merge == "" { @@ -637,6 +640,19 @@ func narrowed(tree string) bool { // directory chose, one to a line (empty: none). const scopeMark = "gomodules" +// moduleDirMark is the file beside a workspace's tree naming the module directory the fetch was asked with: the one +// module.json a step compares generated files for (design 47 G, A-447c). +const moduleDirMark = "moduledir" + +// moduleDirOf is the module directory the workspace of tree was fetched for, ok false for a workspace with none. +func moduleDirOf(tree string) (string, bool) { + b, err := os.ReadFile(filepath.Join(filepath.Dir(tree), moduleDirMark)) + if err != nil { + return "", false + } + return string(b), true +} + // cleanDir is a request's module directory in the form goModDirs lists, "" for none. func cleanDir(dir string) string { if strings.TrimSpace(dir) == "" { diff --git a/modules/mesh-build-go/cmd/mesh-build-go/test.go b/modules/mesh-build-go/cmd/mesh-build-go/test.go index 4ec9307df..108c73940 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/test.go @@ -109,18 +109,22 @@ func (m *Module) test(ctx context.Context, args map[string]any) (any, error) { } // compareGenerated regenerates the generated Go files of each contracts module in the tree (every module.json in the tree, in -// any folder: ADR 0634 §8; CAT-BG5) and fails the level on any out of step, Why naming each path. +// any folder: ADR 0634 §8; CAT-BG5; for a workspace fetched for a module's directory, that module's own alone) and fails the level on any out of step, Why naming each path. func compareGenerated(tree string, res *TestResult) error { dirs, err := steprun.ModuleDirs(tree) if err != nil { return err } for _, dir := range dirs { + // A step for a module compares its own module.json alone, never another module's of the repository (A-447c). + rel, _ := filepath.Rel(tree, dir) + if own, ok := moduleDirOf(tree); ok && filepath.ToSlash(rel) != own { + continue + } paths, err := steprun.OutOfStep(dir, steprun.GeneratedRootGo) if err != nil { return err } - rel, _ := filepath.Rel(tree, dir) for _, p := range paths { res.OutOfStep = append(res.OutOfStep, filepath.ToSlash(filepath.Join(rel, p))) } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/test_test.go b/modules/mesh-build-go/cmd/mesh-build-go/test_test.go index 0931f3dcd..779b4dba5 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/test_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/test_test.go @@ -340,3 +340,36 @@ func TestADirectoryThatVendorsIsReadFromItsVendorDirectory(t *testing.T) { t.Errorf("the last GOFLAGS of go test in a vendoring directory is %q", flags) } } + +// A-447c: a workspace fetched for a module's directory compares the generated files of that module's own module.json +// alone, never another module's of the repository. +func TestUnitLevelComparesOnlyTheOwnModulesGeneratedFiles(t *testing.T) { + m, states, ws, _ := workspaceFor(t, "empty") + tree := filepath.Join(m.Work, ws, "tree") + for _, folder := range []string{"domain", "other"} { + for p, text := range map[string]string{ + "module.json": `{"module":"c","build":{"artifacts":[{"name":"go","kind":"package","language":"go","paths":["go"],"generated-from":"."}]}}`, + "seats/one-store/seat.json": `{"format":1,"seat":"one-store","scope":"mesh","grain":"seat-list","domain":"Module","decision":"ADR 1","verbs":[{"name":"query","input":{"type":"object"}}]}`, + "go/go.mod": "module example.org/c/go\n", + "go/stray.go": "package stray\n", + } { + full := filepath.Join(tree, folder, filepath.FromSlash(p)) + _ = os.MkdirAll(filepath.Dir(full), 0o755) + if err := os.WriteFile(full, []byte(text), 0o644); err != nil { + t.Fatal(err) + } + } + } + if err := os.WriteFile(filepath.Join(m.Work, ws, moduleDirMark), []byte("domain"), 0o600); err != nil { + t.Fatal(err) + } + res, _ := runUnit(t, m, states, ws) + if len(res.OutOfStep) == 0 { + t.Fatalf("nothing out of step: %+v", res) + } + for _, p := range res.OutOfStep { + if !strings.HasPrefix(p, "domain/") { + t.Errorf("out of step = %v, want the module's own files under domain/ alone", res.OutOfStep) + } + } +} From ae78fb039f99b2a3a90546806c4176063fc893ce Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 16 Nov 2026 11:40:18 +0100 Subject: [PATCH 5/6] Go fetch with no dir fetches the tree whole again, for the checker until G removes it (A-447e) Builds: design 47 G (G-22 lead answers 2) A-447e --- .../mesh-build-go/cmd/mesh-build-go/fetch.go | 17 ++--------------- .../cmd/mesh-build-go/gomoddirs_test.go | 18 ++++++------------ 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go index 046e9a9b3..cd61cc20a 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go @@ -22,8 +22,6 @@ import ( "golang.org/x/mod/modfile" - stdio "git.novox.be/novox/mesh-sdk/go" - sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" dforgekey "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/forgekey" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" @@ -110,11 +108,8 @@ func (m *Module) fetch(ctx context.Context, args map[string]any) (res any, err e if err != nil { return nil, err } - // A build is for one module, never a repository (design 47 G, A-447b): a fetch names its module's directory. - if cleanDir(req.Dir) == "" { - return nil, fmt.Errorf("the fetch of %s at %s names no module directory (dir): a fetch is for one module, never a whole repository; asked by %s", - req.Repository, req.Commit, askerOf(args)) - } + // A fetch with no dir (the checker's check of a pull request, until G removes the checker; A-447e) fetches the tree + // whole: every go.mod of it. siblings, err := siblingsArg(args["beside"]) if err != nil { return nil, err @@ -717,11 +712,3 @@ func scopedDirs(tree, dir string, all []string) ([]string, error) { } return out, nil } - -// askerOf names who asked a tool: the caller the bus proved, else "an unnamed asker". -func askerOf(args map[string]any) string { - if c := stdio.CallerOf(args); c != "" { - return c - } - return "an unnamed asker" -} diff --git a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go index 1001dbb0a..f08f77218 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go @@ -95,22 +95,16 @@ func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *test } } -// A-447b: a build is for one module, never a repository: a fetch with no module directory is refused, naming its asker -// and the repository, and nothing is fetched. -func TestAFetchWithNoDirectoryIsRefusedNamingItsAsker(t *testing.T) { - f := &fakeStep{gomods: "go.mod\n"} +// A-447e: a fetch with no dir (the checker's check of a pull request, until G removes the checker) fetches the tree whole: +// it chooses every go.mod of it. +func TestAFetchWithNoDirectoryChoosesEveryGoModOfTheTree(t *testing.T) { + f := &fakeStep{gomods: "go.mod\nsub/go.mod\n"} m, states := fetchModule(t, f) if _, err := rawFetchTool(t, m)(map[string]any{"repository": "novox/mesh-sdk", "commit": commitSHA}); err != nil { t.Fatal(err) } st := states.waitEnd(t) - if st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, "novox/mesh-sdk") || !strings.Contains(st.Reason, "names no module directory") || - !strings.Contains(st.Reason, "asked by") { - t.Fatalf("a fetch with no dir ended %s: %q", st.State, st.Reason) - } - for _, l := range f.lines() { - if strings.HasPrefix(l, "git ") { - t.Errorf("a refused fetch ran %s", l) - } + if st.State != sdkstep.StepPassed || !strings.Contains(string(st.Result), `"modules":[".","sub"]`) { + t.Fatalf("a fetch with no dir ended %s: %q %s", st.State, st.Reason, st.Result) } } From cdf168706ba9164dd52b44529916f40c744b8692 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 16 Nov 2026 11:42:56 +0100 Subject: [PATCH 6/6] Go steps test a module's own directories alone, not what its go.mod replaces from outside (A-447c) Builds: design 47 G (G-22 lead answers 2) A-447c --- .../mesh-build-go/cmd/mesh-build-go/digest.go | 2 +- .../mesh-build-go/cmd/mesh-build-go/fetch.go | 21 ++++++++++ .../cmd/mesh-build-go/gomoddirs_test.go | 41 +++++++++++++++++++ .../cmd/mesh-build-go/integration.go | 2 +- .../mesh-build-go/cmd/mesh-build-go/test.go | 2 +- 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/digest.go b/modules/mesh-build-go/cmd/mesh-build-go/digest.go index fe7c06c79..9aa9c4616 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/digest.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/digest.go @@ -260,7 +260,7 @@ func (m *Module) digestHere(ctx context.Context, workspace, level string, names namesHeader = []string{"node-names " + strings.Join(sortedNames, ",")} } - dirs, err := m.goModDirs(ctx, tree) + dirs, err := m.testDirs(ctx, tree) if err != nil { return DigestResult{}, err } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go index cd61cc20a..04a18aadf 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go @@ -476,6 +476,27 @@ func (m *Module) goModDirs(ctx context.Context, tree string) ([]string, error) { return dirs, nil } +// testDirs are the directories the unit and integration steps and the digest take: the chosen go.mod directories at or +// under the module's own directory. A directory the module's go.mod replaces from outside its own stays for download and +// resolution alone; its tests are another module's (design 47 G, A-447c). A workspace fetched with no dir keeps every go.mod. +func (m *Module) testDirs(ctx context.Context, tree string) ([]string, error) { + all, err := m.goModDirs(ctx, tree) + if err != nil { + return nil, err + } + own, ok := moduleDirOf(tree) + if !ok || own == "." || own == "" { + return all, nil + } + out := []string{} + for _, d := range all { + if d == own || strings.HasPrefix(d, own+"/") { + out = append(out, d) + } + } + return out, nil +} + // inTestdata says a path lies under a directory named testdata, whose go.mod files are fixtures Go ignores, not modules // of the tree (issue 1810). func inTestdata(path string) bool { diff --git a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go index f08f77218..1a7a8d429 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go @@ -108,3 +108,44 @@ func TestAFetchWithNoDirectoryChoosesEveryGoModOfTheTree(t *testing.T) { t.Fatalf("a fetch with no dir ended %s: %q %s", st.State, st.Reason, st.Result) } } + +// A-447c: the unit level of a module whose go.mod replaces ../lib tests the module alone; lib is downloaded and resolved +// for it, and its tests are another module's. +func TestTheUnitLevelTestsOnlyTheModulesOwnDirectoriesNotWhatItReplaces(t *testing.T) { + f := &fakeStep{} + m, _ := fetchModule(t, f) + root := filepath.Join(m.Work, "ws1") + tree := filepath.Join(root, "tree") + for d, body := range map[string]string{"mod": "module example.org/mod\nreplace example.org/lib => ../lib\n", "lib": "module example.org/lib\n"} { + if err := os.MkdirAll(filepath.Join(tree, d), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(tree, d, "go.mod"), []byte(body), 0o644); err != nil { + t.Fatal(err) + } + } + for name, body := range map[string]string{scopeMark: "lib\nmod", moduleDirMark: "mod"} { + if err := os.WriteFile(filepath.Join(root, name), []byte(body), 0o600); err != nil { + t.Fatal(err) + } + } + dirs, err := m.testDirs(context.Background(), tree) + if err != nil || !reflect.DeepEqual(dirs, []string{"mod"}) { + t.Fatalf("the test directories = %v, %v; want mod alone", dirs, err) + } + if _, err := m.runLevel(context.Background(), tree, levelOpts{workspace: "ws1", level: LevelUnit}); err != nil { + t.Fatal(err) + } + for _, l := range f.lines() { + if strings.Contains(l, "/tree/lib test") { + t.Errorf("the module's unit level tested lib: %s", l) + } + } + // No dir asked: every go.mod of the tree stays. + if err := os.Remove(filepath.Join(root, moduleDirMark)); err != nil { + t.Fatal(err) + } + if dirs, _ = m.testDirs(context.Background(), tree); len(dirs) != 2 { + t.Errorf("a workspace with no module directory tests %v, want every go.mod", dirs) + } +} diff --git a/modules/mesh-build-go/cmd/mesh-build-go/integration.go b/modules/mesh-build-go/cmd/mesh-build-go/integration.go index 86c9d6db5..cfa51ba98 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/integration.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/integration.go @@ -58,7 +58,7 @@ func (m *Module) integrationHere(ctx context.Context, workspace, tree string, pa defer os.RemoveAll(run) began := m.clock() - dirs, err := m.goModDirs(ctx, tree) + dirs, err := m.testDirs(ctx, tree) if err != nil { return nil, err } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/test.go b/modules/mesh-build-go/cmd/mesh-build-go/test.go index 108c73940..e76b9b2c5 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/test.go @@ -256,7 +256,7 @@ type levelOpts struct { // runLevel runs `go test -json` in each go.mod directory of the tree and judges the whole against the bounds. func (m *Module) runLevel(ctx context.Context, tree string, o levelOpts) (*TestResult, error) { - dirs, err := m.goModDirs(ctx, tree) + dirs, err := m.testDirs(ctx, tree) if err != nil { return nil, err }