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 876736730..28170e413 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/asks.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/asks.go @@ -3,14 +3,12 @@ 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" ) @@ -36,8 +34,7 @@ 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"], - // 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(), + Imports: byName[ImportsTool], 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) @@ -61,18 +58,6 @@ 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 da3c3fa2c..465c142e3 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,13 +3,11 @@ 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. @@ -44,7 +42,6 @@ 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 { @@ -77,14 +74,4 @@ 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 c70c041f0..5ebd5d91c 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,7 +13,6 @@ 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" ) @@ -196,9 +195,9 @@ 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) + // Design 47 H2 H-A-539: no prefix tells a mesh package apart, so the module declares no setting import-prefix. + if _, ok := doc.Config["import-prefix"]; ok { + t.Errorf("the manifest declares import-prefix, which H-A-539 withdrew") } var capacity float64 _ = json.Unmarshal(doc.Config[sdkstep.CapacitySetting].Default, &capacity) 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 994146a0c..939bcea7a 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/imports.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/imports.go @@ -1,11 +1,11 @@ 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 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). +// The imports step (novox/hq ADR 0593 §2): at a build's end the module says which Go modules the module's Go programs +// import, so the indexer can take the fingerprint over the ones a module of the module graph publishes and no other. It +// is `go list -deps` of 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: the module path of every package but the standard library's, with no +// prefix telling a mesh module apart (H-A-539: mesh modules come from the seat go-package-registry), and none of the +// module's own (its main module, or a module that lies under its own directory, which comes with its tree). import ( "context" @@ -15,7 +15,6 @@ import ( "slices" "strings" - trunk "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/forgekey" ) @@ -28,8 +27,9 @@ type ImportsResult struct { 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 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. " + + Description: "Read the Go modules the Go programs of a module of a fetched workspace import: `go list -deps` of each Go " + + "bundle artifact and of the packages each Go package artifact's paths hold, without tests, the module path of every " + + "package but the standard library's and the module's own, 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", @@ -69,8 +69,9 @@ 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 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. +// ModuleImports are the Go modules the Go bundle and package artifacts of manifest, in the module directory dir, import: +// one sorted union of module paths (design 47 H2 H-A-518, H-A-539), the standard library, the tests and the module's +// own 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 { @@ -94,16 +95,17 @@ func ModuleImports(ctx context.Context, run forgekey.Runner, env []string, dir s } } for _, l := range lists { - r := run(ctx, env, "go", "-C", l[0], "list", "-deps", "-f", "{{.ImportPath}} {{.Dir}}", l[1]) + r := run(ctx, env, "go", "-C", l[0], "list", "-deps", "-f", listFormat, 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) { + //
; a package of the standard library has no module. + f := strings.Fields(line) + if len(f) != 4 || f[3] == "true" || ownedBy(dir, f[1]) { continue } - seen[pkg] = true + seen[f[2]] = true } } } @@ -113,16 +115,12 @@ func ModuleImports(ctx context.Context, run forgekey.Runner, env []string, dir s out = append(out, p) } slices.Sort(out) - if err := trunk.CheckImports(out); err != nil { - return ImportsResult{}, err - } return ImportsResult{Imports: out}, nil } -func meshPackage(pkg string) bool { - _, _, ok := trunk.PackageRepository(pkg) - return ok -} +// listFormat is what go list says of each package a program reaches: its import path, its directory and, outside the +// standard library, its module's path and whether that is the main module. +const listFormat = "{{.ImportPath}} {{.Dir}}{{with .Module}} {{.Path}} {{.Main}}{{end}}" // ownedBy says the package directory where lies under the module directory dir and is not a vendored copy. func ownedBy(dir, where string) bool { 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 f2dd57635..1d60be3c5 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 @@ -12,22 +12,24 @@ import ( "git.novox.be/novox/mesh-sdk/go/forgekey" ) -// ADR 0593 §2 (C3): a fixture whose program imports announce, a sibling package of its own repository and a package of its -// own directory reports the first two; a test's import never appears (go list is run without -test), and the standard -// library, a package of another host and the module's own packages are left out. -func TestModuleImportsAreTheMeshPackagesOutsideTheModule(t *testing.T) { +// ADR 0593 §2 (C3), design 47 H2 H-A-539: a fixture whose program imports packages of other Go modules reports the +// module path of each, whatever its host (a mesh module comes from the seat go-package-registry, never told apart by a +// forge prefix): a sibling module of its own repository and an outside module count, each once; the standard library +// (no module), the module's own packages (its main module, or a module under its own directory) and a test's import +// (go list runs without -test) are left out. +func TestModuleImportsAreTheModulesItsBuildResolved(t *testing.T) { dir := "/w/ws1/tree/modules/demo" var line string run := func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran { line = name + " " + strings.Join(args, " ") return forgekey.Ran{Stdout: strings.Join([]string{ "fmt /usr/lib/go/src/fmt", - "git.novox.be/novox/mesh-sdk/go/announce /cache/mesh-sdk@v1/go/announce", - "git.novox.be/novox/mesh-sdk/go/announce /cache/mesh-sdk@v1/go/announce", - "git.novox.be/novox/mesh-catalog/modules/sibling/lib /w/ws1/tree/modules/sibling/lib", - "git.novox.be/novox/mesh-catalog/modules/demo/internal/own " + dir + "/internal/own", - "git.novox.be/novox/mesh-sdk/go/vendored " + dir + "/vendor/git.novox.be/novox/mesh-sdk/go/vendored", - "github.com/nats-io/nats.go /cache/nats.go", + "git.novox.be/novox/mesh-sdk/go/announce /cache/mesh-sdk@v1/go/announce git.novox.be/novox/mesh-sdk/go false", + "git.novox.be/novox/mesh-sdk/go/busstate /cache/mesh-sdk@v1/go/busstate git.novox.be/novox/mesh-sdk/go false", + "git.novox.be/novox/mesh-catalog/modules/sibling/lib /w/ws1/tree/modules/sibling/lib git.novox.be/novox/mesh-catalog/modules/sibling/lib false", + "demo/internal/own " + dir + "/internal/own demo true", + "git.novox.be/novox/mesh-catalog/modules/demo/inner " + dir + "/inner git.novox.be/novox/mesh-catalog/modules/demo/inner false", + "github.com/nats-io/nats.go /cache/nats.go github.com/nats-io/nats.go false", }, "\n")} } m, err := catalogue.Decode([]byte(`{"module":"demo","version":"1","build":{"artifacts":[` + goBundle + `,{"name":"files","kind":"archive","from":"files"}]}}`)) @@ -38,7 +40,7 @@ func TestModuleImportsAreTheMeshPackagesOutsideTheModule(t *testing.T) { if err != nil { t.Fatal(err) } - want := []string{"git.novox.be/novox/mesh-catalog/modules/sibling/lib", "git.novox.be/novox/mesh-sdk/go/announce", "git.novox.be/novox/mesh-sdk/go/vendored"} + want := []string{"git.novox.be/novox/mesh-catalog/modules/sibling/lib", "git.novox.be/novox/mesh-sdk/go", "github.com/nats-io/nats.go"} if !slices.Equal(got.Imports, want) { t.Errorf("imports %v, want %v", got.Imports, want) } @@ -62,7 +64,7 @@ func TestImportsStepReadsTheWorkspace(t *testing.T) { } } -// G2 (design 47 H2 H-A-532 DQ-5, H-A-518): a Go package artifact reports the mesh packages the packages its paths +// G2 (design 47 H2 H-A-532 DQ-5, H-A-518, H-A-539): a Go package artifact reports the modules 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) { @@ -71,10 +73,10 @@ func TestAPackageArtifactReportsItsImportSet(t *testing.T) { 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", + "git.novox.be/novox/mesh-catalog/modules/contracts/go/seats/demo " + dir + "/go/seats/demo git.novox.be/novox/mesh-catalog/modules/contracts/go/seats/demo true", + "git.novox.be/novox/mesh-sdk/go/stdio /cache/mesh-sdk@v1/go/stdio git.novox.be/novox/mesh-sdk/go false", }, "\n"), - dir + " ./cmd/demo-tools": "git.novox.be/novox/mesh-sdk/go/announce /cache/mesh-sdk@v1/go/announce", + dir + " ./cmd/demo-tools": "github.com/nats-io/nats.go /cache/nats.go github.com/nats-io/nats.go false", } for _, c := range []struct { name string @@ -82,9 +84,9 @@ func TestAPackageArtifactReportsItsImportSet(t *testing.T) { want []string }{ // G2/AC1: a contracts module whose artifact is of kind package. - {"package", pkg, []string{"git.novox.be/novox/mesh-sdk/go/stdio"}}, + {"package", pkg, []string{"git.novox.be/novox/mesh-sdk/go"}}, // 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"}}, + {"both", goBundle + "," + pkg, []string{"git.novox.be/novox/mesh-sdk/go", "github.com/nats-io/nats.go"}}, } { t.Run(c.name, func(t *testing.T) { run := func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran { diff --git a/modules/mesh-build-go/module.json b/modules/mesh-build-go/module.json index 94820f615..2de4a290b 100644 --- a/modules/mesh-build-go/module.json +++ b/modules/mesh-build-go/module.json @@ -103,13 +103,6 @@ "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 2b081b53a..d8abc5872 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/asks.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/asks.go @@ -33,9 +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(), + // The npm packages its bundles import, read after the build and said as imports-reported (design 47 H2 H-A-523, + // H-A-532 DQ-6, H-A-539). + Imports: byName[ImportsTool], 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) 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 0984cea90..29c03bcdf 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 @@ -4,13 +4,11 @@ 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. @@ -61,13 +59,6 @@ func TestADoneNodeBuildSaysItsImportSetAndAFailedOneNone(t *testing.T) { 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" { 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 dc27d50da..d71fbbe53 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,7 +12,6 @@ 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" ) @@ -116,9 +115,9 @@ 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) + // Design 47 H2 H-A-539: no scope tells a mesh package apart, so the module declares no setting import-scope. + if _, ok := doc.Config["import-scope"]; ok { + t.Errorf("the manifest declares import-scope, which H-A-539 withdrew") } var capacity float64 _ = json.Unmarshal(doc.Config[sdkstep.CapacitySetting].Default, &capacity) 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 2eac23065..c490b24ce 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 @@ -362,23 +362,23 @@ 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) { +// N1/AC1, N1/AC2 (design 47 H2 H-A-523, H-A-532 DQ-6, H-A-539): after a bundle's build, the imports step runs esbuild +// over the staged entrypoint and launcher with a metafile and answers every npm package among its inputs and external +// imports, each by its package name once (the SDK's stdio sub-path is the SDK), with no scope filter: a mesh package is +// told apart by the indexer, never by its scope. Node's own modules (node:module, fs) are no package; a bundle reaching +// only those answers none. +func TestTheImportsStepNamesTheNpmPackagesABundleReaches(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}]}, + {"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},{"path":"node:module","external":true},{"path":"fs","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{}}, + "../../node_modules/nats/lib/index.js":{"imports":[]}}}`, []string{"@novox/mesh-other", "@novox/mesh-sdk", "nats", "pg"}}, + {"none", `{"inputs":{".mesh-build/tools/tools/index.js":{"imports":[{"path":"node:module","external":true},{"path":"path","external":true}]}}}`, []string{}}, } { t.Run(c.name, func(t *testing.T) { m, f, work := builtMachine(t, `,"external":["pg"]`) diff --git a/modules/mesh-build-node/cmd/mesh-build-node/imports.go b/modules/mesh-build-node/cmd/mesh-build-node/imports.go index 50d002b7f..1ea77c83d 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/imports.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/imports.go @@ -1,11 +1,11 @@ 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. +// The imports step (design 47 H2 H-A-523, H-A-532 DQ-6, H-A-539): at a build's end the module says which npm packages +// its TypeScript bundles import, so the indexer can take the fingerprint over the ones a module of the module graph +// publishes 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: every npm package among them, each by its package name once +// (@novox/mesh-sdk, never a sub-path of it), with no scope telling a mesh package apart, and none of Node's own modules. +// A bundle's inputs are what its entrypoints reach, so no test file is among them. import ( "context" @@ -17,35 +17,22 @@ import ( "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). +// ImportsTool reads the 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 " + + Description: "Read the 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, every npm package among them but Node's own " + + "modules, 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", @@ -78,13 +65,12 @@ func (m *Module) imports(ctx context.Context, args map[string]any) (any, error) 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) + names, err := m.bundleImports(ctx, root, a) if err != nil { return nil, err } @@ -100,9 +86,9 @@ func (m *Module) imports(ctx context.Context, args map[string]any) (any, error) 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 +// bundleImports are the npm packages 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) { +func (m *Module) bundleImports(ctx context.Context, root string, a artifact) ([]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 { @@ -133,12 +119,12 @@ func (m *Module) bundleImports(ctx context.Context, root string, a artifact, sco if err != nil { return nil, fmt.Errorf("esbuild wrote no metafile for %s: %w", a.Name, err) } - return inScope(raw, scope) + return packagesOf(raw) } -// 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) { +// packagesOf are the npm packages an esbuild metafile names: each input under a node_modules directory and each external +// import, by its package name, but Node's own modules. +func packagesOf(metafile []byte) ([]string, error) { var meta struct { Inputs map[string]struct { Imports []struct { @@ -152,7 +138,7 @@ func inScope(metafile []byte, scope string) ([]string, error) { } var out []string keep := func(name string) { - if strings.HasPrefix(name, scope+"/") && !slices.Contains(out, name) { + if name != "" && !nodeBuiltin(name) && !slices.Contains(out, name) { out = append(out, name) } } @@ -183,3 +169,19 @@ func packageName(spec string) string { } return parts[0] } + +// nodeBuiltin says whether name is one of Node's own modules (node:, or a name of module.builtinModules), which +// is the runtime's standard library and no npm package. +func nodeBuiltin(name string) bool { + if strings.HasPrefix(name, "node:") { + return true + } + return slices.Contains(nodeBuiltins, name) +} + +// nodeBuiltins are the names of Node 22's module.builtinModules without a sub-path. +var nodeBuiltins = []string{"assert", "async_hooks", "buffer", "child_process", "cluster", "console", "constants", + "crypto", "dgram", "diagnostics_channel", "dns", "domain", "events", "fs", "http", "http2", "https", "inspector", + "module", "net", "os", "path", "perf_hooks", "process", "punycode", "querystring", "readline", "repl", "stream", + "string_decoder", "sys", "timers", "tls", "trace_events", "tty", "url", "util", "v8", "vm", "wasi", + "worker_threads", "zlib"} diff --git a/modules/mesh-build-node/module.json b/modules/mesh-build-node/module.json index 44aa50ee1..b0a1f50f4 100644 --- a/modules/mesh-build-node/module.json +++ b/modules/mesh-build-node/module.json @@ -72,13 +72,6 @@ "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": [