mesh-build-node: narrow a step's workspace as H-A-136 and H-A-137 say; a narrowed-out package is no module of the step #716

Merged
mesh-tools merged 2 commits from fix/47-H1-mesh-contracts-publish into main 2026-11-20 15:12:44 +00:00
6 changed files with 79 additions and 5 deletions
Showing only changes of commit 0b834bbca7 - Show all commits
@@ -48,6 +48,20 @@ type artifact struct {
// when that is a directory of the module holding a package.json (the contracts folders: ts/seats/<seat>/, each with its
// own lock, ADR 0634 section 8), else the module's directory itself (a package that names no path, or source paths
// beside the module's package.json: today's form).
// narrowedAway says whether the package's one named path is absent from the module's directory dir: in a workspace
// narrowed to another artifact's paths, it is not this build's.
func (a artifact) narrowedAway(dir string) bool {
if len(a.Paths) != 1 {
return false
}
p := path.Clean(strings.TrimSuffix(a.Paths[0], "/"))
if p == "." || path.IsAbs(p) || p == ".." || strings.HasPrefix(p, "../") {
return false
}
_, err := os.Lstat(filepath.Join(dir, filepath.FromSlash(p)))
return os.IsNotExist(err)
}
func (a artifact) packageDir(dir string) string {
if len(a.Paths) != 1 {
return "."
@@ -135,7 +135,7 @@ func (m *Module) testLevel(ctx context.Context, args map[string]any) (*TestResul
if where == "" {
where = "."
}
targets, err := testTargets(dir, where)
targets, err := testTargets(dir, where, narrowed(filepath.Join(root, "tree")))
if err != nil {
return nil, err
}
@@ -338,8 +338,9 @@ type testTarget struct{ dir, where string }
// testTargets are the directories a module's tests run in: each TypeScript package artifact's own path when every
// package the module declares names one (ADR 0634 §8: its package.json, its lock, its tests), else the module's
// directory.
func testTargets(dir, where string) ([]testTarget, error) {
// directory. In a workspace narrowed to one artifact's paths (narrowedTree), a package whose path was left out is no
// target of this step (ADR 0499 clause 4; design 47 H, H-A-136).
func testTargets(dir, where string, narrowedTree bool) ([]testTarget, error) {
whole := []testTarget{{dir, where}}
raw, err := os.ReadFile(filepath.Join(dir, "module.json"))
if err != nil {
@@ -362,6 +363,9 @@ func testTargets(dir, where string) ([]testTarget, error) {
if a.Kind != "package" {
return whole, nil // a bundle is tested in its module's directory
}
if narrowedTree && a.narrowedAway(dir) {
continue
}
at := a.packageDir(dir)
if at == "." {
return whole, nil
@@ -300,6 +300,11 @@ func (m *Module) typeScriptModules(ctx context.Context, tree string) (dirs []str
continue
}
d := filepath.ToSlash(filepath.Dir(l))
if a.Kind == "package" && narrowed(tree) && a.narrowedAway(filepath.Join(tree, filepath.FromSlash(d))) {
// A package of the module whose own path the narrowing left out (ADR 0499 clause 4; design 47 H, H-A-136:
// nothing else is fetched): no module of this build, never the module's directory, which holds no lock.
continue
}
if a.Kind == "package" {
d = path.Join(d, a.packageDir(filepath.Join(tree, filepath.FromSlash(d))))
}
@@ -22,7 +22,7 @@ func (m *Module) lintStep(ctx context.Context, args map[string]any) (any, error)
if err != nil {
return nil, err
}
targets, err := testTargets(mdir, filepath.ToSlash(filepath.Clean(dir)))
targets, err := testTargets(mdir, filepath.ToSlash(filepath.Clean(dir)), narrowed(filepath.Join(root, "tree")))
if err != nil {
return nil, err
}
@@ -91,3 +91,54 @@ func TestAFolderModulesPackageIsBuiltAndTestedInItsPath(t *testing.T) {
t.Errorf("unit level = %+v, ran tests in %v, want both package paths", r, tests)
}
}
// A step of one package of a root module with several (mesh-contracts' ts-meshbroker beside ts-meshrecovery, pipeline
// main.3 of 2026-11-20) fetches a workspace narrowed to that package's path (ADR 0499 clause 4; design 47 H, H-A-136:
// nothing else is fetched). The other packages' paths are not in it: they are no module of this build, never the
// module's directory, which holds no lock ("the package in . has no package-lock.json").
func TestANarrowedFetchOfOneFolderPackageLeavesTheOthersOut(t *testing.T) {
m, f := fetchMachine(t, folderFiles(""), nil)
args := map[string]any{"repository": "novox/mesh-contracts", "commit": commitSHA, "dir": ".", "paths": []any{"ts/seats/a"}}
out, err := m.fetch(context.Background(), args)
if err != nil {
t.Fatal(err)
}
r := out.(FetchResult)
if strings.Join(r.Modules, ",") != "ts/seats/a" || strings.Join(r.Installed, ",") != "ts/seats/a" {
t.Errorf("modules %v, installed %v; want only the named package ts/seats/a", r.Modules, r.Installed)
}
if n := len(npmCalls(f)); n != 1 {
t.Errorf("%d npm commands, want the one install of the named package", n)
}
}
// The lint and test levels of that narrowed workspace run in the named package's path alone: a package whose path was
// left out is no target, and the module's directory is not tested whole in its place (H-A-136).
func TestANarrowedWorkspaceTestsOnlyItsPackage(t *testing.T) {
work := t.TempDir()
files := folderFiles("")
for p := range files {
if strings.HasPrefix(p, "ts/seats/b/") {
delete(files, p)
}
}
for p, body := range files {
write(t, filepath.Join(work, filepath.FromSlash(p)), body, 0o644)
}
for _, tc := range []struct {
narrowed bool
want string
}{{true, "ts/seats/a"}, {false, "."}} {
got, err := testTargets(work, ".", tc.narrowed)
if err != nil {
t.Fatal(err)
}
var where []string
for _, g := range got {
where = append(where, g.where)
}
if strings.Join(where, ",") != tc.want {
t.Errorf("narrowed %v: targets %v, want %s", tc.narrowed, where, tc.want)
}
}
}
@@ -45,7 +45,7 @@ type StepsAnswer struct {
// detectSteps finds the steps of the package directories under dir.
func detectSteps(dir string) ([]DetectedStep, error) {
targets, err := testTargets(dir, ".")
targets, err := testTargets(dir, ".", false)
if err != nil {
return nil, err
}