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)
186 lines
6.6 KiB
Go
186 lines
6.6 KiB
Go
package main
|
|
|
|
// 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"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
|
)
|
|
|
|
// ImportsTool reads the npm packages a module's bundles import (design 47 H2 H-A-532 DQ-6).
|
|
const ImportsTool = "build_node_imports"
|
|
|
|
// ImportsResult is what the imports step answers; a BuildAnswer carries Imports as it is.
|
|
type ImportsResult struct {
|
|
Imports []string `json:"imports"`
|
|
}
|
|
|
|
func (m *Module) importsTool() StepTool {
|
|
return StepTool{
|
|
Name: ImportsTool,
|
|
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",
|
|
"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)
|
|
}
|
|
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, builtins)
|
|
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 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, 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 {
|
|
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 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, builtins []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 name != "" && !nodeBuiltin(name, builtins) && !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]
|
|
}
|
|
|
|
// 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)
|
|
}
|