classes: npm: Handle peer dependencies for npm packages

NPM changed its manner to handle peer dependencies over its versions.
Before NPM 3: NPM installs automatically peer dependencies
between NPM 3 and 7: NPM shows a warning about peer dependencies
After NPM 3: NPM reworked its manner how to handle peer dependencies

The shrinkwrap doesn't have the parameters of the peer dependencies, so we cannot
fetch them. in the same time peer dependencies are not direct dependencies, they should
be installed as run time dependencies.

(From OE-Core rev: a5734148649be93529e5d5172cb47928957a6536)

Signed-off-by: BELOUARGA Mohamed <m.belouarga@technologyandstrategy.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
BELOUARGA Mohamed
2023-05-31 00:27:48 +02:00
committed by Richard Purdie
parent 2a3888069f
commit ee8b906a83

View File

@@ -109,6 +109,7 @@ python npm_do_configure() {
import tempfile
from bb.fetch2.npm import NpmEnvironment
from bb.fetch2.npm import npm_unpack
from bb.fetch2.npm import npm_package
from bb.fetch2.npmsw import foreach_dependencies
from bb.progress import OutOfProgressHandler
from oe.npm_registry import NpmRegistry
@@ -169,6 +170,7 @@ python npm_do_configure() {
if has_shrinkwrap_file:
cached_shrinkwrap = copy.deepcopy(orig_shrinkwrap)
cached_shrinkwrap.pop("dependencies", None)
cached_shrinkwrap["packages"][""].pop("peerDependencies", None)
# Manage the dependencies
progress = OutOfProgressHandler(d, r"^(\d+)/(\d+)$")
@@ -203,6 +205,19 @@ python npm_do_configure() {
if has_shrinkwrap_file:
foreach_dependencies(orig_shrinkwrap, _count_dependency, dev)
foreach_dependencies(orig_shrinkwrap, _cache_dependency, dev)
# Manage Peer Dependencies
if has_shrinkwrap_file:
packages = orig_shrinkwrap.get("packages", {})
peer_deps = packages.get("", {}).get("peerDependencies", {})
package_runtime_dependencies = d.getVar("RDEPENDS:%s" % d.getVar("PN"))
for peer_dep in peer_deps:
peer_dep_yocto_name = npm_package(peer_dep)
if peer_dep_yocto_name not in package_runtime_dependencies:
bb.warn(peer_dep + " is a peer dependencie that is not in RDEPENDS variable. " +
"Please add this peer dependencie to the RDEPENDS variable as %s and generate its recipe with devtool"
% peer_dep_yocto_name)
# Configure the main package
with tempfile.TemporaryDirectory() as tmpdir:
@@ -279,6 +294,9 @@ python npm_do_compile() {
args.append(("target_arch", d.getVar("NPM_ARCH")))
args.append(("build-from-source", "true"))
# Don't install peer dependencies as they should be in RDEPENDS variable
args.append(("legacy-peer-deps", "true"))
# Pack and install the main package
(tarball, _) = npm_pack(env, d.getVar("NPM_PACKAGE"), tmpdir)
cmd = "npm install %s %s" % (shlex.quote(tarball), d.getVar("EXTRA_OENPM"))