From 11ec231e895300e5b7292ac16685d37d1d5df3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Juli=C3=A1n=20Merelo=20Guerv=C3=B3s?= Date: Wed, 29 Nov 2023 17:03:10 +0100 Subject: [PATCH] fix: skip creation of log directory if `logs-max` is set to 0 (#7033) Closes: https://github.com/npm/cli/issues/7032 --- lib/npm.js | 10 ++++++---- test/lib/utils/exit-handler.js | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/npm.js b/lib/npm.js index 14706629e79c2..3322131d8df44 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -216,11 +216,13 @@ class Npm { fs.mkdir(this.cache, { recursive: true }) .catch((e) => log.verbose('cache', `could not create cache: ${e}`))) - // its ok if this fails. user might have specified an invalid dir + // it's ok if this fails. user might have specified an invalid dir // which we will tell them about at the end - await this.time('npm:load:mkdirplogs', () => - fs.mkdir(this.logsDir, { recursive: true }) - .catch((e) => log.verbose('logfile', `could not create logs-dir: ${e}`))) + if (this.config.get('logs-max') > 0) { + await this.time('npm:load:mkdirplogs', () => + fs.mkdir(this.logsDir, { recursive: true }) + .catch((e) => log.verbose('logfile', `could not create logs-dir: ${e}`))) + } // note: this MUST be shorter than the actual argv length, because it // uses the same memory, so node will truncate it if it's too long. diff --git a/test/lib/utils/exit-handler.js b/test/lib/utils/exit-handler.js index 3eb5840985b8f..b48f96d581775 100644 --- a/test/lib/utils/exit-handler.js +++ b/test/lib/utils/exit-handler.js @@ -344,12 +344,12 @@ t.test('no logs dir', async (t) => { const { exitHandler, logs } = await mockExitHandler(t, { config: { 'logs-max': 0 }, }) - await exitHandler(new Error()) t.match(logs.error.filter(([t]) => t === ''), [ ['', 'Log files were not written due to the config logs-max=0'], ]) + t.match(logs.filter(([_, task]) => task === 'npm.load.mkdirplogs'), []) }) t.test('timers fail to write', async (t) => {