Skip to content

Commit

Permalink
Fix package id in shrinkwrap lifecycle step output
Browse files Browse the repository at this point in the history
Currently all logging related to shrinkwrap steps reports 'undefined'
for the package in output and log messages.

This is due to the package associated with the `idealTree` being
recreated in the `savePackageJson()` method which precedes these
steps. For now, just copy forward the `_id` attribute which lifecycle
logging expects, but note that mutating `package` here is surprising.

Fixes npm/npm#20756
  • Loading branch information
bz2 committed Nov 6, 2019
1 parent 9c7161d commit 3c7d80e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/install/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ function savePackageJson (tree, next) {
fs.readFile(saveTarget, 'utf8', iferr(next, function (packagejson) {
const indent = detectIndent(packagejson).indent
const newline = detectNewline(packagejson)
// TODO - mutating tree.package here is surprising, do something else?
try {
tree.package = parseJSON(packagejson)
tree.package = Object.assign(parseJSON(packagejson), { _id: tree.package._id })
} catch (ex) {
return next(ex)
}
Expand Down
5 changes: 5 additions & 0 deletions test/common-tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,8 @@ Environment.prototype.extend = function (env) {
}
return self
}

var reEscape = /[\\[\](){}*?+.^$|]/g
exports.escapeForRe = function (string) {
return string.replace(reEscape, '\\$&')
}
35 changes: 35 additions & 0 deletions test/tap/install-lifecycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var fs = require('graceful-fs')
var path = require('path')
var test = require('tap').test
var common = require('../common-tap.js')
var pkg = common.pkg

test('npm install execution order', function (t) {
const packageJson = {
name: 'life-test',
version: '0.0.1',
description: 'Test for npm install execution order',
scripts: {
install: 'true',
preinstall: 'true',
preshrinkwrap: 'true',
postinstall: 'true',
postshrinkwrap: 'true',
shrinkwrap: 'true'
}
}
fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify(packageJson), 'utf8')
common.npm(['install', '--loglevel=error'], { cwd: pkg }, function (err, code, stdout, stderr) {
if (err) throw err

t.comment(stdout)
t.comment(stderr)

const steps = ['preinstall', 'install', 'postinstall', 'preshrinkwrap', 'shrinkwrap', 'postshrinkwrap']
const expectedLines = steps.map(function (step) {
return '> ' + packageJson.name + '@' + packageJson.version + ' ' + step
})
t.match(stdout, new RegExp(expectedLines.map(common.escapeForRe).join('.*'), 's'))
t.end()
})
})

0 comments on commit 3c7d80e

Please sign in to comment.