N1: Node's own modules are read from the probed toolchain at the imports step

Review r1 finding 4: the imports step held a fixed list of Node 22's
builtin modules (principle 11). It now reads module.builtinModules from the
installed Node at each step, and fails loud when it cannot.

Builds: design 47 H2 H-A-523, H-A-532 (DQ-6)
This commit is contained in:
2026-11-23 13:18:52 +01:00
parent 7b325126da
commit e5be3c464e
4 changed files with 49 additions and 22 deletions
@@ -5,6 +5,7 @@ import (
"bytes"
"compress/gzip"
"context"
"errors"
"io"
"maps"
"os"
@@ -366,7 +367,8 @@ func TestTheBundleCarriesTheAsksVersionInItsPackageJSON(t *testing.T) {
// 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.
// only those answers none. Node's own modules are the probed toolchain's module.builtinModules, read at the step
// (principle 11), here holding sqlite, which no fixed list of Node 22 holds.
func TestTheImportsStepNamesTheNpmPackagesABundleReaches(t *testing.T) {
for _, c := range []struct {
name string
@@ -374,7 +376,7 @@ func TestTheImportsStepNamesTheNpmPackagesABundleReaches(t *testing.T) {
want []string
}{
{"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}]},
".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},{"path":"sqlite","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", "nats", "pg"}},
@@ -382,6 +384,7 @@ func TestTheImportsStepNamesTheNpmPackagesABundleReaches(t *testing.T) {
} {
t.Run(c.name, func(t *testing.T) {
m, f, work := builtMachine(t, `,"external":["pg"]`)
m.Builtins = func(context.Context) ([]string, error) { return []string{"fs", "module", "path", "sqlite"}, nil }
bundled := f.effect
var metaRun []string
f.effect = func(cl call) {
@@ -416,9 +419,15 @@ func TestTheImportsStepNamesTheNpmPackagesABundleReaches(t *testing.T) {
}
}
// N1 (H-A-517): a workspace whose bundle was not built has no import set to read, and the step says so.
// N1 (H-A-517): a workspace whose bundle was not built has no import set to read, and the step says so; a toolchain whose
// own modules cannot be read fails the step loud, never answering them as packages.
func TestTheImportsStepRefusesABundleNotBuilt(t *testing.T) {
m, _, _ := builtMachine(t, ``)
m.Builtins = func(context.Context) ([]string, error) { return nil, errors.New("no node") }
if _, err := m.imports(context.Background(), map[string]any{"workspace": "w1", "module": "modules/m"}); err == nil || !strings.Contains(err.Error(), "Node's own modules") {
t.Errorf("imports with no builtins read: %v", err)
}
m.Builtins = func(context.Context) ([]string, error) { return []string{"fs"}, nil }
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)
}
@@ -65,12 +65,20 @@ 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)
}
probe := m.Builtins
if probe == nil {
probe = ProbeBuiltins
}
builtins, err := probe(ctx)
if err != nil {
return nil, fmt.Errorf("the imports cannot be told from Node's own modules: %w", err)
}
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)
names, err := m.bundleImports(ctx, root, a, builtins)
if err != nil {
return nil, err
}
@@ -88,7 +96,7 @@ func (m *Module) imports(ctx context.Context, args map[string]any) (any, error)
// bundleImports are the npm packages esbuild names as inputs of bundle a, read in the stage its build left
// (build.go: <root>/stage-<artifact>/modules/module), from the same entries and externals the build bundled.
func (m *Module) bundleImports(ctx context.Context, root string, a artifact) ([]string, error) {
func (m *Module) bundleImports(ctx context.Context, root string, a artifact, builtins []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 {
@@ -119,12 +127,12 @@ func (m *Module) bundleImports(ctx context.Context, root string, a artifact) ([]
if err != nil {
return nil, fmt.Errorf("esbuild wrote no metafile for %s: %w", a.Name, err)
}
return packagesOf(raw)
return packagesOf(raw, builtins)
}
// 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) {
func packagesOf(metafile []byte, builtins []string) ([]string, error) {
var meta struct {
Inputs map[string]struct {
Imports []struct {
@@ -138,7 +146,7 @@ func packagesOf(metafile []byte) ([]string, error) {
}
var out []string
keep := func(name string) {
if name != "" && !nodeBuiltin(name) && !slices.Contains(out, name) {
if name != "" && !nodeBuiltin(name, builtins) && !slices.Contains(out, name) {
out = append(out, name)
}
}
@@ -170,18 +178,8 @@ func packageName(spec string) string {
return parts[0]
}
// nodeBuiltin says whether name is one of Node's own modules (node:<name>, 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)
// nodeBuiltin says whether name is one of Node's own modules (node:<name>, or a name of the probed toolchain's
// module.builtinModules), which is the runtime's standard library and no npm package.
func nodeBuiltin(name string, builtins []string) bool {
return strings.HasPrefix(name, "node:") || slices.Contains(builtins, 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"}
@@ -45,6 +45,9 @@ type Module struct {
Reload func() (Recipe, error)
// Probe reads the installed Node version (ProbeNode).
Probe func(ctx context.Context) (string, error)
// Builtins reads the installed Node's own module names, its module.builtinModules (ProbeBuiltins), at each imports
// step; nil reads them with ProbeBuiltins.
Builtins func(ctx context.Context) ([]string, error)
// Toolchain installs the toolchain directory; nil in a module that only guards.
Toolchain *Toolchain
// Work is the work directory, outside the home: a workspace is <Work>/<step id>/tree. Run runs the commands of a
@@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
@@ -22,6 +23,22 @@ func ProbeNode(ctx context.Context) (string, error) {
return parseNodeVersion(string(out))
}
// ProbeBuiltins reads the installed Node's own modules, its module.builtinModules, so the imports step leaves out exactly
// what the probed toolchain holds (principle 11: read, never a fixed list).
func ProbeBuiltins(ctx context.Context) ([]string, error) {
ctx, cancel := context.WithTimeout(ctx, ProbeTimeout)
defer cancel()
out, err := exec.CommandContext(ctx, "node", "-p", "JSON.stringify(require('module').builtinModules)").Output()
if err != nil {
return nil, fmt.Errorf("node's builtinModules cannot be read: %w", err)
}
var names []string
if err := json.Unmarshal(out, &names); err != nil || len(names) == 0 {
return nil, fmt.Errorf("node's builtinModules answered %q", strings.TrimSpace(string(out)))
}
return names, nil
}
func parseNodeVersion(out string) (string, error) {
v := strings.TrimSpace(out)
if len(v) < 2 || v[0] != 'v' || v[1] < '0' || v[1] > '9' || strings.ContainsAny(v, " \n\t") {