Skip to content

Commit

Permalink
Fix regression in lockfile repair for sub-deps
Browse files Browse the repository at this point in the history
This fixes a regression where lockfiles with missing sub-dependencies
are no longer repaired. The regression was introduced in this commit:
1fafb51 which in turn was bringing back
the change from this commit 24acc9f.

PR-URL: #265
Credit: @feelepxyz
Close: #265
Reviewed-by: @mikemimik
  • Loading branch information
feelepxyz authored and Michael Perrotte committed Oct 29, 2019
1 parent 6508e83 commit b6588a8
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/install/inflate-shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function isGit (sw) {
}

function makeFakeChild (name, topPath, tree, sw, requested) {
const isDirectory = requested.type === 'directory'
const from = sw.from || requested.raw
const pkg = {
name: name,
Expand All @@ -167,16 +168,16 @@ function makeFakeChild (name, topPath, tree, sw, requested) {
}
const child = createChild({
package: pkg,
loaded: true,
loaded: isDirectory,
parent: tree,
children: [],
fromShrinkwrap: requested,
fakeChild: sw,
fromBundle: sw.bundled ? tree.fromBundle || tree : null,
path: childPath(tree.path, pkg),
realpath: requested.type === 'directory' ? requested.fetchSpec : childPath(tree.realpath, pkg),
realpath: isDirectory ? requested.fetchSpec : childPath(tree.realpath, pkg),
location: (tree.location === '/' ? '' : tree.location + '/') + pkg.name,
isLink: requested.type === 'directory',
isLink: isDirectory,
isInLink: tree.isLink || tree.isInLink,
swRequires: sw.requires
})
Expand Down
118 changes: 118 additions & 0 deletions test/tap/install-test-cli-with-broken-package-lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
var fs = require('graceful-fs')
var path = require('path')

var mkdirp = require('mkdirp')
var osenv = require('osenv')
var rimraf = require('rimraf')
var test = require('tap').test

var common = require('../common-tap.js')

var pkg = common.pkg

var EXEC_OPTS = { cwd: pkg }

var json = {
name: 'install-test-cli-with-broken-package-lock',
description: 'fixture',
version: '0.0.0',
dependencies: {
optimist: '0.6.0'
}
}

var brokenLockfile = {
name: 'install-test-cli-with-broken-package-lock',
version: '0.0.0',
lockfileVersion: 1,
requires: true,
dependencies: {
optimist: {
version: '0.6.0',
resolved: 'https://registry.npmjs.org/optimist/-/optimist-0.6.0.tgz',
integrity: 'sha1-aUJIJvNAX3nxQub8PZrljU27kgA=',
requires: {
minimist: '~0.0.1',
wordwrap: '~0.0.2'
}
},
wordwrap: {
version: '0.0.3',
resolved: 'https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz',
integrity: 'sha1-o9XabNXAvAAI03I0u68b7WMFkQc='
}
}
}

var expected = {
name: 'install-test-cli-with-broken-package-lock',
version: '0.0.0',
lockfileVersion: 1,
requires: true,
dependencies: {
minimist: {
version: '0.0.10',
resolved: 'https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz',
integrity: 'sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8='
},
optimist: {
version: '0.6.0',
resolved: 'https://registry.npmjs.org/optimist/-/optimist-0.6.0.tgz',
integrity: 'sha1-aUJIJvNAX3nxQub8PZrljU27kgA=',
requires: {
minimist: '~0.0.1',
wordwrap: '~0.0.2'
}
},
wordwrap: {
version: '0.0.3',
resolved: 'https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz',
integrity: 'sha1-o9XabNXAvAAI03I0u68b7WMFkQc='
}
}
}

test('setup', function (t) {
setup()
t.end()
})

test('\'npm install-test\' should repair package-lock.json', function (t) {
common.npm(['install-test'], EXEC_OPTS, function (err, code, stderr, stdout) {
if (err) throw err
t.comment(stdout.trim())
t.comment(stderr.trim())
t.is(code, 0, 'npm install did not raise error code')
var lockfile = JSON.parse(fs.readFileSync(path.join(pkg, 'package-lock.json')))
t.same(
lockfile,
expected,
'package-lock.json should be repaired'
)
t.end()
})
})

test('cleanup', function (t) {
cleanup()
t.end()
})

function setup () {
cleanup()
mkdirp.sync(pkg)
fs.writeFileSync(
path.join(pkg, 'package.json'),
JSON.stringify(json, null, 2)
)
fs.writeFileSync(
path.join(pkg, 'package-lock.json'),
JSON.stringify(brokenLockfile, null, 2)
)
process.chdir(pkg)
}

function cleanup () {
process.chdir(osenv.tmpdir())
rimraf.sync(pkg)
}

0 comments on commit b6588a8

Please sign in to comment.