diff --git a/lib/node-gyp.js b/lib/node-gyp.js index f60ea51c35..18c786823c 100644 --- a/lib/node-gyp.js +++ b/lib/node-gyp.js @@ -97,6 +97,9 @@ class Gyp extends EventEmitter { parseArgv (argv) { this.opts = nopt(this.configDefs, this.shorthands, argv) this.argv = this.opts.argv.remain.slice() + // the environment is read below and would overwrite this, but an explicit + // --loglevel on the command line should win over npm's inherited loglevel + const cliLoglevel = this.opts.loglevel const commands = this.todo = [] @@ -158,8 +161,9 @@ class Gyp extends EventEmitter { } } - if (this.opts.loglevel) { - log.logger.level = this.opts.loglevel + const loglevel = cliLoglevel || this.opts.loglevel + if (loglevel) { + log.logger.level = loglevel delete this.opts.loglevel } log.resume() diff --git a/test/test-options.js b/test/test-options.js index 7168d2e088..9a7e4dd99c 100644 --- a/test/test-options.js +++ b/test/test-options.js @@ -36,6 +36,10 @@ describe('options', function () { // loglevel does not get added to opts but will change the logger's level. process.env.npm_config_loglevel = 'silly' + // the logger is shared, so reset whatever level earlier tests left behind + // (e.g. from an npm_config_loglevel set by the caller of `npm test`) + log.logger.level = 'info' + const g = gyp() assert.strictEqual(log.logger.level.id, 'info') @@ -99,4 +103,26 @@ describe('options', function () { assert.strictEqual(g.opts['msvs-version'], '2022') }) + + it('--loglevel on the command line wins over npm_config_loglevel', () => { + process.env.npm_config_loglevel = 'error' + log.logger.level = 'info' + + try { + const g = gyp() + g.parseArgv(['node', 'node-gyp', 'rebuild', '--loglevel=verbose']) + + assert.strictEqual(log.logger.level.id, 'verbose') + assert.strictEqual(g.opts.loglevel, undefined) + + gyp().parseArgv(['node', 'node-gyp', 'rebuild', '--silly']) + assert.strictEqual(log.logger.level.id, 'silly') + + gyp().parseArgv(['node', 'node-gyp', 'rebuild']) + assert.strictEqual(log.logger.level.id, 'error') + } finally { + delete process.env.npm_config_loglevel + log.logger.level = 'info' + } + }) })