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
This commit is contained in:
2026-11-16 11:37:23 +01:00
parent 666be37916
commit 928de07c43
3 changed files with 55 additions and 2 deletions
@@ -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) == "" {
@@ -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)))
}
@@ -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)
}
}
}