[bitbake-devel,v2] bitbake: fetch2/npm: allow to build npm package with no dependency

Submitted by Sandra Tobajas on Aug. 28, 2020, 7:06 p.m. | Patch ID: 175788

Details

Message ID 20200828190635.3393373-1-sandra.tobajas@savoirfairelinux.com
State New
Headers show

Commit Message

Sandra Tobajas Aug. 28, 2020, 7:06 p.m.
In the case of an empty npm-shrinkwrap.json file (with no dependency),
the npmsw fetcher was causing a RecursionError reported in [1].

It also happened if the package has only "dev" dependencies and was built
without --npm-dev option.

Add sanity checks to ensure that the dependency list is not empty and set
the urldata proxy attribute to None if no dependency is found.

Add bb.tests.fetch.NPMTest.test_npmsw_nodeps unit test to ensure that a
npm package without dependency can be built.

[1] https://bugzilla.yoctoproject.org/show_bug.cgi?id=13901.

Signed-off-by: Sandra Tobajas <sandra.tobajas@savoirfairelinux.com>
---
 bitbake/lib/bb/fetch2/npmsw.py | 16 ++++++++++------
 bitbake/lib/bb/tests/fetch.py  | 20 ++++++++++++++++++++
 2 files changed, 30 insertions(+), 6 deletions(-)

Patch hide | download patch | download mbox

diff --git a/bitbake/lib/bb/fetch2/npmsw.py b/bitbake/lib/bb/fetch2/npmsw.py
index 0c3511d8ab..e06719f45b 100644
--- a/bitbake/lib/bb/fetch2/npmsw.py
+++ b/bitbake/lib/bb/fetch2/npmsw.py
@@ -177,17 +177,21 @@  class NpmShrinkWrap(FetchMethod):
         # This fetcher resolves multiple URIs from a shrinkwrap file and then
         # forwards it to a proxy fetcher. The management of the donestamp file,
         # the lockfile and the checksums are forwarded to the proxy fetcher.
-        ud.proxy = Fetch([dep["url"] for dep in ud.deps], data)
+        if ud.deps:
+            ud.proxy = Fetch([dep["url"] for dep in ud.deps], data)
+        else:
+            ud.proxy = None
         ud.needdonestamp = False
 
     @staticmethod
     def _foreach_proxy_method(ud, handle):
         returns = []
-        for proxy_url in ud.proxy.urls:
-            proxy_ud = ud.proxy.ud[proxy_url]
-            proxy_d = ud.proxy.d
-            proxy_ud.setup_localpath(proxy_d)
-            returns.append(handle(proxy_ud.method, proxy_ud, proxy_d))
+        if ud and ud.proxy:
+            for proxy_url in ud.proxy.urls:
+                proxy_ud = ud.proxy.ud[proxy_url]
+                proxy_d = ud.proxy.d
+                proxy_ud.setup_localpath(proxy_d)
+                returns.append(handle(proxy_ud.method, proxy_ud, proxy_d))
         return returns
 
     def verify_donestamp(self, ud, d):
diff --git a/bitbake/lib/bb/tests/fetch.py b/bitbake/lib/bb/tests/fetch.py
index 13561cea9b..6251f1607a 100644
--- a/bitbake/lib/bb/tests/fetch.py
+++ b/bitbake/lib/bb/tests/fetch.py
@@ -2308,6 +2308,26 @@  class NPMTest(FetcherTest):
         self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'node_modules', 'content-type', 'package.json')))
         self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'node_modules', 'content-type', 'node_modules', 'cookie', 'package.json')))
 
+    @skipIfNoNpm()
+    @skipIfNoNetwork()
+    def test_npmsw_nodeps(self):
+        swfile = self.create_shrinkwrap_file({
+            'dependencies': {
+                'content-type': {
+                    'version': '1.0.4',
+                    'resolved': 'https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz',
+                    'integrity': 'sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==',
+                    'dev': True
+                }
+            }
+        })
+        # Fetch with dev disabled
+        fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
+        fetcher.download()
+        self.assertFalse(os.path.exists(os.path.join(self.dldir, 'npm2', 'content-type-1.0.4.tgz')))
+        fetcher.unpack(self.unpackdir)
+        self.assertTrue(os.path.exists(os.path.join(self.sdir, 'npm-shrinkwrap.json')))
+
     @skipIfNoNpm()
     @skipIfNoNetwork()
     def test_npmsw_dev(self):