mesh-build-node: place every mesh package a module names at its latest release (H-A-267)

A trunk build put only the SDK into a module's node_modules; any other
@novox/mesh-* package its package.json names was left out of the install
and only a group or a member's build brought it, so the forge module's
build failed with "Cannot find module" for its contracts package. Each
such package is now placed at the latest release the registry holds, as
the SDK is; a group's packed package or a member's build still takes
precedence.

Builds: design 47 H H-A-267
This commit is contained in:
2026-11-21 15:35:26 +01:00
parent efc8c52e68
commit 83802dc4a6
4 changed files with 121 additions and 60 deletions
@@ -245,6 +245,11 @@ func (m *Module) fetchInto(ctx context.Context, req sdkstep.FetchRequest, group
return nil, err
}
}
// Every other mesh package it names, at the latest release (design 47 H, H-A-267); the group's and the
// members' builds are placed over them.
if err := m.placeMeshPackages(ctx, dir, d, served, members); err != nil {
return nil, err
}
if err := placeGroup(dir, served); err != nil {
return nil, err
}
@@ -97,9 +97,17 @@ func TestAModuleNamingOnlyTheSdkAndTypesInstallsNothing(t *testing.T) {
}
// 47/G A-72: a module naming another @novox/mesh-* package beside the SDK has no package beyond the SDK, so it installs
// nothing and is not refused for want of a lock.
// nothing and is not refused for want of a lock. 47/H H-A-267 (ADR 0573 §2): a fetch with no group and no members' builds
// places that package at the latest release the registry holds, as it places the SDK, whatever the module's lock names.
func TestAMeshPackageIsNotBeyondTheSdk(t *testing.T) {
m, f := fetchMachine(t, typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6","@novox/mesh-tools":"^0.1.6"}}`), nil)
const contracts = "@novox/mesh-delivery-contracts-git"
files := typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6","` + contracts + `":"^0.1.2"}}`)
files["modules/m/package-lock.json"] = `{"lockfileVersion":3,"packages":{"node_modules/` + contracts + `":{"version":"0.1.2","integrity":"sha512-old"}}}`
m, f := fetchMachine(t, files, nil)
fakeRegistryOf(t, m, map[string]map[string][]byte{
sdkPackage: {"0.1.15": tgzOf(t, sdkPackage, "0.1.15")},
contracts: {"0.1.2": tgzOf(t, contracts, "0.1.2"), "0.1.4": tgzOf(t, contracts, "0.1.4"), "0.1.5-feat.1": tgzOf(t, contracts, "0.1.5-feat.1")},
})
out, err := m.fetch(context.Background(), fetchArgs)
if err != nil {
t.Fatal(err)
@@ -108,6 +116,14 @@ func TestAMeshPackageIsNotBeyondTheSdk(t *testing.T) {
if len(npmCalls(f)) != 0 || len(r.Installed) != 0 || len(r.Refused) != 0 {
t.Errorf("result = %+v, npm calls %d, want nothing installed and nothing refused", r, len(npmCalls(f)))
}
dir := filepath.Join(m.Work, r.Workspace, "tree", "modules", "m")
got, err := readPackageJSON(filepath.Join(dir, "node_modules", filepath.FromSlash(contracts)))
if err != nil || got.Version != "0.1.4" {
t.Errorf("%s placed %q (%v), want the latest release 0.1.4", contracts, got.Version, err)
}
if v := placedSDK(dir); v != "0.1.15" || r.SDK["modules/m"] != "0.1.15" {
t.Errorf("the SDK placed %q, answered %q, want the latest release 0.1.15", v, r.SDK["modules/m"])
}
}
// 1489/AC2: a module naming lodash with a lock gets npm ci --omit=dev --ignore-scripts (fake runner), its install put
@@ -364,9 +364,9 @@ func placeGroup(dir string, served map[string]*packed) error {
return nil
}
// sdkLocked is what a module's lock names of the SDK: its version, its integrity and where it was resolved from; ok is
// false when the lock names none (the latest release is then placed, placeSDK).
func sdkLocked(dir string) (version, integrity string, ok bool, err error) {
// locked is what a module's lock names of the package name: its version and its integrity; ok is false when the lock
// names none (the latest release is then placed, placeLatest).
func locked(dir, name string) (version, integrity string, ok bool, err error) {
raw, err := os.ReadFile(filepath.Join(dir, "package-lock.json"))
if os.IsNotExist(err) {
return "", "", false, nil
@@ -383,7 +383,7 @@ func sdkLocked(dir string) (version, integrity string, ok bool, err error) {
if err := json.Unmarshal(raw, &lock); err != nil {
return "", "", false, fmt.Errorf("the package-lock.json is not JSON: %w", err)
}
p, ok := lock.Packages["node_modules/"+sdkPackage]
p, ok := lock.Packages["node_modules/"+name]
if !ok || p.Version == "" {
return "", "", false, nil
}
@@ -398,51 +398,85 @@ func sdkLocked(dir string) (version, integrity string, ok bool, err error) {
// none. The lock's integrity is checked when the lock names the registry's version placed; a registry holding no
// release is a wait (`waits for:`).
func (m *Module) placeSDK(ctx context.Context, dir, rel string, group *packed) error {
version, integrity, _, err := sdkLocked(dir)
if group == nil {
return m.placeLatest(ctx, dir, rel, sdkPackage)
}
version, _, _, err := locked(dir, sdkPackage)
if err != nil {
return err
}
var tgz []byte
if group != nil {
if m.Say != nil && version != "" && version != group.Version {
m.Say("%s: its lock names %s@%s; the group's %s is placed (design 47 G A-284)", rel, sdkPackage, version, group.Version)
}
tgz = group.tgz
} else {
latest, err := m.registryLatest(ctx, sdkPackage)
if err != nil {
return err
}
if latest == "" {
return fmt.Errorf("%s%s", builds.WaitsFor, sdkPackage)
}
if version != latest {
if m.Say != nil && version != "" {
m.Say("%s: its lock names %s@%s; the latest published %s is placed (ADR 0573 §2)", rel, sdkPackage, version, latest)
}
version, integrity = latest, ""
}
want := sdkPackage + "@" + version
got, registered, held, err := m.registryPackageChecked(ctx, sdkPackage, version)
if err != nil {
return err
}
if !held {
return fmt.Errorf("%s%s", builds.WaitsFor, want)
}
if integrity == "" {
integrity = registered
}
if integrity != "" && integrityOf(got) != integrity {
return fmt.Errorf("the registry's %s is %s, and the lock of %s names %s", want, integrityOf(got), rel, integrity)
}
tgz = got
if m.Say != nil && version != "" && version != group.Version {
m.Say("%s: its lock names %s@%s; the group's %s is placed (design 47 G A-284)", rel, sdkPackage, version, group.Version)
}
into := filepath.Join(dir, "node_modules", filepath.FromSlash(sdkPackage))
if err := os.RemoveAll(into); err != nil {
return err
}
return unpackPackage(tgz, into)
return unpackPackage(group.tgz, into)
}
// placeMeshPackages places in the module's node_modules each mesh package beyond the SDK (@novox/mesh-*) its
// package.json names at the latest release the registry holds, as placeSDK does for the SDK (ADR 0573 §2; design 47 H,
// H-A-267): the install leaves them out (ownDependencies). One the group serves or a member's build lays is left to
// placeGroup and layBuilds, which take precedence; the module itself is not placed.
func (m *Module) placeMeshPackages(ctx context.Context, dir, rel string, served map[string]*packed, members []storedBuild) error {
self, _ := readPackageJSON(dir)
var names []string
for name := range namedPackages(dir) {
if name == sdkPackage || !strings.HasPrefix(name, "@novox/mesh-") || name == self.Name || served[name] != nil || namesPackage(members, name) {
continue
}
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
if err := m.placeLatest(ctx, dir, rel, name); err != nil {
return err
}
}
return nil
}
// placeLatest places in the module's node_modules the latest release of the mesh package name the registry holds,
// whatever the module's lock names (ADR 0573 §2), checked against the lock's integrity when the lock names that
// version; a registry holding no release is a wait (`waits for:`).
func (m *Module) placeLatest(ctx context.Context, dir, rel, name string) error {
version, integrity, _, err := locked(dir, name)
if err != nil {
return err
}
latest, err := m.registryLatest(ctx, name)
if err != nil {
return err
}
if latest == "" {
return fmt.Errorf("%s%s", builds.WaitsFor, name)
}
if version != latest {
if m.Say != nil && version != "" {
m.Say("%s: its lock names %s@%s; the latest published %s is placed (ADR 0573 §2)", rel, name, version, latest)
}
version, integrity = latest, ""
}
want := name + "@" + version
got, registered, held, err := m.registryPackageChecked(ctx, name, version)
if err != nil {
return err
}
if !held {
return fmt.Errorf("%s%s", builds.WaitsFor, want)
}
if integrity == "" {
integrity = registered
}
if integrity != "" && integrityOf(got) != integrity {
return fmt.Errorf("the registry's %s is %s, and the lock of %s names %s", want, integrityOf(got), rel, integrity)
}
into := filepath.Join(dir, "node_modules", filepath.FromSlash(name))
if err := os.RemoveAll(into); err != nil {
return err
}
return unpackPackage(got, into)
}
// placedSDK is the version of the SDK placed in the module's node_modules ("" when none is).
@@ -33,34 +33,40 @@ func tgzOf(t *testing.T, name, version string) []byte {
return buf.Bytes()
}
// fakeRegistry serves the packument and the packed files of the versions it holds; asked counts its answers.
// fakeRegistry serves the packument and the packed files of the SDK's versions it holds; asked counts its answers.
func fakeRegistry(t *testing.T, m *Module, held map[string][]byte) *atomic.Int32 {
t.Helper()
return fakeRegistryOf(t, m, map[string]map[string][]byte{sdkPackage: held})
}
// fakeRegistryOf serves the packument and the packed files of each package's versions it holds (by name); asked counts
// its answers.
func fakeRegistryOf(t *testing.T, m *Module, held map[string]map[string][]byte) *atomic.Int32 {
t.Helper()
var asked atomic.Int32
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
asked.Add(1)
switch {
case r.URL.Path == "/npm/@novox%2fmesh-sdk" || r.URL.Path == "/npm/@novox/mesh-sdk":
if len(held) == 0 {
http.NotFound(w, r)
return
}
versions := map[string]any{}
for v, tgz := range held {
versions[v] = map[string]any{"dist": map[string]any{"tarball": srv.URL + "/files/" + v + ".tgz", "integrity": integrityOf(tgz)}}
}
json.NewEncoder(w).Encode(map[string]any{"versions": versions})
case strings.HasPrefix(r.URL.Path, "/files/"):
v := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/files/"), ".tgz")
if tgz, ok := held[v]; ok {
if f, ok := strings.CutPrefix(r.URL.Path, "/files/"); ok {
name, v, _ := strings.Cut(strings.TrimSuffix(f, ".tgz"), "@@")
if tgz, ok := held[strings.ReplaceAll(name, "~", "/")][v]; ok {
w.Write(tgz)
return
}
http.NotFound(w, r)
default:
http.NotFound(w, r)
return
}
name, ok := strings.CutPrefix(r.URL.Path, "/npm/")
name = strings.ReplaceAll(name, "%2f", "/")
if !ok || len(held[name]) == 0 {
http.NotFound(w, r)
return
}
versions := map[string]any{}
for v, tgz := range held[name] {
versions[v] = map[string]any{"dist": map[string]any{"tarball": srv.URL + "/files/" + strings.ReplaceAll(name, "/", "~") + "@@" + v + ".tgz", "integrity": integrityOf(tgz)}}
}
json.NewEncoder(w).Encode(map[string]any{"versions": versions})
}))
t.Cleanup(srv.Close)
m.Toolchain.Registry = func() (Registry, error) { return Registry{URL: srv.URL + "/npm/"}, nil }