Skip to content

Commit 68fa32f

Browse files
committed
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
1 parent 51b12a0 commit 68fa32f

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/npm.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,18 @@ class Npm extends EventEmitter {
241241
await this.time('npm:load:configload', () => this.config.load())
242242

243243
// mkdir this separately since the logs dir can be set to
244-
// a different location. an error here should be surfaced
245-
// right away since it will error in cacache later
244+
// a different location. if this fails, then we don't have
245+
// a cache dir, but we don't want to fail immediately since
246+
// the command might not need a cache dir (like `npm --version`)
246247
await this.time('npm:load:mkdirpcache', () =>
247-
fs.mkdir(this.cache, { recursive: true, owner: 'inherit' }))
248+
fs.mkdir(this.cache, { recursive: true, owner: 'inherit' })
249+
.catch((e) => log.verbose('cache', `could not create cache: ${e}`)))
248250

249251
// its ok if this fails. user might have specified an invalid dir
250252
// which we will tell them about at the end
251253
await this.time('npm:load:mkdirplogs', () =>
252254
fs.mkdir(this.logsDir, { recursive: true, owner: 'inherit' })
253-
.catch((e) => log.warn('logfile', `could not create logs-dir: ${e}`)))
255+
.catch((e) => log.verbose('logfile', `could not create logs-dir: ${e}`)))
254256

255257
// note: this MUST be shorter than the actual argv length, because it
256258
// uses the same memory, so node will truncate it if it's too long.

lib/utils/log-file.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ class LogFiles {
204204
this.#files.push(logStream.path)
205205
return logStream
206206
} catch (e) {
207-
log.warn('logfile', `could not be created: ${e}`)
207+
// If the user has a readonly logdir then we don't want to
208+
// warn this on every command so it should be verbose
209+
log.verbose('logfile', `could not be created: ${e}`)
208210
}
209211
}
210212

0 commit comments

Comments
 (0)