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 539790977..bb65603be 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go @@ -26,6 +26,7 @@ import ( 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" + "git.novox.be/novox/mesh-sdk/go/catalogue" ) var commitOK = regexp.MustCompile(`^[0-9a-f]{40}$`) @@ -737,8 +738,33 @@ func cleanDir(dir string) string { return strings.Trim(filepath.ToSlash(filepath.Clean(dir)), "/") } +// packagePaths are the named paths of the Go package artifacts of the manifest in the module directory dir of the tree, +// as the import step lists them (ModuleImports); none when the directory holds no manifest the catalogue reads. +func packagePaths(tree, dir string) []string { + raw, err := os.ReadFile(filepath.Join(tree, filepath.FromSlash(dir), "module.json")) + if err != nil { + return nil + } + manifest, err := catalogue.Decode(raw) + if err != nil || manifest.Build == nil { + return nil + } + var out []string + for _, a := range manifest.Build.Artifacts { + if a.Language == "go" && a.Kind == "package" { + for _, p := range a.Paths { + if p = strings.Trim(strings.TrimSpace(p), "./"); p != "" { + out = append(out, p) + } + } + } + } + return out +} + // 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 +// go.mod replaces with a local path, followed through their own replaces (design 47 G, A-447), in order, and each package +// path of the ask holding a go.mod of its own (H-A-762). A directory // the tree holds no go.mod for is left out. A directory holding no go.mod chooses every go.mod under it (A-447a), and one that // chooses no Go module at all is an empty scope (H-A-371). func scopedDirs(tree, dir string, paths, all []string) ([]string, error) { @@ -770,6 +796,17 @@ func scopedDirs(tree, dir string, paths, all []string) ([]string, error) { } } } + // Each path of the module's Go package artifacts that holds a Go module file of its own (the builder's builds, a state + // package beside a program) is a module the fetch gives its own module file, as the build and the import step's listing + // of that path run with it (design 47 H2, H-A-762, H-A-765, H-A-633): the paths the module's manifest names, as the + // import step lists them, whether or not the ask names paths, and those the ask names. + if have[dir] { + for _, p := range append(slices.Clone(paths), packagePaths(tree, dir)...) { + if d := path.Join(dir, filepath.ToSlash(p)); have[d] { + todo = append(todo, d) + } + } + } for len(todo) > 0 { d := todo[0] todo = todo[1:] 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 105f302ca..9cbbfdeda 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 @@ -415,6 +415,9 @@ func TestARequirementOfTheCheckedRepositorysOwnModuleIsTakenFromItsTree(t *testi name, own, ownDir, gomods, version string args map[string]any narrowed bool + files map[string]string // laid over the checkout, after the rest + dirs int // the Go module directories the fetch takes (default 1), each with a modfile + listed string // a path directory whose import listing must run with its own modfile }{ {name: "whole tree", own: "git.novox.be/novox/mesh-delivery/nested/go", ownDir: "nested/go", gomods: "go.mod\n", version: zero, args: map[string]any{"repository": "novox/mesh-delivery", "commit": commitSHA}}, @@ -430,6 +433,27 @@ func TestARequirementOfTheCheckedRepositorysOwnModuleIsTakenFromItsTree(t *testi ownDir: "modules/mesh-delivery-sdk", gomods: "modules/mesh-delivery-contracts/go/clients/meshcatalogclient/go.mod\n", version: "v0.1.3", args: map[string]any{"repository": "novox/mesh-delivery", "commit": commitSHA, "dir": "modules/mesh-delivery-contracts", "paths": []any{"go/clients/meshcatalogclient"}}, narrowed: true}, + // H-A-762, H-A-765 (with H-A-633), in the live form: the ask names no paths, the module's manifest names its package + // paths. mesh-delivery-builder's layout: a bundle and a package `builds` with a module file of its own that the + // module's file does not replace; the import step's listing of builds runs with its own modfile. + {name: "a package path with a module file of its own, named by the manifest alone", own: "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk", + ownDir: "modules/mesh-delivery-sdk", gomods: "modules/mesh-delivery-builder/go.mod\nmodules/mesh-delivery-builder/builds/go.mod\n", version: zero, + args: map[string]any{"repository": "novox/mesh-delivery", "commit": commitSHA, "dir": "modules/mesh-delivery-builder"}, + dirs: 2, listed: "modules/mesh-delivery-builder/builds", files: map[string]string{ + "modules/mesh-delivery-builder/module.json": `{"module":"mesh-delivery-builder","build":{"artifacts":[{"name":"program","kind":"bundle","language":"go","from":"cmd/x","binary":"x"},{"name":"builds","kind":"package","language":"go","paths":["builds"]}]}}`}}, + // mesh-grants' layout: package paths whose module files the module's file does not replace; one replaces a module of + // the same repository by a directory outside its own (builds), whose own requirement lies behind a further local + // replace (deep): each module file replaces the own-repository module the graph requires, a replaced module's + // requirements included. + {name: "a module replacing a package path whose requirements are of the same repository", own: "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk", + ownDir: "modules/mesh-delivery-sdk", version: zero, + gomods: "modules/mesh-grants/go.mod\nmodules/mesh-grants/grantsstate/go.mod\nmodules/mesh-delivery-builder/builds/go.mod\nmodules/mesh-delivery-builder/builds/deep/go.mod\n", + args: map[string]any{"repository": "novox/mesh-delivery", "commit": commitSHA, "dir": "modules/mesh-grants"}, + dirs: 4, listed: "modules/mesh-grants/grantsstate", files: map[string]string{ + "modules/mesh-grants/module.json": `{"module":"mesh-grants","build":{"artifacts":[{"name":"grantsstate","kind":"package","language":"go","paths":["grantsstate"]}]}}`, + "modules/mesh-grants/grantsstate/go.mod": "module git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate\n\ngo 1.26\n\nrequire git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds " + zero + + "\n\nreplace git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds => ../../mesh-delivery-builder/builds\n", + "modules/mesh-delivery-builder/builds/go.mod": "module git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds\n\ngo 1.26\n\nrequire fixture/deep v0.0.0\n\nreplace fixture/deep => ./deep\n"}}, } { t.Run(tc.name, func(t *testing.T) { ownMod := "module fixture/member\n\ngo 1.26\n\nrequire (\n\t" + tc.own + " " + tc.version + "\n)\n" @@ -447,6 +471,9 @@ func TestARequirementOfTheCheckedRepositorysOwnModuleIsTakenFromItsTree(t *testi files["modules/mesh-delivery-contracts/module.json"] = `{"module":"mesh-delivery-contracts","build":{"artifacts":[{"name":"go-meshcatalogclient","kind":"package","language":"go","paths":["go/clients/meshcatalogclient"]}]}}` files["modules/mesh-delivery-view/go.mod"] = "module git.novox.be/novox/mesh-delivery/modules/mesh-delivery-view\n\ngo 1.26\n" } + for f, body := range tc.files { + files[f] = body + } for f, body := range files { os.MkdirAll(filepath.Join(tree, filepath.Dir(f)), 0o755) os.WriteFile(filepath.Join(tree, f), []byte(body), 0o644) @@ -469,8 +496,8 @@ func TestARequirementOfTheCheckedRepositorysOwnModuleIsTakenFromItsTree(t *testi 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) + if want := max(tc.dirs, 1); len(modfiles) != want { + t.Fatalf("modfiles = %v, want %d", modfiles, want) } root := filepath.Dir(filepath.Dir(filepath.Dir(modfiles[0]))) where := filepath.Join(root, "tree", filepath.FromSlash(tc.ownDir)) @@ -479,19 +506,26 @@ func TestARequirementOfTheCheckedRepositorysOwnModuleIsTakenFromItsTree(t *testi 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) + for _, modfile := range modfiles { + body, _ := os.ReadFile(modfile) + if !strings.Contains(string(body), "replace "+tc.own+" => "+where+"\n") { + t.Errorf("the module of the repository was not replaced into %s by %s:\n%s", modfile, where, body) + } + if tc.dirs <= 1 && 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 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 tc.listed != "" { + if env := strings.Join(h.m.buildEnv(filepath.Join(root, "tree", filepath.FromSlash(tc.listed))), " "); !strings.Contains(env, "-modfile=") { + t.Errorf("the listing of %s runs without its own modfile: %s", tc.listed, env) + } } 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") } var res FetchResult json.Unmarshal(st.Result, &res) - if len(res.Packages) != 1 || res.Packages[0].Package != tc.own || res.Packages[0].Built != "the fetched tree" || res.Packages[0].Named != tc.version { + if len(res.Packages) != max(tc.dirs, 1) || res.Packages[0].Package != tc.own || res.Packages[0].Built != "the fetched tree" || res.Packages[0].Named != tc.version { t.Errorf("the taken versions = %+v, want %s named %s, built from the fetched tree", res.Packages, tc.own, tc.version) } }) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/read.go b/modules/mesh-build-go/cmd/mesh-build-go/read.go index b036c8b3b..3949dfb72 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/read.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/read.go @@ -89,19 +89,26 @@ func readValues(keys []string, get func(string) (json.RawMessage, error)) ([]jso return out, fail } -// stepKey is the key of a step of this node: the id alone, or the whole key . as a step tool answered it. +// stepKey is the key of a step of this node: the id alone, or a whole key as a step tool answered it, `.` or an +// ask's `..` (design 47 G §481); the node of the key is this module's. func (m *Module) stepKey(id string) (string, error) { id = strings.TrimSpace(id) - if node, rest, ok := strings.Cut(id, "."); ok { - if node != m.Node { - return "", fmt.Errorf("the step %q is of node %q, this module runs on %q", id, node, m.Node) - } - id = rest + if !strings.Contains(id, ".") { + return sdkstep.StepKey(m.Node, id) } - return sdkstep.StepKey(m.Node, id) + _, node, _, err := sdkstep.ParseStepKey(id) + if err != nil { + return "", err + } + if node != m.Node { + return "", fmt.Errorf("the step %q is of node %q, this module runs on %q", id, node, m.Node) + } + return id, nil } -// ReadStep is the stored value of this node's step id (state, reason, result). +// ReadStep is the stored value of this node's step id (state, reason, result). A step is stored under `.`, or +// under its asker for an ask (`..`, design 47 G §481): when the key as given holds none, the keys of the +// state are searched for this node's step of that id under either form, as the verb output's read does (EndedHere). func (m *Module) ReadStep(id string) (sdkstep.Step, error) { key, err := m.stepKey(id) if err != nil { @@ -114,6 +121,24 @@ func (m *Module) ReadStep(id string) (sdkstep.Step, error) { if err != nil { return sdkstep.Step{}, err } + if len(raw) == 0 && m.ReadSteps.Keys != nil { + _, _, want, _ := sdkstep.ParseStepKey(key) + keys, err := m.ReadSteps.Keys() + if err != nil { + return sdkstep.Step{}, err + } + for _, k := range keys { + if _, node, id, err := sdkstep.ParseStepKey(k); err != nil || node != m.Node || id != want { + continue + } + if raw, err = m.ReadSteps.Get(k); err != nil { + return sdkstep.Step{}, err + } + if len(raw) > 0 { + break + } + } + } if len(raw) == 0 { return sdkstep.Step{}, fmt.Errorf("no step %s in the steps state", key) } @@ -229,9 +254,10 @@ func (m *Module) ReadTools() []stdio.Tool { {Name: StepReadTool, Description: "What one step of this machine ended with, as stored in the module's steps state: its tool, state " + "(running, passed, failed, refused), when it started and ended, why it failed or was refused, and its result " + - "(a fetch's workspace id is there). id is the step id a step tool answered, or the whole key .. Read-only.", + "(a fetch's workspace id is there). id is the step id a step tool answered, or the whole key: ., or .. " + + "for an ask's step, the asker the asking seat's name. Read-only.", Input: map[string]any{"type": "object", "required": []string{"id"}, "properties": map[string]any{ - "id": map[string]any{"type": "string", "description": "the step id, or the key ."}}}, + "id": map[string]any{"type": "string", "description": "the step id, or the key . or .."}}}, Run: func(args map[string]any) (any, error) { id, _ := args["id"].(string) return m.ReadStep(id) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/read_test.go b/modules/mesh-build-go/cmd/mesh-build-go/read_test.go index 8b5d2776a..673694c30 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/read_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/read_test.go @@ -74,6 +74,16 @@ func TestStepToolAnswersTheStoredStep(t *testing.T) { t.Errorf("id %s: step = %+v", id, st) } } + // An ask's step is stored under its asker (design 47 G §481, issue 1984 note 52): found by its id, or by its whole key. + asked, _ := sdkstep.AskStepKey("mesh-wants", "ace", "mwwmppk5788534") + b, _ := sdkstep.EncodeStep(sdkstep.Step{ID: "mwwmppk5788534", Tool: "build_go_test", Node: "ace", State: sdkstep.StepPassed, Started: t0, Ended: t0.Add(time.Second)}) + steps[asked] = b + for _, id := range []string{"mwwmppk5788534", "ace.mwwmppk5788534", asked} { + out, err := tool(t, m, StepReadTool)(map[string]any{"id": id}) + if err != nil || out.(sdkstep.Step).ID != "mwwmppk5788534" { + t.Errorf("the ask's step by %q = %v, %v", id, out, err) + } + } out, _ := tool(t, m, StepReadTool)(map[string]any{"id": "abc2"}) if st := out.(sdkstep.Step); st.State != sdkstep.StepRefused || st.Reason != "go cannot be run" { t.Errorf("refused step = %+v", st)