Skip to contentSkip to conversation

classes-recipe: npm: Complain immediately if npm-shrinkwrap.json is too old

PATCH New [email protected] 1 participant 1 message Apr 8, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 classes-recipe: npm: Complain immediately if npm-shrinkwrap.json is too old

From: Mike Crowe <[email protected]>

Rather than emitting:

Exception: KeyError: 'packages'

and a stack trace, let's fail immediately if lockfileVersion implies that the npm-shrinkwrap.json file isn't compatible.

The documentation[1] doesn't make it clear which lockfileVersions are guaranteed to contain "packages". I have lockfileVersion 1 files without. Running npm 7.5.2 generates npm-shrinkwrap.json files with lockfileVersion 2 and "packages", so I've set the minimum to be 2.

[1] https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json

---
 meta/classes-recipe/npm.bbclass | 3 +++
 1 file changed, 3 insertions(+)
meta/classes-recipe/npm.bbclass +3 -0
index a73ff29be8..344e8b4bec 100644
@@ -154,6 +154,9 @@ python npm_do_configure() {
         has_shrinkwrap_file = False
 
     if has_shrinkwrap_file:
+       if int(orig_shrinkwrap.get("lockfileVersion", 0)) < 2:
+           bb.fatal("%s: lockfileVersion version 2 or later is required" % orig_shrinkwrap_file)
+
        cached_shrinkwrap = copy.deepcopy(orig_shrinkwrap)
        for package in orig_shrinkwrap["packages"]:
             if package != "":
Signature · 1 line
2.39.5