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
This commit is contained in:
2026-11-16 11:16:16 +01:00
parent 1168376930
commit ccab80d726
2 changed files with 32 additions and 3 deletions
@@ -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
}
@@ -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)
}