diff mbox series

[08/10] npm: Check that SRC_URI lists all dependencies of the shrinkwrap file

Message ID 20261120135342.2628228-9-stondo@gmail.com
State New
Headers show
Series npm: list the dependencies in SRC_URI | expand

Commit Message

Stefano Tondo Nov. 20, 2026, 1:53 p.m. UTC
From: Stefano Tondo <stefano.tondo.ext@siemens.com>

When a recipe lists the npm dependencies in SRC_URI, the list can become
different from npm-shrinkwrap.json, for example after an upgrade. Then
npm.bbclass packs a wrong set of dependencies.

If SRC_URI has no npmsw:// entry, compare SRC_URI with the shrinkwrap
file in do_configure. Fail if a dependency is missing, or if an entry
unpacks into node_modules but the shrinkwrap file does not have it. The
error tells the user to run the update task.

Do not check with externalsrc, which removes the dependencies from
SRC_URI. There the external source tree must contain them.

Signed-off-by: Stefano Tondo <stefano.tondo.ext@siemens.com>
---
 meta/classes-recipe/npm.bbclass | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
diff mbox series

Patch

diff --git a/meta/classes-recipe/npm.bbclass b/meta/classes-recipe/npm.bbclass
index 6f3261238d..e0d3d88d2b 100644
--- a/meta/classes-recipe/npm.bbclass
+++ b/meta/classes-recipe/npm.bbclass
@@ -111,6 +111,24 @@  def npm_pack(env, srcdir, workdir, excludes=None):
 
     return (tarball, j)
 
+def npm_check_src_uri(d, shrinkwrap):
+    """Check that SRC_URI has one entry for each dependency of the shrinkwrap file"""
+    import oe.npm
+
+    destdir = os.path.relpath(d.getVar("S"), d.getVar("UNPACKDIR"))
+    dev = bb.utils.to_boolean(d.getVar("NPM_INSTALL_DEV"), False)
+    try:
+        expected = oe.npm.shrinkwrap_src_uri(shrinkwrap, destdir, dev)
+    except ValueError as e:
+        bb.fatal("npm-shrinkwrap.json: %s" % e)
+
+    missing, extra = oe.npm.src_uri_differences(expected, d.getVar("SRC_URI").split())
+    if missing or extra:
+        bb.fatal("SRC_URI does not match npm-shrinkwrap.json (%d missing, %d extra entries). "
+                 "Inherit npm-update-dependencies and run 'bitbake -c update_npm_deps %s'.\n%s"
+                 % (len(missing), len(extra), d.getVar("PN"),
+                    "\n".join(["missing: " + url for url in missing] + ["extra: " + url for url in extra])))
+
 python npm_do_configure() {
     """
     Step one: configure the npm cache and the main npm package
@@ -177,6 +195,12 @@  python npm_do_configure() {
        if int(orig_shrinkwrap.get("lockfileVersion", 0)) < 2:
            bb.fatal("%s: lockfileVersion version 2 or later is required" % orig_shrinkwrap_file)
 
+       # externalsrc removes the dependencies from SRC_URI: the external
+       # source tree must contain them
+       if not d.getVar("EXTERNALSRC") and \
+               not any(url.startswith("npmsw://") for url in d.getVar("SRC_URI").split()):
+           npm_check_src_uri(d, orig_shrinkwrap)
+
        cached_shrinkwrap = copy.deepcopy(orig_shrinkwrap)
        for package in orig_shrinkwrap["packages"]:
             if package != "":