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
This commit is contained in:
2026-11-16 11:09:26 +01:00
parent 2679399d60
commit 1168376930
2 changed files with 125 additions and 0 deletions
@@ -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
}
@@ -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("."))
}
}