diff --git a/modules/mesh-delivery-indexer/internal/index/modules.go b/modules/mesh-delivery-indexer/internal/index/modules.go index 0b130d11..a67cd6b7 100644 --- a/modules/mesh-delivery-indexer/internal/index/modules.go +++ b/modules/mesh-delivery-indexer/internal/index/modules.go @@ -63,6 +63,7 @@ func (ix *Indexer) applyMoving(ctx context.Context, f whole, drop bool, moves ma return w, err } objects, namedErr := ix.namedObjects(ctx, f) + trees := ix.goModTree(ctx, f) overlay := map[string]meshcatalog.ModulesByModuleValue{} dirOf := map[string]string{} named, every := map[string]bool{}, true // every module the message's manifests name, and whether each names one @@ -109,7 +110,23 @@ func (ix *Indexer) applyMoving(ctx context.Context, f whole, drop bool, moves ma next.None2 = "the manifest cannot be read: it cannot be kept as a value" } else { packages := ix.goPackages(ctx, f, dir, cur, has) + if m, err := judge.ForInputs([]byte(raw)); err == nil && usesGo(languagesOf(m)) { + // The entry pins every mesh package of another repository the module's build requires, transitively across + // the same-repository modules of its graph (design 47 H-A-201). + packages = unionSorted(packages, trees.otherRequired(goModuleDirs(m, dir))) + } in := inputsOfNamed(name, raw, f.Trees[dir], f.Locks[dir], objects[dir], packages, ix.baseFP(ctx)) + if in.Kept != nil { + // A Go build resolves the same-repository modules its go.mod files require, transitively, from the tree: their + // directories' objects join the tree input of every form, never the entry's paths (design 47 H-A-202, H-A-172). + if m, err := judge.ForInputs([]byte(raw)); err == nil && usesGo(languagesOf(m)) { + if tree, none := ix.derivedTree(ctx, f, trees, m, dir, in.Kept.Tree); none != "" { + in.Kept, in.None2 = nil, none + } else { + in.Kept.Tree = tree + } + } + } next.Inputs2, next.None2, next.Paths = in.Kept, in.None2, in.ArtifactPaths if in.Kept != nil && has && cur.Inputs2 != nil { // The imports its last trunk build reported stand until its next build reports again (ADR 0593 §2, §4). diff --git a/modules/mesh-delivery-indexer/internal/index/package_trees.go b/modules/mesh-delivery-indexer/internal/index/package_trees.go new file mode 100644 index 00000000..40d716d2 --- /dev/null +++ b/modules/mesh-delivery-indexer/internal/index/package_trees.go @@ -0,0 +1,196 @@ +package index + +import ( + "context" + "fmt" + "path" + "path/filepath" + "slices" + "strings" + + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" + "git.novox.be/novox/mesh-sdk/go/catalogue" + + "mesh-delivery-indexer/internal/fingerprint" +) + +// goModTree reads the go.mod files of one merge's tree, each directory once (Indexer.GoMod), for the walk of the module +// graph a Go build resolves from the tree (design 47 H-A-202, H-A-157). +type goModTree struct { + ix *Indexer + ctx context.Context + f whole + read map[string][]byte // by directory from the repository's root; nil for none +} + +func (ix *Indexer) goModTree(ctx context.Context, f whole) *goModTree { + return &goModTree{ix: ix, ctx: ctx, f: f, read: map[string][]byte{}} +} + +// goMod is the go.mod of the directory dir (from the root, "" for the root) at the merge commit; nil when there is none +// or it cannot be read, said. +func (t *goModTree) goMod(dir string) []byte { + if raw, ok := t.read[dir]; ok { + return raw + } + var raw []byte + if t.ix.GoMod != nil { + var err error + if raw, err = t.ix.GoMod(t.ctx, t.f.Repository, t.f.Merge, dir); err != nil { + t.ix.logf("%s at %s: the go.mod of %q cannot be read, so no module of the tree is found there: %v", t.f.Repository, t.f.Merge, dir, err) + raw = nil + } + } + t.read[dir] = raw + return raw +} + +// sameRepositoryDir is the directory of the merge's tree that holds the Go module required: the required path starts with +// the repository's module prefix, and the directory it names holds a go.mod declaring that path (H-A-198, H-A-202). false +// for a module of another repository, or one the tree holds no go.mod of. +func (t *goModTree) sameRepositoryDir(required string) (string, bool) { + repo, dir, ok := trunk.PackageRepository(required) + if !ok || dir == "" || !trunk.SameRepository(repo, t.f.Repository) { + return "", false + } + if goModModule(t.goMod(dir)) != required { + return "", false + } + return dir, true +} + +// sameRepositoryRequired are the directories (from the root, sorted, each once) of every same-repository Go module the +// go.mod of each start directory requires, transitively through those modules' own go.mod files (H-A-202, H-A-157). The +// start directories themselves are not among them. +func (t *goModTree) sameRepositoryRequired(start []string) []string { + seen := map[string]bool{} + for _, s := range start { + seen[s] = true + } + queue := slices.Clone(start) + var out []string + for len(queue) > 0 { + dir := queue[0] + queue = queue[1:] + for _, req := range fingerprint.MeshRequiresOf(t.goMod(dir)) { + d, ok := t.sameRepositoryDir(req) + if !ok || seen[d] { + continue + } + seen[d] = true + out = append(out, d) + queue = append(queue, d) + } + } + slices.Sort(out) + return out +} + +// goModuleDirs are the directories (from the root) a module's Go build starts from: its own directory and the directory +// of each path of each Go package artifact of its manifest m. +func goModuleDirs(m catalogue.Manifest, dir string) []string { + out := []string{dir} + if m.Build == nil { + return out + } + for _, a := range m.Build.Artifacts { + if a.Kind != catalogue.ArtifactPackage || a.Language != "go" { + continue + } + for _, p := range a.Paths { + if d := path.Join(dir, strings.TrimSuffix(filepath.ToSlash(p), "/")); !slices.Contains(out, d) { + out = append(out, d) + } + } + } + return out +} + +// otherRequired are the mesh packages of other repositories (sorted, each once) that the go.mod of each start directory, +// and of every same-repository module those require transitively (sameRepositoryRequired), requires at any version, the +// zero version included: every one the module's build resolves, which the entry pins (design 47 H-A-201, H-A-183, +// H-A-157). A same-repository module is none: it is built from the tree (H-A-198, H-A-202). nil for none. +func (t *goModTree) otherRequired(start []string) []string { + var out []string + for _, d := range append(slices.Clone(start), t.sameRepositoryRequired(start)...) { + out = append(out, fingerprint.OtherPackages(fingerprint.MeshRequiresOf(t.goMod(d)), t.f.Repository)...) + } + if len(out) == 0 { + return nil + } + return slices.Compact(slices.Sorted(slices.Values(out))) +} + +// unionSorted is a and b together, sorted and each once; nil for none. +func unionSorted(a, b []string) []string { + if len(a)+len(b) == 0 { + return nil + } + return slices.Compact(slices.Sorted(slices.Values(append(slices.Clone(a), b...)))) +} + +// derivedDirs are the same-repository module directories (from the root, sorted) the module's Go build resolves from the +// tree beyond its own tree input (H-A-202): every one its go.mod files require, transitively, that its tree input does +// not hold already, which is the module's directory for the directory's tree, and its named paths for the named tree. +func (t *goModTree) derivedDirs(m catalogue.Manifest, dir string) []string { + var held []string + if paths, every := m.NamedPaths(); every { + for _, p := range paths { + held = append(held, path.Join(dir, filepath.ToSlash(p))) + } + } else { + held = []string{dir} + } + var out []string + for _, d := range t.sameRepositoryRequired(goModuleDirs(m, dir)) { + if !slices.ContainsFunc(held, func(h string) bool { return h == "" || h == "." || d == h || strings.HasPrefix(d, h+"/") }) { + out = append(out, d) + } + } + return out +} + +// DerivedLine opens each line of the tree input naming a derived directory and its git object (H-A-202, H-A-172). +const DerivedLine = "derived " + +// derivedTree is the module's tree input with a line per derived directory and its object appended, sorted by directory +// (H-A-202, H-A-172: what a build reads moves its fingerprint), so a change of such a directory moves the fingerprint of +// every form. The objects are asked of the forge at the merge commit by their paths from the root (Named); an answer that +// lacks one gives the reason the module has no inputs, naming them. A module with no derived directory keeps its tree. +func (ix *Indexer) derivedTree(ctx context.Context, f whole, t *goModTree, m catalogue.Manifest, dir, tree string) (string, string) { + dirs := t.derivedDirs(m, dir) + if len(dirs) == 0 { + return tree, "" + } + if ix.Named == nil { + return "", "the forge cannot be asked the git objects of the derived directories " + strings.Join(dirs, ", ") + } + got, err := ix.Named(ctx, f.Repository, f.Merge, map[string][]string{"": dirs}) + if err != nil { + return "", fmt.Sprintf("the git objects of the derived directories %s: %v", strings.Join(dirs, ", "), err) + } + lines := []string{tree} + var missing []string + for _, d := range dirs { + o := got[""][d] + if o == "" { + missing = append(missing, d) + continue + } + lines = append(lines, DerivedLine+d+" "+o) + } + if len(missing) > 0 { + return "", "the forge names no git object of the derived directories " + strings.Join(missing, ", ") + } + return strings.Join(lines, "\n"), "" +} + +// goModModule is the module path a go.mod declares; "" for none. +func goModModule(raw []byte) string { + for _, line := range strings.Split(string(raw), "\n") { + if f := strings.Fields(line); len(f) == 2 && f[0] == "module" { + return strings.Trim(f[1], `"`) + } + } + return "" +} diff --git a/modules/mesh-delivery-indexer/internal/index/package_trees_test.go b/modules/mesh-delivery-indexer/internal/index/package_trees_test.go new file mode 100644 index 00000000..999bb220 --- /dev/null +++ b/modules/mesh-delivery-indexer/internal/index/package_trees_test.go @@ -0,0 +1,144 @@ +package index + +import ( + "context" + "strings" + "testing" + + sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" + "git.novox.be/novox/mesh-sdk/go/catalogue" + + "mesh-delivery-indexer/internal/fingerprint" +) + +// A fixture repository novox/mesh-delivery: the Go package state of modules/g requires the same-repository module +// modules/b/builds at the zero version, which requires modules/c/client in turn; mesh-sdk is of another repository, and +// modules/b/gone names a directory the tree holds no go.mod of. +const ptpZero = " v0.0.0-00010101000000-000000000000" + +var ptpGoMods = map[string]string{ + "modules/g/state": "module git.novox.be/novox/mesh-delivery/modules/g/state\n\nrequire (\n\tgit.novox.be/novox/mesh-delivery/modules/b/builds" + ptpZero + + "\n\tgit.novox.be/novox/mesh-sdk/go" + ptpZero + "\n\tgit.novox.be/novox/mesh-delivery/modules/b/gone" + ptpZero + "\n)\n", + "modules/b/builds": "module git.novox.be/novox/mesh-delivery/modules/b/builds\n\nrequire git.novox.be/novox/mesh-delivery/modules/c/client" + ptpZero + " // indirect\n", + "modules/c/client": "module git.novox.be/novox/mesh-delivery/modules/c/client\n", +} + +// ptpProgram is modules/g's manifest: a program naming no paths beside the package state. +const ptpProgram = `{"module":"g","version":"1.0","build":{"artifacts":[{"name":"program","kind":"bundle","language":"go","system":"arch","from":"cmd/g","binary":"g","loads":["g"]},` + + `{"name":"state","kind":"package","language":"go","paths":["state"],"series":"0.1"}]}}` + +// ptpIndexer answers the fixture's go.mod files and, for a path asked from the root, the object objects names for it. +func ptpIndexer(t *testing.T, objects map[string]string) (*Indexer, *memStore, *[]map[string][]string) { + ix, mods, _ := manifestsIndexer(t) + ix.GoMod = func(_ context.Context, _, _, dir string) ([]byte, error) { + if c, ok := ptpGoMods[dir]; ok { + return []byte(c), nil + } + return nil, nil + } + var asked []map[string][]string + ix.Named = func(_ context.Context, _, _ string, paths map[string][]string) (map[string]map[string]string, error) { + asked = append(asked, paths) + out := map[string]map[string]string{} + for dir, ps := range paths { + out[dir] = map[string]string{} + for _, p := range ps { + out[dir][p] = objects[p] + } + } + return out, nil + } + return ix, mods, &asked +} + +// Design 47 H-A-202, H-A-172: the same-repository modules a Go build resolves from the tree, transitively, join the tree +// input of every form with their objects, so a change of one moves the fingerprint of a module whose program names no +// paths; the entry's paths stay the manifest's. +func TestADerivedDirectoryMovesTheFingerprintOfEveryForm(t *testing.T) { + objects := map[string]string{"modules/b/builds": strings.Repeat("b", 40), "modules/c/client": strings.Repeat("c", 40)} + tree := func(objects map[string]string) string { + ix, mods, asked := ptpIndexer(t, objects) + f := withManifests(mergedMsg("novox/mesh-delivery", map[string]string{"modules/g": "gtree"}), map[string]string{"modules/g": ptpProgram}, nil) + if err := ix.Apply(context.Background(), f); err != nil { + t.Fatal(err) + } + m, _ := moduleOf(t, mods, "g") + if got := strings.Join(m.Paths["state"], ","); got != "state" { + t.Errorf("the entry's paths of state are %q, want the manifest's", got) + } + if len(*asked) != 1 || strings.Join((*asked)[0][""], ",") != "modules/b/builds,modules/c/client" { + t.Fatalf("asked the forge %v, want the derived directories from the root once", *asked) + } + if m.Inputs2 == nil || !strings.HasPrefix(m.Inputs2.Tree, "gtree\n") { + t.Fatalf("inputs = %+v (%s), want the directory's tree with the derived lines", m.Inputs2, m.None2) + } + return m.Inputs2.Tree + } + before := tree(objects) + if want := "gtree\nderived modules/b/builds " + objects["modules/b/builds"] + "\nderived modules/c/client " + objects["modules/c/client"]; before != want { + t.Errorf("the tree input is %q, want %q", before, want) + } + objects["modules/c/client"] = strings.Repeat("d", 40) + after := tree(objects) + fp := func(tree string) string { + v, _ := fingerprint.Of(fingerprint.Inputs{Module: "g", Tree: tree}) + return v + } + if fp(before) == fp(after) { + t.Errorf("a change of modules/c/client left the fingerprint %s", fp(after)) + } + // The named tree takes them too: a module whose every artifact names its paths. + named := `{"module":"g","version":"1.0","build":{"artifacts":[{"name":"state","kind":"package","language":"go","paths":["state"],"series":"0.1"}]}}` + ix, mods, _ := ptpIndexer(t, objects) + f := withManifests(mergedMsg("novox/mesh-delivery", map[string]string{"modules/g": "gtree"}), map[string]string{"modules/g": named}, nil) + f.Paths = map[string]map[string]string{"modules/g": {"state": strings.Repeat("1", 40)}} + if err := ix.Apply(context.Background(), f); err != nil { + t.Fatal(err) + } + if m, _ := moduleOf(t, mods, "g"); m.Inputs2 == nil || !strings.HasPrefix(m.Inputs2.Tree, fingerprint.NamedTreePrefix) || + !strings.HasSuffix(m.Inputs2.Tree, "\nderived modules/c/client "+objects["modules/c/client"]) || strings.Join(m.Paths["state"], ",") != "state" { + t.Errorf("module = %+v, want the named tree with the derived lines and the manifest's paths", m) + } + // A forge that names no object of a derived directory gives the module none, naming it. + ix, mods, _ = ptpIndexer(t, map[string]string{"modules/b/builds": strings.Repeat("b", 40)}) + if err := ix.Apply(context.Background(), withManifests(mergedMsg("novox/mesh-delivery", map[string]string{"modules/g": "gtree"}), + map[string]string{"modules/g": ptpProgram}, nil)); err != nil { + t.Fatal(err) + } + if m, _ := moduleOf(t, mods, "g"); m.Fp2 != "" || !strings.Contains(m.None2, "modules/c/client") { + t.Errorf("module = %+v, want none naming the directory the forge gave no object of", m) + } +} + +// Design 47 H-A-201: the entry pins every mesh package of another repository the module's build requires, across the +// same-repository modules of its graph: here the one modules/c/client requires, which modules/g's go.mod never names. +// A module of the module's own repository is never pinned, whatever a build reported (H-A-198, H-A-202). +func TestTheEntryPinsEveryRequirementOfTheModuleGraph(t *testing.T) { + const conditions = "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" + objects := map[string]string{"modules/b/builds": strings.Repeat("b", 40), "modules/c/client": strings.Repeat("c", 40)} + ix, mods, _ := ptpIndexer(t, objects) + mod := map[string]string{ + "modules/g": "module g\n\nrequire git.novox.be/novox/mesh-delivery/modules/b/builds" + ptpZero + "\n", + "modules/c/client": "module git.novox.be/novox/mesh-delivery/modules/c/client\n\nrequire " + conditions + ptpZero + "\n", + } + ix.GoMod = func(_ context.Context, _, _, dir string) ([]byte, error) { + if c, ok := mod[dir]; ok { + return []byte(c), nil + } + return []byte(ptpGoMods[dir]), nil + } + f := withManifests(mergedMsg("novox/mesh-delivery", map[string]string{"modules/g": "gtree"}), map[string]string{"modules/g": ptpProgram}, + map[string]map[string]string{"modules/g": {"go.sum": avahiGoSum}}) + if err := ix.Apply(context.Background(), f); err != nil { + t.Fatal(err) + } + m, _ := moduleOf(t, mods, "g") + if m.Inputs2 == nil || strings.Join(m.Inputs2.Packages, ",") != conditions+",git.novox.be/novox/mesh-sdk/go" { + t.Errorf("packages = %+v, want the client's requirement and state's mesh-sdk, and no module of the repository", m.Inputs2) + } + builds := "git.novox.be/novox/mesh-delivery/modules/b/builds" + pins := releasedPins(sdkstep.PackageVersions{builds: "v0.1.106", conditions: "v0.1.4"}, nil, catalogue.Manifest{}, "novox/mesh-delivery") + if _, pinned := pins[builds]; pinned || len(pins) != 1 { + t.Errorf("pins = %v, want %s only, never a module of the repository", pins, conditions) + } +} diff --git a/modules/mesh-delivery-indexer/internal/index/toolchains.go b/modules/mesh-delivery-indexer/internal/index/toolchains.go index c6a4a53a..f8c1eb67 100644 --- a/modules/mesh-delivery-indexer/internal/index/toolchains.go +++ b/modules/mesh-delivery-indexer/internal/index/toolchains.go @@ -298,7 +298,7 @@ func (ix *Indexer) recomputeLocked(ctx context.Context, overlay map[string]meshc r.fp, r.none = f.fp, f.none if r.fp != "" { // a pin is a released version, never a commit (H-A-161), and the version line's fingerprint takes it (H-A-167) - r.fp, r.pkgs = pinnedOf(f, series, k.manifest) + r.fp, r.pkgs = pinnedOf(f, series, k.manifest, k.cur.Repository) r.tcs = toolchainsUsed(toolchains) } } @@ -517,7 +517,7 @@ func stillComputed(module string, kept meshcatalog.ModulesByModuleValueInputs2, if f.none != "" || f.fp == "" { return "", nil, false } - fp, pkgs := pinnedOf(f, series, k.manifest) + fp, pkgs := pinnedOf(f, series, k.manifest, k.held.Repository) return fp, pkgs, fp == k.held.Fp2 } diff --git a/modules/mesh-delivery-indexer/internal/index/versions.go b/modules/mesh-delivery-indexer/internal/index/versions.go index f199ff14..9500d302 100644 --- a/modules/mesh-delivery-indexer/internal/index/versions.go +++ b/modules/mesh-delivery-indexer/internal/index/versions.go @@ -15,6 +15,7 @@ import ( "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/keys" @@ -373,13 +374,17 @@ func isFloor(raw []byte) bool { // within the series the module accepts, the one its manifest m names or the default (Manifest.AcceptedSeries), whether // or not that series holds contents yet; a publish in another series pins nothing and moves nothing, and a module's // release number is never a pin. A package with none published in that series is kept with no version, so its kind-1 -// edge stands (H §746) and its build waits for the publish (H-A-165). nil for none. -func releasedPins(versions sdkstep.PackageVersions, series map[string]SeriesPackage, m catalogue.Manifest) sdkstep.PackageVersions { +// edge stands (H §746) and its build waits for the publish (H-A-165). A module of the module's own repository is never +// pinned, whatever a build reported: it is built from the tree (design 47 H-A-198, H-A-201, H-A-202). nil for none. +func releasedPins(versions sdkstep.PackageVersions, series map[string]SeriesPackage, m catalogue.Manifest, repository string) sdkstep.PackageVersions { if len(versions) == 0 { return nil } out := sdkstep.PackageVersions{} for p := range versions { + if repo, _, ok := trunk.PackageRepository(p); ok && repository != "" && trunk.SameRepository(repo, repository) { + continue + } out[p] = "" for _, s := range series { if s.GoModule != p || s.Newest == nil || s.Series != m.AcceptedSeries(s.Name) { @@ -398,8 +403,8 @@ func releasedPins(versions sdkstep.PackageVersions, series map[string]SeriesPack // form's fingerprint takes them (design 47 H-A-167, H-A-172; ADR 0573 §4) and a re-pin moves it, the module a mover // built again against the release; a fingerprint with no pin is the form's as it is. The held fingerprint of a build that // stands is kept while its pins are the ones it was built with, else its import form takes the new pins. -func pinnedOf(f form, series map[string]SeriesPackage, m catalogue.Manifest) (string, sdkstep.PackageVersions) { - pins := releasedPins(f.versions, series, m) +func pinnedOf(f form, series map[string]SeriesPackage, m catalogue.Manifest, repository string) (string, sdkstep.PackageVersions) { + pins := releasedPins(f.versions, series, m, repository) if f.stands { if maps.Equal(f.built, map[string]string(pins)) { return f.fp, pins diff --git a/modules/mesh-delivery-sdk/buildstep/request.go b/modules/mesh-delivery-sdk/buildstep/request.go index 5ea272b0..e56ddb19 100644 --- a/modules/mesh-delivery-sdk/buildstep/request.go +++ b/modules/mesh-delivery-sdk/buildstep/request.go @@ -146,9 +146,15 @@ func (r FetchRequest) Check() error { if r.Dir != "" && (outside(r.Dir) || strings.HasPrefix(r.Dir, "/")) { return fmt.Errorf("the module directory %q leaves the repository", r.Dir) } + // A path is relative to the module's directory; one that leaves it but stays inside the repository (a sibling the + // artifact's go.mod requires, "..//") is accepted and narrowed from the repository's root, and one that + // leaves the repository is refused (design 47 H-A-198, H-A-199). for _, p := range r.Paths { - if p == "" || strings.HasPrefix(p, "/") || outside(p) || path.Clean(p) == "." { - return fmt.Errorf("the path %q leaves the module's directory or is not a path in it", p) + if p == "" || strings.HasPrefix(p, "/") || path.Clean(p) == "." { + return fmt.Errorf("the path %q is not a path relative to the module's directory", p) + } + if outside(path.Join(r.Dir, p)) { + return fmt.Errorf("the path %q leaves the repository", p) } } return nil diff --git a/modules/mesh-delivery-sdk/buildstep/request_test.go b/modules/mesh-delivery-sdk/buildstep/request_test.go index fbd79d55..c7360587 100644 --- a/modules/mesh-delivery-sdk/buildstep/request_test.go +++ b/modules/mesh-delivery-sdk/buildstep/request_test.go @@ -37,6 +37,11 @@ func TestAFetchRequestCarriesTheNamedPathsThroughTheToolCall(t *testing.T) { if dirOnly, err := ParseFetchRequest(map[string]any{"repository": "novox/x", "commit": strings.Repeat("b", 40), "dir": "modules/a"}); err != nil || dirOnly.Dir != "modules/a" || dirOnly.Paths != nil { t.Fatalf("a directory with no paths: %+v, %v", dirOnly, err) } + // A sibling of the same repository is a path that leaves the module's directory, not the repository (H-A-199). + if sib, err := ParseFetchRequest(map[string]any{"repository": "novox/x", "commit": strings.Repeat("b", 40), "dir": "modules/a", + "paths": []any{"pkg", "../b/client"}}); err != nil || len(sib.Paths) != 2 { + t.Fatalf("a sibling path inside the repository: %+v, %v", sib, err) + } for _, c := range []struct { set map[string]any says string @@ -46,7 +51,8 @@ func TestAFetchRequestCarriesTheNamedPathsThroughTheToolCall(t *testing.T) { {map[string]any{"commit": "abc"}, "is not a full commit"}, {map[string]any{"base": "abc"}, "is not a trunk commit"}, {map[string]any{"paths": []any{"src"}}, "names no module directory"}, - {map[string]any{"dir": "modules/a", "paths": []any{"../b"}}, "leaves the module's directory"}, + {map[string]any{"dir": "modules/a", "paths": []any{"../../../b"}}, "leaves the repository"}, + {map[string]any{"dir": "modules/a", "paths": []any{"/b"}}, "not a path relative to the module's directory"}, {map[string]any{"dir": "../a", "paths": []any{"src"}}, "leaves the repository"}, {map[string]any{"dir": "modules/a", "paths": []any{3}}, "paths is a list of strings"}, {map[string]any{"packages": map[string]any{"git.novox.be/novox/mesh-sdk/go": "main"}}, "neither a full commit nor a Go semantic version"}, diff --git a/modules/mesh-delivery-sdk/steprun/narrow.go b/modules/mesh-delivery-sdk/steprun/narrow.go index 7ced0ec1..c5be833c 100644 --- a/modules/mesh-delivery-sdk/steprun/narrow.go +++ b/modules/mesh-delivery-sdk/steprun/narrow.go @@ -25,9 +25,11 @@ func Narrow(tree string, r sdkstep.FetchRequest) error { } keep := map[string]bool{".git": true} for _, p := range r.Paths { + // Kept by its path from the repository's root: a path leaving the module's directory but not the repository is + // narrowed from the root like any other (design 47 H-A-199). rel := path.Join(r.Dir, p) if strings.HasPrefix(rel, "../") || rel == ".." || path.IsAbs(p) { - return fmt.Errorf("the named path %q leaves the module's directory", p) + return fmt.Errorf("the named path %q leaves the repository", p) } if _, err := os.Lstat(filepath.Join(tree, filepath.FromSlash(rel))); err != nil { return fmt.Errorf("the named path %q is not in the fetched tree: %w", p, err) diff --git a/modules/mesh-delivery-sdk/steprun/narrow_test.go b/modules/mesh-delivery-sdk/steprun/narrow_test.go index d27352ba..e62dd6de 100644 --- a/modules/mesh-delivery-sdk/steprun/narrow_test.go +++ b/modules/mesh-delivery-sdk/steprun/narrow_test.go @@ -68,6 +68,25 @@ func TestNarrowKeepsAModuleDirectorysPathsAndFailsOnAnAbsentOne(t *testing.T) { } } +// A path leaving the module's directory but not the repository is kept from the root; one leaving the repository is +// refused, naming the repository (design 47 H-A-198, H-A-199). +func TestNarrowKeepsASiblingOfTheRepositoryAndRefusesOneOutsideIt(t *testing.T) { + tree := t.TempDir() + for _, f := range []string{"modules/a/module.json", "modules/a/pkg/go.mod", "modules/a/notes.md", "modules/b/client/go.mod", + "modules/b/other.go", "root.txt"} { + writeTreeFile(t, tree, f, "x") + } + if err := Narrow(tree, sdkstep.FetchRequest{Dir: "modules/a", Paths: []string{"../../../x"}}); err == nil || !strings.Contains(err.Error(), "leaves the repository") { + t.Fatalf("a path leaving the repository gave %v", err) + } + if err := Narrow(tree, sdkstep.FetchRequest{Dir: "modules/a", Paths: []string{"pkg", "../b/client"}}); err != nil { + t.Fatal(err) + } + if got := strings.Join(filesUnder(t, tree), ","); got != "modules/a/module.json,modules/a/pkg/go.mod,modules/b/client/go.mod" { + t.Fatalf("the narrowed tree holds %s", got) + } +} + // A build that needs package.json or go.mod names it among its paths, and so keys on it (ADR 0499 clause 4). func TestNarrowKeepsAManifestOfTheLanguageOnlyWhenNamed(t *testing.T) { tree := t.TempDir()