2021-06-24 21:39:48 +00:00
|
|
|
// this is called when an ERESOLVE error is caught in the exit-handler,
|
2020-10-02 17:52:19 -04:00
|
|
|
// 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.
|
2020-10-13 12:42:55 -04:00
|
|
|
const { explainEdge, explainNode, printNode } = require('./explain-dep.js')
|
2020-10-02 17:52:19 -04:00
|
|
|
|
|
|
|
// 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.
|
2023-03-31 06:38:48 -07:00
|
|
|
const explain = (expl, chalk, depth) => {
|
2021-07-29 16:11:38 +00:00
|
|
|
const { edge, dep, current, peerConflict, currentEdge } = expl
|
2020-10-02 17:52:19 -04:00
|
|
|
|
|
|
|
const out = []
|
2021-07-29 16:11:38 +00:00
|
|
|
const whileInstalling = dep && dep.whileInstalling ||
|
|
|
|
current && current.whileInstalling ||
|
|
|
|
edge && edge.from && edge.from.whileInstalling
|
2021-11-18 20:58:02 +00:00
|
|
|
if (whileInstalling) {
|
2023-03-31 06:38:48 -07:00
|
|
|
out.push('While resolving: ' + printNode(whileInstalling, chalk))
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2021-04-01 17:27:18 -04:00
|
|
|
// it "should" be impossible for an ERESOLVE explanation to lack both
|
|
|
|
// current and currentEdge, but better to have a less helpful error
|
|
|
|
// than a crashing failure.
|
2021-11-18 20:58:02 +00:00
|
|
|
if (current) {
|
2023-03-31 06:38:48 -07:00
|
|
|
out.push('Found: ' + explainNode(current, depth, chalk))
|
2021-11-18 20:58:02 +00:00
|
|
|
} else if (peerConflict && peerConflict.current) {
|
2023-03-31 06:38:48 -07:00
|
|
|
out.push('Found: ' + explainNode(peerConflict.current, depth, chalk))
|
2021-11-18 20:58:02 +00:00
|
|
|
} else if (currentEdge) {
|
2023-03-31 06:38:48 -07:00
|
|
|
out.push('Found: ' + explainEdge(currentEdge, depth, chalk))
|
2021-11-18 20:58:02 +00:00
|
|
|
} else /* istanbul ignore else - should always have one */ if (edge) {
|
2023-03-31 06:38:48 -07:00
|
|
|
out.push('Found: ' + explainEdge(edge, depth, chalk))
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-04-01 17:27:18 -04:00
|
|
|
|
2020-10-13 12:42:55 -04:00
|
|
|
out.push('\nCould not resolve dependency:\n' +
|
2023-03-31 06:38:48 -07:00
|
|
|
explainEdge(edge, depth, chalk))
|
2020-10-02 17:52:19 -04:00
|
|
|
|
|
|
|
if (peerConflict) {
|
|
|
|
const heading = '\nConflicting peer dependency:'
|
2023-03-31 06:38:48 -07:00
|
|
|
const pc = explainNode(peerConflict.peer, depth, chalk)
|
2020-10-02 17:52:19 -04:00
|
|
|
out.push(heading + ' ' + pc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.join('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate a full verbose report and tell the user how to fix it
|
2023-06-08 05:24:49 -07:00
|
|
|
const report = (expl, chalk, noColorChalk) => {
|
2022-12-06 22:18:33 -05:00
|
|
|
const flags = [
|
|
|
|
expl.strictPeerDeps ? '--no-strict-peer-deps' : '',
|
|
|
|
'--force',
|
|
|
|
'--legacy-peer-deps',
|
|
|
|
].filter(Boolean)
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
const or = (arr) => arr.length <= 2
|
|
|
|
? arr.join(' or ') :
|
|
|
|
arr.map((v, i, l) => i + 1 === l.length ? `or ${v}` : v).join(', ')
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
const fix = `Fix the upstream dependency conflict, or retry
|
|
|
|
this command with ${or(flags)}
|
|
|
|
to accept an incorrect (and potentially broken) dependency resolution.`
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
return {
|
2023-03-31 06:38:48 -07:00
|
|
|
explanation: `${explain(expl, chalk, 4)}\n\n${fix}`,
|
2023-06-08 05:24:49 -07:00
|
|
|
file: `# npm resolution error report\n\n${explain(expl, noColorChalk, Infinity)}\n\n${fix}`,
|
2022-12-06 22:18:33 -05:00
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
explain,
|
2020-11-01 07:54:36 +01:00
|
|
|
report,
|
2020-10-02 17:52:19 -04:00
|
|
|
}
|