Merge pull request 'The Node build module installs the dependencies a placed mesh package declares (H-A-491)' (#741) from fix/node-placed-deps into main
This commit was merged in pull request #741.
This commit is contained in:
@@ -230,6 +230,10 @@ func (m *Module) fetchInto(ctx context.Context, req sdkstep.FetchRequest, group
|
||||
if err := m.layBuilds(dir, members); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// What the mesh packages placed declare, as a registry install would (design 47 H, H-A-491 addendum).
|
||||
if err := m.installPlacedDependencies(ctx, root, dir, d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res.Installed = append(res.Installed, d)
|
||||
continue
|
||||
}
|
||||
@@ -256,6 +260,10 @@ func (m *Module) fetchInto(ctx context.Context, req sdkstep.FetchRequest, group
|
||||
if err := m.layBuilds(dir, members); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// What the mesh packages placed declare, as a registry install would (design 47 H, H-A-491 addendum).
|
||||
if err := m.installPlacedDependencies(ctx, root, dir, d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.SDK == nil {
|
||||
res.SDK = map[string]string{}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -154,6 +157,58 @@ func TestAMeshPackageIsNotBeyondTheSdk(t *testing.T) {
|
||||
if err != nil || got.Version != "0.0.0-member" {
|
||||
t.Errorf("%s placed %q (%v), want the member's build", member, got.Version, err)
|
||||
}
|
||||
|
||||
// Design 47 H, H-A-491 addendum: the dependencies a placed mesh package declares are installed beside it, as a
|
||||
// registry install would (the SDK's seal code imports blakejs); its devDependencies, a mesh package and one the
|
||||
// module's node_modules holds already are not asked.
|
||||
files = typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6"}}`)
|
||||
files["modules/m/node_modules/tweetnacl/package.json"] = `{"name":"tweetnacl","version":"1.0.3"}`
|
||||
m, f = fetchMachine(t, files, map[string]string{"blakejs/package.json": `{"name":"blakejs","version":"1.2.1"}`})
|
||||
var asked []string
|
||||
effect := f.effect
|
||||
f.effect = func(c call) {
|
||||
if c.name == "npm" {
|
||||
for i, a := range c.args {
|
||||
if a == "--prefix" {
|
||||
raw, _ := os.ReadFile(filepath.Join(c.args[i+1], "package.json"))
|
||||
asked = append(asked, strings.Join(c.args[:1], " ")+" "+string(raw))
|
||||
}
|
||||
}
|
||||
}
|
||||
effect(c)
|
||||
}
|
||||
fakeRegistryOf(t, m, map[string]map[string][]byte{sdkPackage: {"0.1.15": tgzDeclaring(t, sdkPackage, "0.1.15",
|
||||
`{"blakejs":"1.2.1","tweetnacl":"1.0.3","@novox/mesh-other":"^0.1.0"}`, `{"typescript":"^5","left-out":"1"}`)}})
|
||||
out, err = m.fetch(context.Background(), fetchArgs)
|
||||
if err != nil {
|
||||
t.Fatalf("a fetch placing an SDK that declares dependencies: %v", err)
|
||||
}
|
||||
r = out.(FetchResult)
|
||||
if want := `install {"dependencies":{"blakejs":"1.2.1"},"name":"placed-dependencies","private":true}`; len(asked) != 1 || asked[0] != want {
|
||||
t.Errorf("npm was asked %q, want once %q", asked, want)
|
||||
}
|
||||
nm := filepath.Join(m.Work, r.Workspace, "tree", "modules", "m", "node_modules")
|
||||
if _, err := os.Stat(filepath.Join(nm, "blakejs", "package.json")); err != nil {
|
||||
t.Errorf("the SDK's dependency blakejs is not installed beside it: %v", err)
|
||||
}
|
||||
if v := placedSDK(filepath.Join(m.Work, r.Workspace, "tree", "modules", "m")); v != "0.1.15" {
|
||||
t.Errorf("the SDK placed %q after the install, want 0.1.15", v)
|
||||
}
|
||||
}
|
||||
|
||||
// tgzDeclaring is a packed npm package of name@version whose package.json declares the dependencies and
|
||||
// devDependencies given (JSON objects).
|
||||
func tgzDeclaring(t *testing.T, name, version, deps, devDeps string) []byte {
|
||||
t.Helper()
|
||||
var buf bytes.Buffer
|
||||
zw := gzip.NewWriter(&buf)
|
||||
tw := tar.NewWriter(zw)
|
||||
body := `{"name":"` + name + `","version":"` + version + `","dependencies":` + deps + `,"devDependencies":` + devDeps + `}`
|
||||
tw.WriteHeader(&tar.Header{Name: "package/package.json", Mode: 0o644, Size: int64(len(body)), Typeflag: tar.TypeReg})
|
||||
tw.Write([]byte(body))
|
||||
tw.Close()
|
||||
zw.Close()
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
// 1489/AC2: a module naming lodash with a lock gets npm ci --omit=dev --ignore-scripts (fake runner), its install put
|
||||
|
||||
@@ -841,3 +841,115 @@ func setPackageVersion(dir, version string) (restore func(), err error) {
|
||||
}
|
||||
return func() { _ = os.WriteFile(path, raw, 0o644) }, nil
|
||||
}
|
||||
|
||||
// installPlacedDependencies installs into the module's node_modules the dependencies every mesh package placed there
|
||||
// (@novox/mesh-*: placeSDK, placeMeshPackages, placeGroup, layBuilds) declares, as a registry install of that package
|
||||
// would (design 47 H, H-A-491 addendum): a placed package's tarball is unpacked whole, and its built code imports what
|
||||
// its package.json's `dependencies` name (the SDK's seal code imports blakejs). Its devDependencies are left out, a mesh
|
||||
// package and what is build-time are not installed (ownDependencies), nor one the module's node_modules holds already.
|
||||
// The install is installOwn's: a scratch package.json naming each at the range its package declares (the first placed
|
||||
// package by name decides a name two declare), `npm install` through the registry's config file and the npm cache,
|
||||
// the install's copies of the SDK removed, and each package it put that the module's node_modules lacks moved there.
|
||||
func (m *Module) installPlacedDependencies(ctx context.Context, root, dir, rel string) error {
|
||||
nm := filepath.Join(dir, "node_modules")
|
||||
entries, err := os.ReadDir(filepath.Join(nm, "@novox"))
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wanted := map[string]any{}
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() || !strings.HasPrefix(e.Name(), "mesh-") {
|
||||
continue
|
||||
}
|
||||
names, pkg, err := ownDependencies(filepath.Join(nm, "@novox", e.Name()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("the placed @novox/%s: %w", e.Name(), err)
|
||||
}
|
||||
deps, _ := pkg["dependencies"].(map[string]any)
|
||||
for _, name := range names {
|
||||
if _, ok := wanted[name]; ok {
|
||||
continue
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(nm, filepath.FromSlash(name), "package.json")); err == nil {
|
||||
continue
|
||||
}
|
||||
wanted[name] = deps[name]
|
||||
}
|
||||
}
|
||||
if len(wanted) == 0 {
|
||||
return nil
|
||||
}
|
||||
var names []string
|
||||
for name := range wanted {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
scratch := filepath.Join(root, "npm-placed-"+strings.NewReplacer("/", "-", ".", "root").Replace(rel))
|
||||
if err := os.MkdirAll(scratch, 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(scratch)
|
||||
raw, _ := json.Marshal(map[string]any{"name": "placed-dependencies", "private": true, "dependencies": wanted})
|
||||
if err := os.WriteFile(filepath.Join(scratch, "package.json"), raw, 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
npmrc, err := m.npmrcIn(root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("installing the dependencies of the mesh packages placed in %s: %w", rel, err)
|
||||
}
|
||||
env := append(m.baseEnv(), "npm_config_cache="+m.npmCache())
|
||||
r := m.Run(ctx, env, "npm", "install", "--omit=dev", "--omit=peer", "--ignore-scripts", "--no-bin-links",
|
||||
"--no-audit", "--no-fund", "--prefix", scratch, "--userconfig", npmrc)
|
||||
os.Remove(npmrc)
|
||||
if why := ranFailed(r); why != "" {
|
||||
return steprun.Transport(fmt.Errorf("installing the dependencies of the mesh packages placed in %s (%s): %s", rel, strings.Join(names, ", "), why))
|
||||
}
|
||||
got := filepath.Join(scratch, "node_modules")
|
||||
if err := removeSDKCopies(got); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := moveAbsent(got, nm); err != nil {
|
||||
return err
|
||||
}
|
||||
if m.Say != nil {
|
||||
m.Say("%s: installed %s, which the mesh packages placed there declare (design 47 H, H-A-491)", rel, strings.Join(names, ", "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// moveAbsent moves each package of the node_modules from (a scope's packages one by one) to the node_modules into
|
||||
// that holds none of its name; npm's own files (.bin, .package-lock.json) stay.
|
||||
func moveAbsent(from, into string) error {
|
||||
entries, err := os.ReadDir(from)
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, e := range entries {
|
||||
if strings.HasPrefix(e.Name(), ".") || !e.IsDir() {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(e.Name(), "@") {
|
||||
if err := moveAbsent(filepath.Join(from, e.Name()), filepath.Join(into, e.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
target := filepath.Join(into, e.Name())
|
||||
if _, err := os.Stat(target); err == nil {
|
||||
continue
|
||||
}
|
||||
if err := os.MkdirAll(into, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(filepath.Join(from, e.Name()), target); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user