diff --git a/modules/mesh-delivery-sdk/steprun/askrun.go b/modules/mesh-delivery-sdk/steprun/askrun.go index 28e0977a8..ab4d93e39 100644 --- a/modules/mesh-delivery-sdk/steprun/askrun.go +++ b/modules/mesh-delivery-sdk/steprun/askrun.go @@ -66,6 +66,10 @@ type AskSteps struct { // fingerprint main's modules state holds (design 47 H2 H-A-517, H-A-532 DQ-2, DMC-2). A say that fails leaves the // build's answer standing and is said in the journal. Nil says none. SayImports func(nodebuildgo.ImportsReported) error + // ImportPrefix and ImportScope are the forms an import set is judged by, read from the build module's settings + // (design 47 H2 H-A-534): a Go build's imports are Go module paths under ImportPrefix, the forge's module prefix + // (trunk.ForgeModulePrefix when empty); a Node build's are npm names in ImportScope (trunk.DefaultNpmScope when empty). + ImportPrefix, ImportScope string // Outputs are the holder's outputs objects (OutputsStore), where a test step keeps its run's output as `.` (design 47 H2 §791, H-A-412), which the seat's verb output reads (OutputTool). Nil keeps none, said in the journal. Outputs OutputObjects @@ -90,7 +94,7 @@ func RunAsk(ctx context.Context, steps AskSteps, ask sdkstep.StepAsk) (any, erro if ask.Build != nil { job := *ask.Build job.Fetch = withPackages(job.Fetch, ask.Packages) - out, err := steps.build(ctx, job, ask.Version) + out, err := steps.build(ctx, job, ask.Version, ask.Language) var dec *Declining if err != nil && !sdkstep.IsPassing(err) && !errors.As(err, &dec) && ctx.Err() == nil { // A failed build keeps its error text, as the build module bounded it, as the ask's output object, header and @@ -208,7 +212,7 @@ func (s AskSteps) onToolchain(ctx context.Context, want *sdkstep.ToolchainUsed) // build runs a build job; version is the version the ask carries (design 47 G §573), which each artifact's build is given // as its argument version, the build's MESH_VERSION (§370), unless the artifact names its own. -func (s AskSteps) build(ctx context.Context, job sdkstep.BuildJob, version string) (any, error) { +func (s AskSteps) build(ctx context.Context, job sdkstep.BuildJob, version, language string) (any, error) { answer := sdkstep.BuildAnswer{} tc, err := s.onToolchain(ctx, job.Toolchain) if err != nil { @@ -326,7 +330,7 @@ func (s AskSteps) build(ctx context.Context, job sdkstep.BuildJob, version strin if err := json.Unmarshal(r, &read); err != nil { return nil, fmt.Errorf("the imports of %s answered what is not a list: %s", job.Module, r) } - if err := trunk.CheckImports(read.Imports); err != nil { + if err := s.checkImports(language, read.Imports); err != nil { return nil, err } answer.Imports = read.Imports @@ -335,6 +339,15 @@ func (s AskSteps) build(ctx context.Context, job sdkstep.BuildJob, version strin return answer, nil } +// checkImports judges a build's import set by the form of the language its build seat serves (design 47 H2 H-A-534): npm +// names in the mesh's scope for a Node build, Go module paths under the forge's prefix for every other. +func (s AskSteps) checkImports(language string, imports []string) error { + if language == sdkstep.AskNode { + return trunk.CheckNpmImports(imports, s.ImportScope) + } + return trunk.CheckGoImports(imports, s.ImportPrefix) +} + // sayImports says the import set a done build read (design 47 H2 H-A-517, H-A-532 DQ-2): the module, the fingerprint // it was asked for, its commit and the imports, an empty list when its programs import no mesh package. A failed say is // said in the journal and never fails the build (principle 3: loud, not lost). diff --git a/modules/mesh-delivery-sdk/steprun/askrun_test.go b/modules/mesh-delivery-sdk/steprun/askrun_test.go index f31d68d7b..634058b67 100644 --- a/modules/mesh-delivery-sdk/steprun/askrun_test.go +++ b/modules/mesh-delivery-sdk/steprun/askrun_test.go @@ -141,6 +141,18 @@ imports {"module":"modules/m","workspace":"/w/1"}` t.Errorf("a failed say: said %q, journal %q; want it said in the journal", said, journal) } said, sayErr = nil, nil + // Design 47 H2 H-A-534: an import set is judged by its build seat's form: npm names on the Node seat, Go paths on the Go seat. + steps.Imports = func(context.Context, map[string]any) (any, error) { + return map[string]any{"imports": []string{"@novox/mesh-sdk"}}, nil + } + if _, err := RunAsk(context.Background(), steps, a); err == nil || !strings.Contains(err.Error(), "@novox/mesh-sdk") { + t.Errorf("an npm set on the Go seat: %v, want it refused", err) + } + a.Language = sdkstep.AskNode + if out, err := RunAsk(context.Background(), steps, a); err != nil || !slices.Equal(out.(sdkstep.BuildAnswer).Imports, []string{"@novox/mesh-sdk"}) { + t.Errorf("an npm set on the Node seat: %v, %v; want it taken", out, err) + } + said = nil // Design 47 H2 H-A-482 addendum (a): a failed build keeps its error text, as the build module bounded it, as the ask's // output object, which the verb output answers. diff --git a/modules/mesh-delivery-sdk/trunk/packages.go b/modules/mesh-delivery-sdk/trunk/packages.go index 4d5f653b1..53df9f615 100644 --- a/modules/mesh-delivery-sdk/trunk/packages.go +++ b/modules/mesh-delivery-sdk/trunk/packages.go @@ -28,12 +28,52 @@ func PackageRepository(pkg string) (repository, dir string, ok bool) { return "novox/" + name, dir, true } +// DefaultNpmScope is the npm scope of the mesh's npm packages when a build module's setting names none (design 47 H2 +// H-A-534): a TypeScript import set names each mesh npm package by its name in it, such as @novox/mesh-sdk. +const DefaultNpmScope = "@novox" + // CheckImports refuses an import set (the imports of a module's kept inputs, of a build's answer and of a build log entry) // that holds a path that is no mesh package's, or is not sorted and each once. func CheckImports(imports []string) error { + return CheckGoImports(imports, ForgeModulePrefix) +} + +// CheckGoImports refuses a Go import set holding a path that is no Go module path under prefix, the forge's module prefix +// a build module's setting names (design 47 H2 H-A-534), or that is not sorted and each once. +func CheckGoImports(imports []string, prefix string) error { + if prefix == "" { + prefix = ForgeModulePrefix + } + return checkSorted(imports, func(pkg string) error { + rest, found := strings.CutPrefix(pkg, prefix) + if name, _, _ := strings.Cut(rest, "/"); !found || name == "" || strings.ContainsAny(pkg, " \t\n@") { + return fmt.Errorf("trunk: the import %q is no mesh package's path (it starts with %s)", pkg, prefix) + } + return nil + }) +} + +// CheckNpmImports refuses a TypeScript import set holding an entry that is no npm package name in scope (such as +// @novox/mesh-sdk, never a sub-path of it: one package, one digest, H-A-532 DQ-6), or that is not sorted and each once +// (design 47 H2 H-A-534). +func CheckNpmImports(imports []string, scope string) error { + if scope == "" { + scope = DefaultNpmScope + } + return checkSorted(imports, func(pkg string) error { + name, found := strings.CutPrefix(pkg, scope+"/") + if !found || name == "" || strings.ContainsAny(name, "/@ \t\n") { + return fmt.Errorf("trunk: the import %q is no mesh npm package's name (%s/)", pkg, scope) + } + return nil + }) +} + +// checkSorted refuses imports holding an entry one refuses, or not sorted and each once. +func checkSorted(imports []string, one func(string) error) error { for i, pkg := range imports { - if _, _, ok := PackageRepository(pkg); !ok || strings.ContainsAny(pkg, " \t\n@") { - return fmt.Errorf("trunk: the import %q is no mesh package's path (it starts with %s)", pkg, ForgeModulePrefix) + if err := one(pkg); err != nil { + return err } if i > 0 && imports[i-1] >= pkg { return fmt.Errorf("trunk: the imports are not sorted and each once (%s after %s)", pkg, imports[i-1]) diff --git a/modules/mesh-delivery-sdk/trunk/trunk_test.go b/modules/mesh-delivery-sdk/trunk/trunk_test.go index b098f048a..b565fe5f8 100644 --- a/modules/mesh-delivery-sdk/trunk/trunk_test.go +++ b/modules/mesh-delivery-sdk/trunk/trunk_test.go @@ -44,6 +44,26 @@ func TestAnImportSetHoldsMeshPackagesSortedAndEachOnce(t *testing.T) { t.Errorf("%s: %v is taken", name, bad) } } + // Design 47 H2 H-A-534: a Go set is judged under the prefix the build module's setting names, a TypeScript set by npm + // names in the mesh's scope, each package once (no sub-path, H-A-532 DQ-6). + if err := CheckGoImports([]string{"git.example.org/o/r/go"}, "git.example.org/o/"); err != nil { + t.Errorf("a Go set under another prefix is refused: %v", err) + } + if CheckGoImports([]string{a}, "git.example.org/o/") == nil { + t.Error("a Go set outside the prefix set is taken") + } + if err := CheckNpmImports([]string{"@novox/mesh-contracts-node-build-go", "@novox/mesh-sdk"}, ""); err != nil { + t.Errorf("an npm set in the default scope is refused: %v", err) + } + if err := CheckNpmImports([]string{"@acme/sdk"}, "@acme"); err != nil { + t.Errorf("an npm set in another scope is refused: %v", err) + } + for name, bad := range map[string][]string{"a Go path": {a}, "a sub-path": {"@novox/mesh-sdk/stdio"}, "another scope": {"@other/x"}, + "no scope": {"mesh-sdk"}, "unsorted": {"@novox/mesh-sdk", "@novox/a"}, "a version": {"@novox/mesh-sdk@1"}} { + if CheckNpmImports(bad, "") == nil { + t.Errorf("npm %s: %v is taken", name, bad) + } + } } // A repository keys the indexer's taken and the view's reach by its forge name, in lower case, from either spelling (ADR 0556).