Skip to content

Commit

Permalink
Integration code for @npmcli/arborist@1.0.0
Browse files Browse the repository at this point in the history
Updates to ERESOLVE explanation code
  • Loading branch information
isaacs committed Oct 13, 2020
1 parent 704b9cd commit ac9fde7
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 3,159 deletions.
6 changes: 3 additions & 3 deletions lib/utils/explain-dep.js
Expand Up @@ -58,7 +58,7 @@ const explainDependents = ({ name, dependents }, depth, color) => {

const max = Math.ceil(depth / 2)
const messages = dependents.slice(0, max)
.map(dep => explainDependency(name, dep, depth, color))
.map(edge => explainEdge(edge, depth, color))

// show just the names of the first 5 deps that overflowed the list
if (dependents.length > max) {
Expand All @@ -82,7 +82,7 @@ const explainDependents = ({ name, dependents }, depth, color) => {
return str.split('\n').join('\n ')
}

const explainDependency = (name, { type, from, spec }, depth, color) => {
const explainEdge = ({ name, type, from, spec }, depth, color) => {
const { bold } = color ? chalk : nocolor
return (type === 'prod' ? '' : `${colorType(type, color)} `) +
`${bold(name)}@"${bold(spec)}" from ` +
Expand All @@ -98,4 +98,4 @@ const explainFrom = (from, depth, color) => {
explainDependents(from, depth - 1, color)
}

module.exports = { explainNode, printNode }
module.exports = { explainNode, printNode, explainEdge }
40 changes: 8 additions & 32 deletions lib/utils/explain-eresolve.js
Expand Up @@ -8,47 +8,23 @@
const npm = require('../npm.js')
const { writeFileSync } = require('fs')
const { resolve } = require('path')
const { explainNode, printNode } = require('./explain-dep.js')
const { explainEdge, explainNode, printNode } = require('./explain-dep.js')

// expl is an explanation object that comes from Arborist. It looks like:
// {
// dep: {
// whileInstalling: {
// explanation of the thing being installed when we hit the conflict
// },
// name,
// version,
// dependents: [
// things depending on this node (ie, reason for inclusion)
// { name, version, dependents }, ...
// ]
// }
// current: {
// explanation of the current node that already was in the tree conflicting
// }
// peerConflict: {
// explanation of the peer dependency that couldn't be added, or null
// }
// fixWithForce: Boolean - can we use --force to push through this?
// type: type of the edge that couldn't be met
// isPeer: true if the edge that couldn't be met is a peer dependency
// }
// 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 { dep, current, peerConflict } = expl
const { edge, current, peerConflict } = expl

const out = []
/* istanbul ignore else - should always have this for ERESOLVEs */
if (dep.whileInstalling) {
out.push('While resolving: ' + printNode(dep.whileInstalling, color))
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 add conflicting dependency: ' +
explainNode(dep, depth, color))
out.push('\nCould not resolve dependency:\n' +
explainEdge(edge, depth, color))

if (peerConflict) {
const heading = '\nConflicting peer dependency:'
Expand All @@ -63,9 +39,9 @@ const explainEresolve = (expl, color, depth) => {
const report = (expl, depth = 4) => {
const fullReport = resolve(npm.cache, 'eresolve-report.txt')

const orForce = expl.fixWithForce ? ' or --force' : ''
const orNoStrict = expl.strictPeerDeps ? '--no-strict-peer-deps, ' : ''
const fix = `Fix the upstream dependency conflict, or retry
this command with --legacy-peer-deps${orForce}
this command with ${orNoStrict}--force, or --legacy-peer-deps
to accept an incorrect (and potentially broken) dependency resolution.`

writeFileSync(fullReport, `# npm resolution error report
Expand Down
6 changes: 3 additions & 3 deletions tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js
Expand Up @@ -77,7 +77,7 @@ exports[`test/lib/utils/explain-dep.js TAP manyDeps > explain color deep 1`] = `
prod-dep@"1.x" from the root project
optional manydep@"1.x" from optdep@1.0.0 optional
node_modules/optdep
optdep [1moptdep[22m@"[1m1.0.0[22m" from the root project
[36moptional[39m [1moptdep[22m@"[1m1.0.0[22m" from the root project
manydep@"1.0.x" from extra-neos@1337.420.69-lol extraneous
node_modules/extra-neos
dev manydep@"*" from deep-dev@2.3.4 dev
Expand Down Expand Up @@ -114,13 +114,13 @@ manydep@1.0.0
exports[`test/lib/utils/explain-dep.js TAP optional > explain color deep 1`] = `
optdep@1.0.0 optional
node_modules/optdep
optdep [1moptdep[22m@"[1m1.0.0[22m" from the root project
[36moptional[39m [1moptdep[22m@"[1m1.0.0[22m" from the root project
`

exports[`test/lib/utils/explain-dep.js TAP optional > explain nocolor shallow 1`] = `
optdep@1.0.0 optional
node_modules/optdep
optdep optdep@"1.0.0" from the root project
optional optdep@"1.0.0" from the root project
`

exports[`test/lib/utils/explain-dep.js TAP optional > print color 1`] = `
Expand Down

0 comments on commit ac9fde7

Please sign in to comment.