Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(exit): log any un-ended timings #3479

Merged
merged 1 commit into from Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/base-command.js
Expand Up @@ -75,7 +75,6 @@ class BaseCommand {
}

async setWorkspaces (filters) {
// TODO npm guards workspaces/global mode so we should use this.npm.prefix?
const ws = await getWorkspaces(filters, { path: this.npm.localPrefix })
this.workspaces = ws
this.workspaceNames = [...ws.keys()]
Expand Down
2 changes: 1 addition & 1 deletion lib/npm.js
Expand Up @@ -50,7 +50,7 @@ const npm = module.exports = new class extends EventEmitter {
constructor () {
super()
// TODO make this only ever load once (or unload) in tests
require('./utils/perf.js')
this.timers = require('./utils/perf.js').timers
this.started = Date.now()
this.command = null
this.commands = proxyCmds
Expand Down
13 changes: 9 additions & 4 deletions lib/utils/exit-handler.js
Expand Up @@ -32,9 +32,14 @@ process.on('timing', (name, value) => {
})

process.on('exit', code => {
// process.emit is synchronous, so the timeEnd handler will run before the
// unfinished timer check below
process.emit('timeEnd', 'npm')
log.disableProgress()
if (npm.config && npm.config.loaded && npm.config.get('timing')) {
for (const [name, timers] of npm.timers)
log.verbose('unfinished npm timer', name, timers)

if (npm.config.loaded && npm.config.get('timing')) {
try {
const file = path.resolve(npm.config.get('cache'), '_timing.json')
const dir = path.dirname(npm.config.get('cache'))
Expand Down Expand Up @@ -69,7 +74,7 @@ process.on('exit', code => {
}
}
// In timing mode we always write the log file
if (npm.config && npm.config.loaded && npm.config.get('timing') && !wroteLogFile)
if (npm.config.loaded && npm.config.get('timing') && !wroteLogFile)
writeLogFile()
if (wroteLogFile) {
// just a line break
Expand Down Expand Up @@ -113,7 +118,7 @@ const exit = (code, noLog) => {

const exitHandler = (err) => {
log.disableProgress()
if (!npm.config || !npm.config.loaded) {
if (!npm.config.loaded) {
// logging won't work unless we pretend that it's ready
err = err || new Error('Exit prior to config file resolving.')
console.error(err.stack || err.message)
Expand Down Expand Up @@ -180,7 +185,7 @@ const exitHandler = (err) => {
for (const errline of [...msg.summary, ...msg.detail])
log.error(...errline)

if (npm.config && npm.config.get('json')) {
if (npm.config.loaded && npm.config.get('json')) {
const error = {
error: {
code: err.code,
Expand Down
11 changes: 6 additions & 5 deletions lib/utils/perf.js
@@ -1,20 +1,21 @@
const log = require('npmlog')
const timings = new Map()
const timers = new Map()

process.on('time', (name) => {
timings.set(name, Date.now())
timers.set(name, Date.now())
})

process.on('timeEnd', (name) => {
if (timings.has(name)) {
const ms = Date.now() - timings.get(name)
if (timers.has(name)) {
const ms = Date.now() - timers.get(name)
process.emit('timing', name, ms)
log.timing(name, `Completed in ${ms}ms`)
timings.delete(name)
timers.delete(name)
} else
log.silly('timing', "Tried to end timer that doesn't exist:", name)
})

exports.timers = timers
// for tests
/* istanbul ignore next */
exports.reset = () => {
Expand Down
89 changes: 43 additions & 46 deletions test/lib/utils/exit-handler.js
@@ -1,8 +1,10 @@
/* eslint-disable no-extend-native */
/* eslint-disable no-global-assign */
const t = require('tap')
const EventEmitter = require('events')
const writeFileAtomic = require('write-file-atomic')
const t = require('tap')

const { real: mockNpm } = require('../../fixtures/mock-npm')

// NOTE: Although these unit tests may look like the rest on the surface,
// they are in fact very special due to the amount of things hooking directly
Expand All @@ -25,23 +27,14 @@ t.cleanSnapshot = (str) => redactCwd(str)

// internal modules mocks
const cacheFolder = t.testdir({})
const config = {
values: {
cache: cacheFolder,
timing: true,
},
loaded: true,
updateNotification: null,
get (key) {
return this.values[key]
},
}

const npm = {
version: '1.0.0',
config,
shelloutCommands: ['exec', 'run-script'],
}
const { npm } = mockNpm(t)

t.before(async () => {
npm.version = '1.0.0'
await npm.load()
npm.config.set('cache', cacheFolder)
})

const npmlog = {
disableProgress: () => null,
Expand Down Expand Up @@ -190,9 +183,15 @@ t.test('handles unknown error', (t) => {
t.test('npm.config not ready', (t) => {
t.plan(1)

config.loaded = false

const { npm: unloaded } = mockNpm(t)
const _error = console.error

t.teardown(() => {
console.error = _error
exitHandler.setNpm(npm)
})

exitHandler.setNpm(unloaded)
console.error = (msg) => {
t.match(
msg,
Expand All @@ -202,25 +201,20 @@ t.test('npm.config not ready', (t) => {
}

exitHandler()

t.teardown(() => {
console.error = _error
config.loaded = true
})
})

t.test('fail to write logfile', (t) => {
t.plan(1)

t.teardown(() => {
npm.config.set('cache', cacheFolder)
})

const badDir = t.testdir({
_logs: 'is a file',
})

config.values.cache = badDir

t.teardown(() => {
config.values.cache = cacheFolder
})
npm.config.set('cache', badDir)

t.doesNotThrow(
() => exitHandler(err),
Expand All @@ -231,7 +225,11 @@ t.test('fail to write logfile', (t) => {
t.test('console.log output using --json', (t) => {
t.plan(1)

config.values.json = true
npm.config.set('json', true)
t.teardown(() => {
console.error = _error
npm.config.set('json', false)
})

const _error = console.error
console.error = (jsonOutput) => {
Expand All @@ -249,11 +247,6 @@ t.test('console.log output using --json', (t) => {
}

exitHandler(new Error('Error: EBADTHING Something happened'))

t.teardown(() => {
console.error = _error
delete config.values.json
})
})

t.test('throw a non-error obj', (t) => {
Expand Down Expand Up @@ -354,7 +347,12 @@ t.test('on exit handler', (t) => {
t.test('it worked', (t) => {
t.plan(2)

config.values.timing = false
npm.config.set('timing', false)

t.teardown(() => {
process.exit = _exit
npm.config.set('timing', true)
})

const _exit = process.exit
process.exit = (code) => {
Expand All @@ -370,11 +368,6 @@ t.test('it worked', (t) => {
process.emit('exit', 0)
}

t.teardown(() => {
process.exit = _exit
config.values.timing = true
})

exitHandler()
})

Expand Down Expand Up @@ -466,6 +459,14 @@ t.test('exit handler called twice', (t) => {
t.test('defaults to log error msg if stack is missing', (t) => {
t.plan(1)

const { npm: unloaded } = mockNpm(t)
const _error = console.error

t.teardown(() => {
console.error = _error
exitHandler.setNpm(npm)
})

const noStackErr = Object.assign(
new Error('Error with no stack'),
{
Expand All @@ -475,15 +476,11 @@ t.test('defaults to log error msg if stack is missing', (t) => {
)
delete noStackErr.stack

npm.config.loaded = false

const _error = console.error
console.error = (msg) => {
console.error = _error
npm.config.loaded = true
t.equal(msg, 'Error with no stack', 'should use error msg')
}

exitHandler.setNpm(unloaded)
exitHandler(noStackErr)
})

Expand Down