mesh-build-go: the imports step lists each package path in the environment its build uses (issue 1984, H-A-633)

The imports step ran every `go list -deps` with the environment of the module's directory, while the build of a
package path runs with the environment of the path's own directory: its temporary modfile of the fetch's pins and the
replace to a module of the module's own repository. A path whose Go module requires such a module at the zero version
(mesh-delivery-contracts' clients require mesh-delivery-sdk) built and published, then failed its listing with "module
lookup disabled by GOPROXY=off" (main.510). ModuleImports now takes the environment as a function of the directory a
listing runs in, and the step passes buildEnv, so each listing reads the import set as the build resolved it.

The row is a subtest of TestImportsStepReadsTheWorkspace: a path's go.mod requires a same-repository module at the zero
version, resolved only through the path's temporary modfile; it fails with the environment of the module directory.

Builds: design 47 H2 H-A-633 (amends H-A-518, H-A-541, H-A-532 DQ-5); issue 1984
This commit is contained in:
2026-11-24 00:19:56 +01:00
parent 51a6ac3461
commit 5526873138
2 changed files with 72 additions and 6 deletions
@@ -67,13 +67,15 @@ func (m *Module) imports(ctx context.Context, args map[string]any) (any, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return ModuleImports(ctx, run, m.buildEnv(dir), dir, manifest) return ModuleImports(ctx, run, m.buildEnv, dir, manifest)
} }
// ModuleImports are the packages the Go bundle and package artifacts of manifest, in the module directory dir, import: // ModuleImports are the packages the Go bundle and package artifacts of manifest, in the module directory dir, import:
// one sorted union of import paths (design 47 H2 H-A-518, H-A-541), the standard library, the tests and the module's // one sorted union of import paths (design 47 H2 H-A-518, H-A-541), the standard library, the tests and the module's
// own left out. // own left out. Each listing runs with env of the directory it runs in, the environment the build gives that directory
func ModuleImports(ctx context.Context, run forgekey.Runner, env []string, dir string, manifest catalogue.Manifest) (ImportsResult, error) { // (its own module file of the fetch's pins, and the tree a module of the module's own repository is built from; design
// 47 H2 H-A-633), never one environment for them all.
func ModuleImports(ctx context.Context, run forgekey.Runner, env func(dir string) []string, dir string, manifest catalogue.Manifest) (ImportsResult, error) {
seen := map[string]bool{} seen := map[string]bool{}
if manifest.Build != nil { if manifest.Build != nil {
for _, a := range manifest.Build.Artifacts { for _, a := range manifest.Build.Artifacts {
@@ -96,7 +98,7 @@ func ModuleImports(ctx context.Context, run forgekey.Runner, env []string, dir s
} }
} }
for _, l := range lists { for _, l := range lists {
r := run(ctx, env, "go", "-C", l[0], "list", "-deps", "-f", listFormat, l[1]) r := run(ctx, env(l[0]), "go", "-C", l[0], "list", "-deps", "-f", listFormat, l[1])
if r.Status != 0 || r.Err != "" { 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)) return ImportsResult{}, fmt.Errorf("go list of %s: %s", a.Name, causeLine(r.Err+"\n"+r.Stderr+"\n"+r.Stdout))
} }
@@ -12,6 +12,9 @@ import (
"git.novox.be/novox/mesh-sdk/go/forgekey" "git.novox.be/novox/mesh-sdk/go/forgekey"
) )
// noEnv is the environment of a listing a test's fake compiler reads nothing from.
func noEnv(string) []string { return nil }
// ADR 0593 §2 (C3), design 47 H2 H-A-517, H-A-539, H-A-541: a fixture whose program imports packages of other Go // ADR 0593 §2 (C3), design 47 H2 H-A-517, H-A-539, H-A-541: a fixture whose program imports packages of other Go
// modules reports the import path of each package, whatever its host (a mesh module comes from the seat // modules reports the import path of each package, whatever its host (a mesh module comes from the seat
// go-package-registry, never told apart by a forge prefix): a sibling module's package and an outside package count, // go-package-registry, never told apart by a forge prefix): a sibling module's package and an outside package count,
@@ -36,7 +39,7 @@ func TestModuleImportsAreTheModulesItsBuildResolved(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
got, err := ModuleImports(context.Background(), run, nil, dir, m) got, err := ModuleImports(context.Background(), run, noEnv, dir, m)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -63,6 +66,67 @@ func TestImportsStepReadsTheWorkspace(t *testing.T) {
if got := out.(ImportsResult).Imports; len(got) != 0 { if got := out.(ImportsResult).Imports; len(got) != 0 {
t.Errorf("a fake compiler's go list names no package, and the answer was %v", got) t.Errorf("a fake compiler's go list names no package, and the answer was %v", got)
} }
// Issue 1984 (design 47 H2 H-A-633): each listing runs with the environment the build gives its directory. A
// package path's Go module requires a module of its own repository at the zero version, which only the path's own
// temporary modfile of the fetch (its replace to the tree) resolves; the module directory's environment names no
// such modfile, and Go's lookup is shut off (GOPROXY=off), as main.510 met on mesh-delivery-contracts' paths.
t.Run("a path resolved only through its own module file", func(t *testing.T) {
const sdk = "git.novox.be/novox/mesh-delivery/modules/demo-sdk"
pkg := `{"name":"clients","kind":"package","language":"go","paths":["go/clients/registry"],"series":"0.1"}`
m, _, _ := buildModule(t, manifestWith(pkg), "x")
tree := filepath.Join(m.Work, "ws1", "tree", "modules")
path, own := filepath.Join(tree, "demo", "go", "clients", "registry"), filepath.Join(tree, "demo-sdk")
for dir, body := range map[string]string{
path: "module git.novox.be/novox/mesh-delivery/modules/demo/go/clients/registry\n\nrequire " + sdk + " " + zeroVersion + "\n",
own: "module " + sdk + "\n",
} {
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
// What the fetch does for a same-repository requirement: a modfile of the path's directory replacing the module.
if err := m.replaceInModfile(path, sdk, own); err != nil {
t.Fatal(err)
}
// Go, as far as the listing needs it: the module file it reads is the one -modfile names, else the directory's
// own go.mod, and a required module no replace of that file points at is looked up, which GOPROXY=off refuses.
m.Run = throughBubblewrap(t, func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
modfile := filepath.Join(args[1], "go.mod")
for _, e := range env {
if flags, ok := strings.CutPrefix(e, "GOFLAGS="); ok {
for _, f := range strings.Fields(flags) {
if file, ok := strings.CutPrefix(f, "-modfile="); ok {
modfile = file
}
}
}
}
body, err := os.ReadFile(modfile)
if err != nil {
return forgekey.Ran{Status: 1, Stderr: err.Error()}
}
if !strings.Contains(string(body), "replace "+sdk+" => "+own) {
return forgekey.Ran{Status: 1, Stderr: "go: " + sdk + "@" + zeroVersion + ": module lookup disabled by GOPROXY=off"}
}
return forgekey.Ran{Stdout: strings.Join([]string{
"git.novox.be/novox/mesh-delivery/modules/demo/go/clients/registry " + path + " git.novox.be/novox/mesh-delivery/modules/demo/go/clients/registry true",
sdk + "/buildstep " + own + "/buildstep " + sdk + " false",
"git.novox.be/novox/mesh-sdk/go/stdio /cache/mesh-sdk@v1/go/stdio git.novox.be/novox/mesh-sdk/go false",
}, "\n")}
}, &[]shutOff{})
out, err := m.imports(context.Background(), map[string]any{"workspace": "ws1", "module": "modules/demo"})
if err != nil {
t.Fatal(err)
}
want := []string{sdk + "/buildstep", "git.novox.be/novox/mesh-sdk/go/stdio"}
if got := out.(ImportsResult).Imports; !slices.Equal(got, want) {
t.Errorf("imports %v, want %v: the sibling module's imports as the path's own module file resolves them", got, want)
}
})
} }
// G2 (design 47 H2 H-A-532 DQ-5, H-A-518, H-A-541): a Go package artifact reports the packages the packages its paths // G2 (design 47 H2 H-A-532 DQ-5, H-A-518, H-A-541): a Go package artifact reports the packages the packages its paths
@@ -105,7 +169,7 @@ func TestAPackageArtifactReportsItsImportSet(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
got, err := ModuleImports(context.Background(), run, nil, dir, m) got, err := ModuleImports(context.Background(), run, noEnv, dir, m)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }