From b117e4ab55ec9294d3422a570f0f023ea0d25de2 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 23 Nov 2026 11:21:35 +0100 Subject: [PATCH] N1: the Node build module reads and says its import set; each build module judges by its own setting (issue 721) The step tool build_node_imports reads, after a build, esbuild's metafile of each TypeScript bundle's staged entrypoints and launchers and answers the mesh npm packages among its inputs and external imports, each by its package name once (@novox/mesh-sdk, never a sub-path); the ask's steps run it as Imports and say the set as imports-reported on node-build-node's subject of the node. The Node build module declares the setting import-scope (default @novox) and the Go build module import-prefix (default the forge's Go module prefix), each filled into the step runner's ask as ImportScope and ImportPrefix. Builds: design 47 H2 H-A-523, H-A-517, H-A-532 DQ-6, DMC-2, DMC-3, H-A-534, H-A-535 --- .../mesh-build-go/cmd/mesh-build-go/asks.go | 17 +- .../cmd/mesh-build-go/asks_say_test.go | 13 ++ .../cmd/mesh-build-go/asks_test.go | 5 + modules/mesh-build-go/module.json | 7 + .../cmd/mesh-build-node/asks.go | 18 +- .../cmd/mesh-build-node/asks_say_test.go | 53 +++++ .../cmd/mesh-build-node/asks_test.go | 5 + .../mesh-build-node/build_integration_test.go | 12 +- .../cmd/mesh-build-node/build_test.go | 62 ++++++ .../cmd/mesh-build-node/imports.go | 185 ++++++++++++++++++ .../cmd/mesh-build-node/tools.go | 2 +- modules/mesh-build-node/go.mod | 2 +- modules/mesh-build-node/module.json | 8 + 13 files changed, 384 insertions(+), 5 deletions(-) create mode 100644 modules/mesh-build-node/cmd/mesh-build-node/imports.go diff --git a/modules/mesh-build-go/cmd/mesh-build-go/asks.go b/modules/mesh-build-go/cmd/mesh-build-go/asks.go index 28170e413..876736730 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/asks.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/asks.go @@ -3,12 +3,14 @@ package main import ( "context" "path/filepath" + "strings" "time" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo" buildrun "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildrun" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" + trunk "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/busstate" ) @@ -34,7 +36,8 @@ func (m *Module) askSteps(steps []StepTool) steprun.AskSteps { } return steprun.AskSteps{ Fetch: byName["build_go_fetch"], Build: byName["build_go_build"], Digest: byName["build_go_digest"], Test: byName["build_go_test"], - Imports: byName[ImportsTool], + // Judged by the setting import-prefix, the forge's Go module prefix by default (design 47 H2 H-A-534, H-A-535). + Imports: byName[ImportsTool], ImportPrefix: importPrefix(), Compare: steprun.CompareStep(steprun.GeneratedRootGo, func(ws string) string { return filepath.Join(m.Work, ws, "tree") }), Toolchain: func(ctx context.Context) (sdkstep.ToolchainUsed, error) { installed, err := m.Probe(ctx) @@ -58,6 +61,18 @@ func (m *Module) askSteps(steps []StepTool) steprun.AskSteps { } } +// ImportPrefixSetting is the module's setting naming the Go module path prefix of the mesh's packages (design 47 H2 +// H-A-535). +const ImportPrefixSetting = "import-prefix" + +// importPrefix is the setting import-prefix, trunk.ForgeModulePrefix when it names none. +func importPrefix() string { + if v, ok := stdio.Setting(ImportPrefixSetting); ok && strings.TrimSpace(v) != "" { + return strings.TrimSpace(v) + } + return trunk.ForgeModulePrefix +} + // sayImports says a build's import set on this node's subject of the seat (design 47 H2 H-A-517, H-A-532 DQ-2), with // SayEvent; nil when the module says no event. func (m *Module) sayImports() func(seat.ImportsReported) error { diff --git a/modules/mesh-build-go/cmd/mesh-build-go/asks_say_test.go b/modules/mesh-build-go/cmd/mesh-build-go/asks_say_test.go index 465c142e3..da3c3fa2c 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/asks_say_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/asks_say_test.go @@ -3,11 +3,13 @@ package main import ( "context" "encoding/json" + "strings" "testing" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" + stdio "git.novox.be/novox/mesh-sdk/go" ) // Design 47 G §496, §602 (BG4, BN4, BI4): the module's puller says the seat's events on this node's subjects. @@ -42,6 +44,7 @@ func TestADoneBuildSaysItsImportSetOnTheNodesSubject(t *testing.T) { } var events []said m := moduleWith(newStates(), "go1.26.0") + m.Outputs = kitObjects{} // a refused build keeps its error text as an output object, never on a bus here m.SayEvent = func(subject string, body any, id string) error { r, ok := body.(seat.ImportsReported) if !ok { @@ -74,4 +77,14 @@ func TestADoneBuildSaysItsImportSetOnTheNodesSubject(t *testing.T) { len(e.body.Imports) != 2 || e.body.Imports[0] != imports[0] || e.body.Imports[1] != imports[1] { t.Errorf("said %+v", e.body) } + // Design 47 H2 H-A-534, H-A-535: the import set is judged by the module's setting import-prefix, so a prefix the + // imports lie outside refuses the build and says none. + events = nil + t.Setenv(stdio.SettingWord(ImportPrefixSetting), "example.org/other/") + if _, err := steprun.RunAsk(context.Background(), m.askSteps(steps), a); err == nil || !strings.Contains(err.Error(), "example.org/other/") { + t.Errorf("imports outside import-prefix: %v, want them refused", err) + } + if len(events) != 0 { + t.Errorf("a refused import set was said: %+v", events) + } } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/asks_test.go b/modules/mesh-build-go/cmd/mesh-build-go/asks_test.go index 5dcf5bfe7..c70c041f0 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/asks_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/asks_test.go @@ -13,6 +13,7 @@ import ( sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" + trunk "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/catalogue" ) @@ -195,6 +196,10 @@ func TestManifestClaimsTheGoBuildSeatAndReadsItsCapacity(t *testing.T) { if strings.Contains(doc.Config[steprun.BuildHoursSetting].Description, "=") { t.Errorf("the build-hours description names a node pair") } + // Design 47 H2 H-A-535: the Go build module declares import-prefix, the forge's Go module prefix by default. + if prefix := string(doc.Config[ImportPrefixSetting].Default); prefix != `"`+trunk.ForgeModulePrefix+`"` { + t.Errorf("the import-prefix default = %s, want %q", prefix, trunk.ForgeModulePrefix) + } var capacity float64 _ = json.Unmarshal(doc.Config[sdkstep.CapacitySetting].Default, &capacity) if capacity != 0 { diff --git a/modules/mesh-build-go/module.json b/modules/mesh-build-go/module.json index 2de4a290b..94820f615 100644 --- a/modules/mesh-build-go/module.json +++ b/modules/mesh-build-go/module.json @@ -103,6 +103,13 @@ "minimum": 1, "maximum": 100, "default": 50 + }, + "import-prefix": { + "type": "string", + "description": "The Go module path prefix of the mesh's packages, under which every entry of a Go build's import set lies (design 47 H2 H-A-534, H-A-535): the step runner refuses an import set naming a package outside it.", + "trusted": false, + "runtime": true, + "default": "git.novox.be/novox/" } }, "tools": [ diff --git a/modules/mesh-build-node/cmd/mesh-build-node/asks.go b/modules/mesh-build-node/cmd/mesh-build-node/asks.go index 402ffcce3..2b081b53a 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/asks.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/asks.go @@ -5,6 +5,7 @@ import ( "path/filepath" "time" + nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildnode" buildrun "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildrun" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" @@ -32,6 +33,9 @@ func (m *Module) askSteps(steps []StepTool) steprun.AskSteps { } return steprun.AskSteps{ Fetch: byName["build_node_fetch"], Build: byName["build_node_build"], Test: byName["build_node_test"], + // The mesh npm packages its bundles import, read after the build and said as imports-reported, judged in the + // mesh's npm scope (design 47 H2 H-A-523, H-A-532 DQ-6, H-A-534, H-A-535). + Imports: byName[ImportsTool], ImportScope: importScope(), Compare: steprun.CompareStep(steprun.GeneratedRootTS, func(ws string) string { return filepath.Join(m.Work, ws, "tree") }), Toolchain: func(ctx context.Context) (sdkstep.ToolchainUsed, error) { installed, err := m.Probe(ctx) @@ -47,10 +51,22 @@ func (m *Module) askSteps(steps []StepTool) steprun.AskSteps { Lint: m.lintStep, Node: m.Node, SayStep: m.sayStep(), - Outputs: m.outputs(), + // A build that read its imports says them as the seat's imports-reported on this node (design 47 H2 H-A-517, + // H-A-532 DQ-2, DMC-2), published as the puller's other events are (SayEvent; nil says none, a test's). + SayImports: m.sayImports(), + Outputs: m.outputs(), } } +// sayImports says a build's import set on this node's subject of the seat (design 47 H2 H-A-517, H-A-532 DQ-2), with +// SayEvent; nil when the module says no event. +func (m *Module) sayImports() func(nodebuildgo.ImportsReported) error { + if m.SayEvent == nil { + return nil + } + return steprun.SayImportsOn(sdkstep.AskNode, m.Node, m.SayEvent) +} + // outputs are the outputs objects a test step keeps its run's output in and the verb output reads (design 47 H2 §791, // §820): Outputs when a test sets it, else the module's own objects store on the bus. func (m *Module) outputs() steprun.OutputObjects { diff --git a/modules/mesh-build-node/cmd/mesh-build-node/asks_say_test.go b/modules/mesh-build-node/cmd/mesh-build-node/asks_say_test.go index 5869cb558..0984cea90 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/asks_say_test.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/asks_say_test.go @@ -1,10 +1,16 @@ package main import ( + "context" + "errors" + "slices" + "strings" "testing" + nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" + stdio "git.novox.be/novox/mesh-sdk/go" ) // Design 47 G §496, §602 (BG4, BN4, BI4): the module's puller says the seat's events on this node's subjects. @@ -28,3 +34,50 @@ func TestPullerSaysTheSeatsEventsOnItsNodesSubject(t *testing.T) { } } } + +// N1/AC1, N1/AC3 (design 47 H2 H-A-517, H-A-523, H-A-534): a done build of a TypeScript module says one imports-reported +// on the Node build seat's subject of the node, naming the mesh npm packages the imports step read; a failed build says +// none. +func TestADoneNodeBuildSaysItsImportSetAndAFailedOneNone(t *testing.T) { + var said []nodebuildgo.ImportsReported + var subjects []string + m := moduleWith(newStates(), "v26.10.0") + m.Outputs = kitObjects{} // a failed build keeps its error text as an output object, never on a bus here + m.SayEvent = func(subject string, body any, _ string) error { + subjects = append(subjects, subject) + said = append(said, body.(nodebuildgo.ImportsReported)) + return nil + } + steps := append(fakeSteps(new([]string)), StepTool{Name: ImportsTool, Run: func(context.Context, map[string]any) (any, error) { + return ImportsResult{Imports: []string{"@novox/mesh-sdk"}}, nil + }}) + a := nodeAsk(sdkstep.AskDelivery) + a.Build.Fp = "src2:0456" + if _, err := steprun.RunAsk(context.Background(), m.askSteps(steps), a); err != nil { + t.Fatal(err) + } + want := "mesh.seat.node-build-node.event.imports-reported.ace" + if len(said) != 1 || subjects[0] != want || said[0].Module != "gitea" || said[0].Fingerprint != "src2:0456" || + said[0].Commit != askCommit || !slices.Equal(said[0].Imports, []string{"@novox/mesh-sdk"}) { + t.Fatalf("said %v on %v, want one imports-reported of gitea naming @novox/mesh-sdk on %s", said, subjects, want) + } + // H-A-534, H-A-535: the import set is judged by the setting import-scope, so a scope it lies outside refuses it. + said = nil + t.Setenv(stdio.SettingWord(ImportScopeSetting), "@other") + if _, err := steprun.RunAsk(context.Background(), m.askSteps(steps), a); err == nil || !strings.Contains(err.Error(), "@other") { + t.Errorf("an import set outside import-scope: %v, want it refused", err) + } + t.Setenv(stdio.SettingWord(ImportScopeSetting), "") + said = nil + for i := range steps { + if steps[i].Name == "build_node_build" { + steps[i].Run = func(context.Context, map[string]any) (any, error) { return nil, errors.New("tsc failed") } + } + } + if _, err := steprun.RunAsk(context.Background(), m.askSteps(steps), a); err == nil { + t.Fatal("the failed build passed") + } + if len(said) != 0 { + t.Errorf("a failed build said %v", said) + } +} diff --git a/modules/mesh-build-node/cmd/mesh-build-node/asks_test.go b/modules/mesh-build-node/cmd/mesh-build-node/asks_test.go index f0794b26f..dc27d50da 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/asks_test.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/asks_test.go @@ -12,6 +12,7 @@ import ( sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" + trunk "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" stdio "git.novox.be/novox/mesh-sdk/go" ) @@ -115,6 +116,10 @@ func TestManifestTakesTheBuildCapacity(t *testing.T) { if strings.Contains(doc.Config[steprun.BuildHoursSetting].Description, "=") { t.Errorf("the build-hours description names a node pair") } + // Design 47 H2 H-A-535: the Node build module declares import-scope, the mesh's npm scope by default. + if scope := string(doc.Config[ImportScopeSetting].Default); scope != `"`+trunk.DefaultNpmScope+`"` { + t.Errorf("the import-scope default = %s, want %q", scope, trunk.DefaultNpmScope) + } var capacity float64 _ = json.Unmarshal(doc.Config[sdkstep.CapacitySetting].Default, &capacity) if capacity != 0 { diff --git a/modules/mesh-build-node/cmd/mesh-build-node/build_integration_test.go b/modules/mesh-build-node/cmd/mesh-build-node/build_integration_test.go index 44cb3a6d7..d8c0545ad 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/build_integration_test.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/build_integration_test.go @@ -89,7 +89,8 @@ var fetchedSDK = map[string]string{ // fixture pass the unit level; a package whose test imports its own build output (as mesh-sdk's do) passes the unit level, // the test step emitting it with the real tsc, shut off, before the test script runs; and the fixture TypeScript bundle // importing @novox/mesh-sdk, built twice by the real tsc and esbuild, has equal digests, its archive holding the bundled -// .js, the .serve.mjs launcher and package.json. The unit levels run in w3, so the two builds' workspaces stay alike. +// .js, the .serve.mjs launcher and package.json, and its imports step names the SDK. The unit levels run in w3, so the +// two builds' workspaces stay alike. func TestTheFixturePassesTheUnitLevelAndBuildsTwiceToOneDigest(t *testing.T) { m, work := realMachine(t) out, err := m.test(context.Background(), map[string]any{"workspace": "w3", "module": "modules/m", "level": "unit"}) @@ -130,6 +131,15 @@ func TestTheFixturePassesTheUnitLevelAndBuildsTwiceToOneDigest(t *testing.T) { if digests[0] != digests[1] { t.Errorf("digests differ: %v", digests) } + // N1/AC1 (design 47 H2 H-A-523, H-A-532 DQ-6): the real esbuild's metafile of the built bundle names the SDK it + // imports, by its package name. + out, err = m.imports(context.Background(), map[string]any{"workspace": "w1", "module": "modules/m"}) + if err != nil { + t.Fatal(err) + } + if got := out.(ImportsResult).Imports; !slices.Equal(got, []string{"@novox/mesh-sdk"}) { + t.Errorf("the bundle's imports = %v, want [@novox/mesh-sdk]", got) + } if links := symlinksUnder(t, work); len(links) != 0 { t.Errorf("symlinks in the workspaces: %v", links) } diff --git a/modules/mesh-build-node/cmd/mesh-build-node/build_test.go b/modules/mesh-build-node/cmd/mesh-build-node/build_test.go index ad25c87e2..2eac23065 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/build_test.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/build_test.go @@ -361,3 +361,65 @@ func TestTheBundleCarriesTheAsksVersionInItsPackageJSON(t *testing.T) { } } } + +// N1/AC1, N1/AC2 (design 47 H2 H-A-523, H-A-532 DQ-6): after a bundle's build, the imports step runs esbuild over the +// staged entrypoint and launcher with a metafile and answers the npm packages in the mesh's scope among its inputs and +// external imports, each by its package name once (the SDK's stdio sub-path is the SDK); a bundle reaching none answers +// none. +func TestTheImportsStepNamesTheMeshNpmPackagesABundleReaches(t *testing.T) { + for _, c := range []struct { + name string + metafile string + want []string + }{ + {"mesh packages", `{"inputs":{ + ".mesh-build/tools/tools/index.js":{"imports":[{"path":"../../node_modules/@novox/mesh-sdk/dist/stdio.js"},{"path":"@novox/mesh-other/x","external":true},{"path":"pg","external":true}]}, + "../../node_modules/@novox/mesh-sdk/dist/stdio.js":{"imports":[]}, + "../../node_modules/@novox/mesh-sdk/dist/index.js":{"imports":[]}, + "../../node_modules/nats/lib/index.js":{"imports":[]}}}`, []string{"@novox/mesh-other", "@novox/mesh-sdk"}}, + {"none", `{"inputs":{".mesh-build/tools/tools/index.js":{"imports":[{"path":"../../node_modules/nats/lib/index.js"}]}, + "../../node_modules/nats/lib/index.js":{"imports":[]}}}`, []string{}}, + } { + t.Run(c.name, func(t *testing.T) { + m, f, work := builtMachine(t, `,"external":["pg"]`) + bundled := f.effect + var metaRun []string + f.effect = func(cl call) { + bundled(cl) + for _, a := range cl.args { + if p, ok := strings.CutPrefix(a, "--metafile="); ok { + metaRun = cl.args + write(t, filepath.Join(cl.dir, p), c.metafile, 0o644) + } + } + } + if _, err := m.build(context.Background(), buildArgs()); err != nil { + t.Fatal(err) + } + if metaRun != nil { + t.Fatalf("the build itself ran esbuild with a metafile: %v", metaRun) + } + out, err := m.imports(context.Background(), map[string]any{"workspace": "w1", "module": "modules/m"}) + if err != nil { + t.Fatal(err) + } + if got := out.(ImportsResult).Imports; !slices.Equal(got, c.want) { + t.Errorf("imports %v, want %v", got, c.want) + } + last := f.calls[len(f.calls)-1] + if dir := filepath.Join(work, "w1", "stage-tools", "modules", "module"); last.dir != dir || + !slices.Contains(last.args, ".mesh-build/tools/tools/index.js") || !slices.Contains(last.args, ".mesh-build/tools/tools/index.serve.mjs") || + !slices.Contains(last.args, "--external:pg") || !slices.Contains(last.args, "--metafile=.mesh-build/tools.imports.json") { + t.Errorf("esbuild ran in %s as %v", last.dir, last.args) + } + }) + } +} + +// N1 (H-A-517): a workspace whose bundle was not built has no import set to read, and the step says so. +func TestTheImportsStepRefusesABundleNotBuilt(t *testing.T) { + m, _, _ := builtMachine(t, ``) + if _, err := m.imports(context.Background(), map[string]any{"workspace": "w1", "module": "modules/m"}); err == nil || !strings.Contains(err.Error(), "was not built") { + t.Errorf("imports of an unbuilt bundle: %v", err) + } +} diff --git a/modules/mesh-build-node/cmd/mesh-build-node/imports.go b/modules/mesh-build-node/cmd/mesh-build-node/imports.go new file mode 100644 index 000000000..50d002b7f --- /dev/null +++ b/modules/mesh-build-node/cmd/mesh-build-node/imports.go @@ -0,0 +1,185 @@ +package main + +// The imports step (design 47 H2 H-A-523, H-A-532 DQ-6, H-A-534): at a build's end the module says which mesh npm +// packages its TypeScript bundles import, so the indexer can take the fingerprint over those packages' content and no +// other. It is what esbuild's metafile names as each bundle's inputs, read from the bundle's compiled entrypoints and +// launchers as the build step staged them, and of those the packages in the mesh's npm scope (the setting +// import-scope), each by its package name once (@novox/mesh-sdk, never a sub-path of it). A bundle's inputs are what +// its entrypoints reach, so no test file is among them. + +import ( + "context" + "encoding/json" + "fmt" + "os" + "path/filepath" + "slices" + "strings" + + sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" + trunk "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// ImportsTool reads the mesh npm packages a module's bundles import (design 47 H2 H-A-532 DQ-6). +const ImportsTool = "build_node_imports" + +// ImportScopeSetting is the module's setting naming the npm scope of the mesh's packages (design 47 H2 H-A-535). +const ImportScopeSetting = "import-scope" + +// ImportsResult is what the imports step answers; a BuildAnswer carries Imports as it is. +type ImportsResult struct { + Imports []string `json:"imports"` +} + +// importScope is the setting import-scope, trunk.DefaultNpmScope when it names none. +func importScope() string { + if v, ok := stdio.Setting(ImportScopeSetting); ok && strings.TrimSpace(v) != "" { + return strings.TrimSpace(v) + } + return trunk.DefaultNpmScope +} + +func (m *Module) importsTool() StepTool { + return StepTool{ + Name: ImportsTool, + Description: "Read the mesh npm packages the TypeScript bundles of a module of a workspace import, after its build: " + + "esbuild's inputs of each bundle's compiled entrypoints and launchers, kept to the packages in the mesh's npm scope " + + "(the setting import-scope), each by its package name, sorted and each once. It is what a build says at its end " + + "for the fingerprint (design 47 H2 H-A-523). Writes only esbuild's scratch output in the workspace.", + Input: map[string]any{ + "type": "object", + "properties": map[string]any{ + "workspace": map[string]any{"type": "string", "description": "the workspace id build_node_fetch answered, its bundles built"}, + "module": map[string]any{"type": "string", "description": "the module's directory in the workspace, '.' when it is the root"}, + }, + "required": []string{"workspace", "module"}, + }, + Run: m.imports, + } +} + +func (m *Module) imports(ctx context.Context, args map[string]any) (any, error) { + workspace, _ := args["workspace"].(string) + module, _ := args["module"].(string) + root, dir, err := m.moduleDirOf(workspace, module) + if err != nil { + return nil, err + } + raw, err := os.ReadFile(filepath.Join(dir, "module.json")) + if err != nil { + return nil, fmt.Errorf("the module has no module.json: %w", err) + } + var manifest struct { + Build struct { + Artifacts []artifact `json:"artifacts"` + } `json:"build"` + } + if err := json.Unmarshal(raw, &manifest); err != nil { + return nil, fmt.Errorf("module.json is not JSON: %w", err) + } + scope := importScope() + seen := map[string]bool{} + for _, a := range manifest.Build.Artifacts { + if a.Kind != "bundle" || a.Language != "typescript" { + continue + } + names, err := m.bundleImports(ctx, root, a, scope) + if err != nil { + return nil, err + } + for _, n := range names { + seen[n] = true + } + } + out := []string{} + for n := range seen { + out = append(out, n) + } + slices.Sort(out) + return ImportsResult{Imports: out}, nil +} + +// bundleImports are the packages in scope that esbuild names as inputs of bundle a, read in the stage its build left +// (build.go: /stage-/modules/module), from the same entries and externals the build bundled. +func (m *Module) bundleImports(ctx context.Context, root string, a artifact, scope string) ([]string, error) { + dir := filepath.Join(root, "stage-"+a.Name, "modules", "module") + out := Out(a.Name) + if _, err := os.Stat(filepath.Join(dir, out)); err != nil { + return nil, fmt.Errorf("the bundle %s was not built in this workspace, so its imports cannot be read: %w", a.Name, err) + } + var entries []string + for _, e := range a.Entrypoints { + if strings.HasSuffix(e, ".js") { + entries = append(entries, out+"/"+e) + } + } + for _, l := range sortedValues(sdkstep.Launchers(a.Language, a.Entrypoints)) { + entries = append(entries, out+"/"+l) + } + if len(entries) == 0 { + return nil, nil + } + meta := out + ".imports.json" + args := append(append([]string{}, entries...), "--bundle", "--platform=node", "--format=esm", "--target=node22", + "--outbase="+out, "--outdir="+out+".imports", "--log-level=warning", "--metafile="+meta) + for _, x := range a.External { + args = append(args, "--external:"+x) + } + if why := ranFailed(m.runIn(ctx, root, false, dir, nil, m.esbuild(), args...)); why != "" { + return nil, fmt.Errorf("esbuild's inputs of %s: %s", a.Name, why) + } + raw, err := os.ReadFile(filepath.Join(dir, meta)) + if err != nil { + return nil, fmt.Errorf("esbuild wrote no metafile for %s: %w", a.Name, err) + } + return inScope(raw, scope) +} + +// inScope are the npm packages in scope an esbuild metafile names: each input under a node_modules directory and each +// external import, by its package name. +func inScope(metafile []byte, scope string) ([]string, error) { + var meta struct { + Inputs map[string]struct { + Imports []struct { + Path string `json:"path"` + External bool `json:"external"` + } `json:"imports"` + } `json:"inputs"` + } + if err := json.Unmarshal(metafile, &meta); err != nil { + return nil, fmt.Errorf("esbuild's metafile is not JSON: %w", err) + } + var out []string + keep := func(name string) { + if strings.HasPrefix(name, scope+"/") && !slices.Contains(out, name) { + out = append(out, name) + } + } + for input, in := range meta.Inputs { + filePath := filepath.ToSlash(input) + if i := strings.LastIndex(filePath, "node_modules/"); i >= 0 { + keep(packageName(filePath[i+len("node_modules/"):])) + } + for _, imp := range in.Imports { + if imp.External { + keep(packageName(imp.Path)) + } + } + } + slices.Sort(out) + return out, nil +} + +// packageName is the npm package a module specifier or a path under node_modules names: its first segment, or its +// first two for a scoped package (@novox/mesh-sdk/stdio is @novox/mesh-sdk). +func packageName(spec string) string { + parts := strings.SplitN(spec, "/", 3) + if strings.HasPrefix(spec, "@") { + if len(parts) < 2 { + return "" + } + return parts[0] + "/" + parts[1] + } + return parts[0] +} diff --git a/modules/mesh-build-node/cmd/mesh-build-node/tools.go b/modules/mesh-build-node/cmd/mesh-build-node/tools.go index ee0e9beaf..0233b633e 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/tools.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/tools.go @@ -25,7 +25,7 @@ type StepTool struct { // StepTools are the module's step tools. Each step's issue adds its own. func (m *Module) StepTools() []StepTool { - return []StepTool{m.fetchTool(), m.testTool(), m.buildTool()} + return []StepTool{m.fetchTool(), m.testTool(), m.buildTool(), m.importsTool()} } // Tools is every tool the module serves: its step tools behind the guard, its status and its health. diff --git a/modules/mesh-build-node/go.mod b/modules/mesh-build-node/go.mod index 48a161b27..73bdb12b6 100644 --- a/modules/mesh-build-node/go.mod +++ b/modules/mesh-build-node/go.mod @@ -5,6 +5,7 @@ go 1.26.0 require ( git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000 + git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildnode v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshhandacts v0.0.0-00010101000000-000000000000 @@ -101,7 +102,6 @@ require ( git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/ociartifactregistryclient v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/gopackageregistry v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/npmpackageregistry v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000 // indirect ) diff --git a/modules/mesh-build-node/module.json b/modules/mesh-build-node/module.json index d3b769eb2..44aa50ee1 100644 --- a/modules/mesh-build-node/module.json +++ b/modules/mesh-build-node/module.json @@ -72,12 +72,20 @@ "minimum": 1, "maximum": 100, "default": 50 + }, + "import-scope": { + "type": "string", + "description": "The npm scope of the mesh's packages (design 47 H2 H-A-534, H-A-535): a TypeScript build's import set names each package of it its bundles import, by its package name, and the step runner refuses an import set naming a package outside it.", + "trusted": false, + "runtime": true, + "default": "@novox" } }, "tools": [ "build_node_fetch", "build_node_test", "build_node_build", + "build_node_imports", "build_node_release", "build_node_step", "build_node_steps",