All files / lib/utils explain-eresolve.js

100% Statements 25/25
100% Branches 10/10
100% Functions 3/3
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70              1x 1x 1x 1x           1x 32x   32x 32x 24x   32x 32x     32x 8x 8x 8x     32x       1x 12x   12x 12x       12x                         12x         8x   1x        
// this is called when an ERESOLVE error is caught in the error-handler,
// or when there's a log.warn('eresolve', msg, explanation), to turn it
// into a human-intelligible explanation of what's wrong and how to fix.
//
// TODO: abstract out the explainNode methods into a separate util for
// use by a future `npm explain <path || spec>` command.
 
const npm = require('../npm.js')
const { writeFileSync } = require('fs')
const { resolve } = require('path')
const { explainEdge, explainNode, printNode } = require('./explain-dep.js')
 
// expl is an explanation object that comes from Arborist.  It looks like:
// Depth is how far we want to want to descend into the object making a report.
// The full report (ie, depth=Infinity) is always written to the cache folder
// at ${cache}/eresolve-report.txt along with full json.
const explainEresolve = (expl, color, depth) => {
  const { edge, current, peerConflict } = expl
 
  const out = []
  if (edge.from && edge.from.whileInstalling)
    out.push('While resolving: ' + printNode(edge.from.whileInstalling, color))
 
  out.push('Found: ' + explainNode(current, depth, color))
  out.push('\nCould not resolve dependency:\n' +
    explainEdge(edge, depth, color))
 
  if (peerConflict) {
    const heading = '\nConflicting peer dependency:'
    const pc = explainNode(peerConflict, depth, color)
    out.push(heading + ' ' + pc)
  }
 
  return out.join('\n')
}
 
// generate a full verbose report and tell the user how to fix it
const report = (expl, depth = 4) => {
  const fullReport = resolve(npm.cache, 'eresolve-report.txt')
 
  const orNoStrict = expl.strictPeerDeps ? '--no-strict-peer-deps, ' : ''
  const fix = `Fix the upstream dependency conflict, or retry
this command with ${orNoStrict}--force, or --legacy-peer-deps
to accept an incorrect (and potentially broken) dependency resolution.`
 
  writeFileSync(fullReport, `# npm resolution error report
 
${new Date().toISOString()}
 
${explainEresolve(expl, false, Infinity)}
 
${fix}
 
Raw JSON explanation object:
 
${JSON.stringify(expl, null, 2)}
`, 'utf8')
 
  return explainEresolve(expl, npm.color, depth) +
    `\n\n${fix}\n\nSee ${fullReport} for a full report.`
}
 
// the terser explain method for the warning when using --force
const explain = (expl, depth = 2) => explainEresolve(expl, npm.color, depth)
 
module.exports = {
  explain,
  report,
}