From 68fa32f350da867cfeaf6d37db4c3dc7bf78662f Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 20 Jul 2022 07:26:35 -0700 Subject: [PATCH] fix: don't fail immediately if cache dir is not accessible This also changes all the log messages about not being able to create initial directories and files to `log.verbose` since we know run those commands on init. There are a lot of valid reasons why those might fail, and we don't want to show a warning for them every time. Fixes: #4769 Fixes: #4838 Fixes: #4996 --- lib/npm.js | 10 ++++++---- lib/utils/log-file.js | 4 +++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/npm.js b/lib/npm.js index 2197f11a52c4a..66111cab89a84 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -241,16 +241,18 @@ class Npm extends EventEmitter { await this.time('npm:load:configload', () => this.config.load()) // mkdir this separately since the logs dir can be set to - // a different location. an error here should be surfaced - // right away since it will error in cacache later + // a different location. if this fails, then we don't have + // a cache dir, but we don't want to fail immediately since + // the command might not need a cache dir (like `npm --version`) await this.time('npm:load:mkdirpcache', () => - fs.mkdir(this.cache, { recursive: true, owner: 'inherit' })) + fs.mkdir(this.cache, { recursive: true, owner: 'inherit' }) + .catch((e) => log.verbose('cache', `could not create cache: ${e}`))) // its 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, owner: 'inherit' }) - .catch((e) => log.warn('logfile', `could not create logs-dir: ${e}`))) + .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/lib/utils/log-file.js b/lib/utils/log-file.js index 9cf6513bedf48..d739325f7b7a3 100644 --- a/lib/utils/log-file.js +++ b/lib/utils/log-file.js @@ -204,7 +204,9 @@ class LogFiles { this.#files.push(logStream.path) return logStream } catch (e) { - log.warn('logfile', `could not be created: ${e}`) + // If the user has a readonly logdir then we don't want to + // warn this on every command so it should be verbose + log.verbose('logfile', `could not be created: ${e}`) } }