`npm install` (via Cordova) removing everything in node_modules

cli
triaged

(Darryl Pogue) #1

What I Wanted to Do

Cordova uses npm to install its platforms and plugins. Essentially it looks at a cordova section in package.json and then runs npm install --production --no-save {platform}@{version}.

Because the platforms and plugins are often already listed in package.json as dependencies, they often already exist in node_modules, so Cordova can detect that and avoid trying to reinstall them. Except in the case of git references, where we have to ask npm to do an install to make sure that the branch/commit match.

What Happened Instead

In some cases, not reproducible on every machine, the step where we ask npm to install one of the git references causes npm to uninstall everything in the node_modules folder.

Cordova breaks shortly after this point because its modules have been uninstalled while it was running.

Reproduction Steps

Unfortunately, I canโ€™t reproduce this locally on my machine, but it is 100% reproducible on our Mac Mini Jenkins machines. It is also unfortunate that the failure case involves several layers before it gets to npm, but Iโ€™m 99% sure that the bug is at the npm level after turning on silly logging.

My example repo: https://github.com/AyogoHealth/cordova-nightly-failure-test
The steps being run on the buildmachine are:

npm ci
npx seymour

Details

As I said, the flow to the point of failure is a bit convoluted:

  1. We run seymour, which calls cordova.prepare: https://github.com/dpogue/seymour/blob/master/src/seymour.js#L150
  2. cordova.prepare tries to fetch the platform/plugin using cordova-fetch: https://github.com/apache/cordova-lib/blob/master/src/cordova/platform/addHelper.js#L297
  3. cordova-fetch constructs an npm install command and spawns it: https://github.com/apache/cordova-fetch/blob/master/index.js#L64-L68

npm output with silly level logging: npm-cordova.log (340.2 KB)

Platform Info

$ npm --versions
{ npm: '6.4.1',
  ares: '1.14.0',
  cldr: '33.1',
  http_parser: '2.8.0',
  icu: '62.1',
  modules: '64',
  napi: '3',
  nghttp2: '1.34.0',
  node: '10.13.0',
  openssl: '1.1.0i',
  tz: '2018e',
  unicode: '11.0',
  uv: '1.23.2',
  v8: '6.8.275.32-node.36',
  zlib: '1.2.11' }

$ node -p process.platform
darwin

npm install is removing scoped packages