Skip to content

Commit

Permalink
fix(arborist): shrinkwrap throws when trying to read a folder without…
Browse files Browse the repository at this point in the history
… permissions (npm#4258)

* fix(arborist): shrinkwrap throws trying to read a folder without permissions

Fix an issue where shrinkwrap throws an error when trying to read
a folder that it doesn't have permissions to, instead of returning
a correct object with an error
  • Loading branch information
Linkgoron authored Jan 26, 2022
1 parent fabcf43 commit 8c3b143
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions workspaces/arborist/lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,13 @@ class Shrinkwrap {
// all good! hidden lockfile is the newest thing in here.
return data
}).catch(er => {
const rel = relpath(this.path, this.filename)
this.log.verbose('shrinkwrap', `failed to load ${rel}`, er)
/* istanbul ignore else */
if (typeof this.filename === 'string') {
const rel = relpath(this.path, this.filename)
this.log.verbose('shrinkwrap', `failed to load ${rel}`, er)
} else {
this.log.verbose('shrinkwrap', `failed to load ${this.path}`, er)
}
this.loadingError = er
this.loadedFromDisk = false
this.ancientLockfile = false
Expand Down
15 changes: 15 additions & 0 deletions workspaces/arborist/test/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -1597,4 +1597,19 @@ t.test('setting lockfileVersion from the file contents', async t => {
})

t.equal(Shrinkwrap.defaultLockfileVersion, 2, 'default is 2')

t.test('load should return error correctly when it cant access folder',
{ skip: process.platform === 'win32' ? 'skip chmod in windows' : false },
async t => {
const dir = t.testdir({})
try {
fs.chmodSync(dir, '000')
const res = await Shrinkwrap.load({ path: dir })
t.ok(res.loadingError, 'loading error should exist')
t.strictSame(res.loadingError.errno, -13)
t.strictSame(res.loadingError.code, 'EACCES')
} finally {
fs.chmodSync(dir, '666')
}
})
})

0 comments on commit 8c3b143

Please sign in to comment.