diff --git a/modules/mesh-build-go/cmd/mesh-build-go/imports.go b/modules/mesh-build-go/cmd/mesh-build-go/imports.go index f7d7aa10b..994146a0c 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/imports.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/imports.go @@ -2,7 +2,8 @@ package main // The imports step (novox/hq ADR 0593 §2): at a build's end the module says which mesh packages the module's Go programs // import, so the indexer can take the fingerprint over those packages' content and no other. It is `go list -deps` of -// each Go bundle artifact's program, the tests left out, kept to the packages of the mesh's forge that lie outside the +// each Go bundle artifact's program and of the packages each Go package artifact's paths hold (design 47 H2 H-A-532 +// DQ-5), the tests left out, kept to the packages of the mesh's forge that lie outside the // module's own directory (a sibling package of the module's own repository counts, a package under the module itself // comes with its tree). @@ -28,7 +29,7 @@ func (m *Module) importsTool() StepTool { return StepTool{ Name: ImportsTool, Description: "Read the mesh packages the Go programs of a module of a fetched workspace import: `go list -deps` of each Go " + - "bundle artifact, without tests, kept to the forge's packages outside the module's own directory, sorted and each once. " + + "bundle artifact and of the packages each Go package artifact's paths hold, without tests, kept to the forge's packages outside the module's own directory, sorted and each once. " + "It is what a build says at its end for the fingerprint (ADR 0593 §2). Read-only.", Input: map[string]any{ "type": "object", @@ -68,24 +69,42 @@ func (m *Module) imports(ctx context.Context, args map[string]any) (any, error) return ModuleImports(ctx, run, m.buildEnv(dir), dir, manifest) } -// ModuleImports are the mesh packages the Go bundle artifacts of manifest, in the module directory dir, import. +// ModuleImports are the mesh packages the Go bundle and package artifacts of manifest, in the module directory dir, +// import: one sorted union (design 47 H2 H-A-518), the tests and the module's own directory left out. func ModuleImports(ctx context.Context, run forgekey.Runner, env []string, dir string, manifest catalogue.Manifest) (ImportsResult, error) { seen := map[string]bool{} if manifest.Build != nil { for _, a := range manifest.Build.Artifacts { - if a.Kind != "bundle" || a.Language != "go" || strings.TrimSpace(a.From) == "" { + if a.Language != "go" { continue } - r := run(ctx, env, "go", "-C", dir, "list", "-deps", "-f", "{{.ImportPath}} {{.Dir}}", "./"+strings.Trim(a.From, "./")) - if r.Status != 0 || r.Err != "" { - return ImportsResult{}, fmt.Errorf("go list of %s: %s", a.Name, causeLine(r.Err+"\n"+r.Stderr+"\n"+r.Stdout)) + // A bundle lists its program; a package (design 47 H2 H-A-532 DQ-5) lists every package each of its paths + // holds, in the Go module that path is, so a contracts module reports its import set too. + var lists [][2]string + switch a.Kind { + case "bundle": + if strings.TrimSpace(a.From) != "" { + lists = append(lists, [2]string{dir, "./" + strings.Trim(a.From, "./")}) + } + case "package": + for _, p := range a.Paths { + if strings.TrimSpace(p) != "" { + lists = append(lists, [2]string{filepath.Join(dir, filepath.FromSlash(strings.Trim(p, "./"))), "./..."}) + } + } } - for _, l := range strings.Split(r.Stdout, "\n") { - pkg, where, ok := strings.Cut(strings.TrimSpace(l), " ") - if !ok || !meshPackage(pkg) || ownedBy(dir, where) { - continue + for _, l := range lists { + r := run(ctx, env, "go", "-C", l[0], "list", "-deps", "-f", "{{.ImportPath}} {{.Dir}}", l[1]) + if r.Status != 0 || r.Err != "" { + return ImportsResult{}, fmt.Errorf("go list of %s: %s", a.Name, causeLine(r.Err+"\n"+r.Stderr+"\n"+r.Stdout)) + } + for _, line := range strings.Split(r.Stdout, "\n") { + pkg, where, ok := strings.Cut(strings.TrimSpace(line), " ") + if !ok || !meshPackage(pkg) || ownedBy(dir, where) { + continue + } + seen[pkg] = true } - seen[pkg] = true } } } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/imports_test.go b/modules/mesh-build-go/cmd/mesh-build-go/imports_test.go index 76227e405..f2dd57635 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/imports_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/imports_test.go @@ -61,3 +61,54 @@ func TestImportsStepReadsTheWorkspace(t *testing.T) { t.Errorf("a fake compiler's go list names no package, and the answer was %v", got) } } + +// G2 (design 47 H2 H-A-532 DQ-5, H-A-518): a Go package artifact reports the mesh packages the packages its paths +// hold import, listed in the Go module each path is, without tests and without the module's own directory; a module +// with both kinds reports one sorted union. +func TestAPackageArtifactReportsItsImportSet(t *testing.T) { + dir := "/w/ws1/tree/modules/contracts" + pkg := `{"name":"seat","kind":"package","language":"go","paths":["go/seats/demo"],"series":"0.1"}` + listed := map[string]string{ + dir + "/go/seats/demo ./...": strings.Join([]string{ + "encoding/json /usr/lib/go/src/encoding/json", + "git.novox.be/novox/mesh-catalog/modules/contracts/go/seats/demo " + dir + "/go/seats/demo", + "git.novox.be/novox/mesh-sdk/go/stdio /cache/mesh-sdk@v1/go/stdio", + }, "\n"), + dir + " ./cmd/demo-tools": "git.novox.be/novox/mesh-sdk/go/announce /cache/mesh-sdk@v1/go/announce", + } + for _, c := range []struct { + name string + artifacts string + want []string + }{ + // G2/AC1: a contracts module whose artifact is of kind package. + {"package", pkg, []string{"git.novox.be/novox/mesh-sdk/go/stdio"}}, + // G2/AC2: a module with a bundle and a package reports one sorted union. + {"both", goBundle + "," + pkg, []string{"git.novox.be/novox/mesh-sdk/go/announce", "git.novox.be/novox/mesh-sdk/go/stdio"}}, + } { + t.Run(c.name, func(t *testing.T) { + run := func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran { + line := args[1] + " " + args[len(args)-1] + if slices.Contains(args, "-test") || args[2] != "list" { + t.Errorf("go ran as %v: it lists dependencies without tests", args) + } + out, ok := listed[line] + if !ok { + t.Errorf("go list in %q, not asked", line) + } + return forgekey.Ran{Stdout: out} + } + m, err := catalogue.Decode([]byte(`{"module":"contracts","version":"1","build":{"artifacts":[` + c.artifacts + `]}}`)) + if err != nil { + t.Fatal(err) + } + got, err := ModuleImports(context.Background(), run, nil, dir, m) + if err != nil { + t.Fatal(err) + } + if !slices.Equal(got.Imports, c.want) { + t.Errorf("imports %v, want %v", got.Imports, c.want) + } + }) + } +}