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 6b0725251..2ab8fff54 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go @@ -22,6 +22,8 @@ import ( "golang.org/x/mod/modfile" + stdio "git.novox.be/novox/mesh-sdk/go" + 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" @@ -108,6 +110,11 @@ func (m *Module) fetch(ctx context.Context, args map[string]any) (res any, err e if err != nil { return nil, err } + // A build is for one module, never a repository (design 47 G, A-447b): a fetch names its module's directory. + if cleanDir(req.Dir) == "" { + return nil, fmt.Errorf("the fetch of %s at %s names no module directory (dir): a fetch is for one module, never a whole repository; asked by %s", + req.Repository, req.Commit, askerOf(args)) + } siblings, err := siblingsArg(args["beside"]) if err != nil { return nil, err @@ -694,3 +701,11 @@ func scopedDirs(tree, dir string, all []string) ([]string, error) { } return out, nil } + +// askerOf names who asked a tool: the caller the bus proved, else "an unnamed asker". +func askerOf(args map[string]any) string { + if c := stdio.CallerOf(args); c != "" { + return c + } + return "an unnamed asker" +} diff --git a/modules/mesh-build-go/cmd/mesh-build-go/fetch_test.go b/modules/mesh-build-go/cmd/mesh-build-go/fetch_test.go index 75c8823c9..aeaa3e44f 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch_test.go @@ -61,6 +61,13 @@ func (f *fakeStep) run(ctx context.Context, env []string, name string, args ...s os.MkdirAll(filepath.Dir(filepath.Join(args[i+1], name)), 0o755) os.WriteFile(filepath.Join(args[i+1], name), []byte(text), 0o644) } + // Every go.mod the fake lists exists, since a fetch asked with a directory reads its go.mod (A-447). + for _, l := range strings.Fields(f.gomods) { + if _, err := os.Stat(filepath.Join(args[i+1], l)); err != nil && f.files[l] == "" { + os.MkdirAll(filepath.Dir(filepath.Join(args[i+1], l)), 0o755) + os.WriteFile(filepath.Join(args[i+1], l), []byte("module fake\n"), 0o644) + } + } for _, v := range f.vendors { os.MkdirAll(filepath.Join(args[i+1], v, "vendor"), 0o755) os.WriteFile(filepath.Join(args[i+1], v, "vendor", "modules.txt"), nil, 0o644) @@ -91,7 +98,24 @@ func fetchModule(t *testing.T, f *fakeStep) (*Module, *keptStates) { return m, states } +// fetchTool is the fetch tool as a build module is asked, always with its module's directory (A-447b): the root of the +// tree unless the args name one. rawFetchTool is the tool as it is. func fetchTool(t *testing.T, m *Module) func(map[string]any) (any, error) { + t.Helper() + raw := rawFetchTool(t, m) + return func(args map[string]any) (any, error) { + if _, ok := args["dir"]; !ok { + named := map[string]any{"dir": "."} + for k, v := range args { + named[k] = v + } + args = named + } + return raw(args) + } +} + +func rawFetchTool(t *testing.T, m *Module) func(map[string]any) (any, error) { t.Helper() for _, tool := range m.Tools(m.StepTools()) { if tool.Name == "build_go_fetch" { @@ -107,7 +131,9 @@ var fetchArgs = map[string]any{"repository": "novox/mesh-sdk", "commit": commitS // 1485/AC1: git is asked through FetchCommit with the ssh remote, and go mod download and verify run in each go.mod // directory (not in one that vendors) with GOTOOLCHAIN=local, GOFLAGS=-mod=mod and the module's cache paths. func TestFetchAsksGitForTheSshRemoteAndDownloadsEachModule(t *testing.T) { - f := &fakeStep{gomods: "go.mod\nsub/dir/go.mod\nvend/go.mod\n", vendors: []string{"vend"}} + // The root go.mod replaces the other two by directory: a fetch asked with a module's directory chooses what it replaces. + f := &fakeStep{gomods: "go.mod\nsub/dir/go.mod\nvend/go.mod\n", vendors: []string{"vend"}, + files: map[string]string{"go.mod": "module fake\nreplace a => ./sub/dir\nreplace b => ./vend\n"}} m, states := fetchModule(t, f) if _, err := fetchTool(t, m)(fetchArgs); err != nil { t.Fatal(err) @@ -270,7 +296,8 @@ func TestAVerifyThatFailsEndsTheStepNamingTheDirectoryAndTheLine(t *testing.T) { return forgekey.Ran{} }} m, states := fetchModule(t, f) - if _, err := fetchTool(t, m)(fetchArgs); err != nil { + // Asked for the module of tools, as a build is for one module (A-447b). + if _, err := fetchTool(t, m)(map[string]any{"repository": "novox/mesh-sdk", "commit": commitSHA, "dir": "tools"}); err != nil { t.Fatal(err) } st := states.waitEnd(t) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go index 75782a9fa..1001dbb0a 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-sdk/go/forgekey" ) @@ -93,3 +94,23 @@ func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *test t.Errorf("cleanDir of nothing = %q, of . = %q", cleanDir(""), cleanDir(".")) } } + +// A-447b: a build is for one module, never a repository: a fetch with no module directory is refused, naming its asker +// and the repository, and nothing is fetched. +func TestAFetchWithNoDirectoryIsRefusedNamingItsAsker(t *testing.T) { + f := &fakeStep{gomods: "go.mod\n"} + m, states := fetchModule(t, f) + if _, err := rawFetchTool(t, m)(map[string]any{"repository": "novox/mesh-sdk", "commit": commitSHA}); err != nil { + t.Fatal(err) + } + st := states.waitEnd(t) + if st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, "novox/mesh-sdk") || !strings.Contains(st.Reason, "names no module directory") || + !strings.Contains(st.Reason, "asked by") { + t.Fatalf("a fetch with no dir ended %s: %q", st.State, st.Reason) + } + for _, l := range f.lines() { + if strings.HasPrefix(l, "git ") { + t.Errorf("a refused fetch ran %s", l) + } + } +} diff --git a/modules/mesh-build-go/cmd/mesh-build-go/mirror_test.go b/modules/mesh-build-go/cmd/mesh-build-go/mirror_test.go index af3f63d02..a71ca19f0 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/mirror_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/mirror_test.go @@ -176,6 +176,10 @@ func TestTheWorkspaceIsSelfContainedAfterAFetchThroughTheMirror(t *testing.T) { return strings.TrimSpace(string(out)) } git(src, "init", "-q") + // A fetch is for one module (A-447b): the repository holds a module at its root. + if err := os.WriteFile(filepath.Join(src, "go.mod"), []byte("module example.org/x\n\ngo 1.26\n"), 0o644); err != nil { + t.Fatal(err) + } if err := os.WriteFile(filepath.Join(src, "f.txt"), []byte("one\n"), 0o644); err != nil { t.Fatal(err) } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/narrow_integration_test.go b/modules/mesh-build-go/cmd/mesh-build-go/narrow_integration_test.go index 2ce32371e..e7ef09577 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/narrow_integration_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/narrow_integration_test.go @@ -45,7 +45,7 @@ func TestAFetchWithNamedPathsNarrowsTheWorkspace(t *testing.T) { if strings.Join(res.Modules, ",") != "lib" { t.Errorf("the fetch answered the modules %v, want lib alone", res.Modules) } - _, st = fetched(t, m, states, map[string]any{"repository": "novox/narrow", "commit": head, "paths": []any{"cmd"}}) + _, st = fetched(t, m, states, map[string]any{"repository": "novox/narrow", "commit": head, "dir": "", "paths": []any{"cmd"}}) if st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, "no module directory") { t.Errorf("paths without a directory: %s %q", st.State, st.Reason) } 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 17b5f20e8..5b957bc42 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 @@ -50,7 +50,15 @@ func newPinHarness(t *testing.T, gomod string, vendor bool, pulls string) (*pinH for _, g := range strings.Fields(f.gomods) { d := filepath.Join(args[i+1], filepath.Dir(g)) os.MkdirAll(d, 0o755) - os.WriteFile(filepath.Join(d, "go.mod"), []byte(gomod), 0o644) + body := gomod + if g == "go.mod" { // the root replaces the other directories, so a fetch asked with it chooses them (A-447) + for _, o := range strings.Fields(f.gomods) { + if o != "go.mod" { + body += "\nreplace local/" + filepath.Dir(o) + " => ./" + filepath.Dir(o) + "\n" + } + } + } + os.WriteFile(filepath.Join(d, "go.mod"), []byte(body), 0o644) os.WriteFile(filepath.Join(d, "go.sum"), []byte("sum\n"), 0o644) } if vendor { diff --git a/modules/mesh-build-go/cmd/mesh-build-go/run.go b/modules/mesh-build-go/cmd/mesh-build-go/run.go index cdd204725..24c9e50eb 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/run.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/run.go @@ -36,7 +36,7 @@ func (m *Module) run(ctx context.Context, fetch steprun.StepFunc, a sdkstep.Step return nil, err } key := sdkstep.RunKey(job.Program, sdkstep.ToolchainUsed{Version: installed, Recipe: m.Recipe.Version()}, a.Packages) - fetchArgs := sdkstep.FetchRequest{Repository: job.Program.Repository, Commit: job.Program.Commit, Packages: a.Packages}.Args() + fetchArgs := sdkstep.FetchRequest{Repository: job.Program.Repository, Commit: job.Program.Commit, Dir: job.Program.Dir, Packages: a.Packages}.Args() exe, built, err := m.programOf(ctx, fetch, "programs", "checking program", key, fetchArgs, job.Program.Dir, job.Program.Package) if err != nil { return nil, err diff --git a/modules/mesh-build-go/cmd/mesh-build-go/verify_once_test.go b/modules/mesh-build-go/cmd/mesh-build-go/verify_once_test.go index 3305e5cfd..cf7beee85 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/verify_once_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/verify_once_test.go @@ -53,7 +53,7 @@ func TestGoModVerifyRunsOncePerGoModAndGoSumDigest(t *testing.T) { // Without bubblewrap the module cache is not read-only to a step: a pass is never reused. func TestAPassOfVerifyIsNotReusedWhenStepsAreNotShutOff(t *testing.T) { - f := &fakeStep{gomods: "a/go.mod\nb/go.mod\n", files: map[string]string{"a/go.mod": "m\n", "b/go.mod": "m\n"}} + f := &fakeStep{gomods: "a/go.mod\nb/go.mod\n", files: map[string]string{"a/go.mod": "module m\n", "b/go.mod": "module m\n"}} m, states := fetchModule(t, f) // no Bubblewrap fetchOnce(t, m, states, fetchArgs) fetchOnce(t, m, states, fetchArgs) @@ -65,7 +65,7 @@ func TestAPassOfVerifyIsNotReusedWhenStepsAreNotShutOff(t *testing.T) { // A verify that fails leaves no mark: the next fetch verifies again. func TestAFailedVerifyLeavesNoMark(t *testing.T) { fail := true - f := &fakeStep{gomods: "go.mod\n", files: map[string]string{"go.mod": "m\n"}} + f := &fakeStep{gomods: "go.mod\n", files: map[string]string{"go.mod": "module m\n"}} f.goRule = func(line string) forgekey.Ran { if strings.HasSuffix(line, " mod verify") && fail { return forgekey.Ran{Status: 1, Stdout: "x v1: dir has been modified\n"} @@ -88,7 +88,7 @@ func TestDownloadsRunSideBySideUpToTheBound(t *testing.T) { running, most := 0, 0 release := make(chan struct{}) // closed once two run at once: they overlap however slowly the machine starts them f := &fakeStep{gomods: "a/go.mod\nb/go.mod\nc/go.mod\nd/go.mod\n", files: map[string]string{ - "a/go.mod": "a\n", "b/go.mod": "b\n", "c/go.mod": "c\n", "d/go.mod": "d\n"}} + "a/go.mod": "module a\n", "b/go.mod": "module b\n", "c/go.mod": "module c\n", "d/go.mod": "module d\n"}} f.goRule = func(line string) forgekey.Ran { if strings.HasSuffix(line, " mod download") { mu.Lock()