From 85098741254c53210bfe6c49cfc2dfca62626700 Mon Sep 17 00:00:00 2001 From: howyi Date: Thu, 10 Mar 2022 20:19:37 +0900 Subject: [PATCH] glob: skip error when subdirectory have not permission --- .../glob/__tests__/internal-globber.test.ts | 24 +++++++++++++++++++ packages/glob/src/internal-globber.ts | 19 ++++++++++----- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/packages/glob/__tests__/internal-globber.test.ts b/packages/glob/__tests__/internal-globber.test.ts index fe471e57..0bf750b4 100644 --- a/packages/glob/__tests__/internal-globber.test.ts +++ b/packages/glob/__tests__/internal-globber.test.ts @@ -114,6 +114,30 @@ describe('globber', () => { ]) }) + it('skip permission denied directory', async () => { + // Create the following layout: + // + // /file + // /folder-a <- EACCESS + // /folder-a/file + // /folder-b + // /folder-b/file + const root = path.join(getTestTemp(), 'skip-permission-denied-directory') + await fs.mkdir(path.join(root, 'folder-a'), {recursive: true}) + await fs.writeFile(path.join(root, 'folder-a', 'file'), 'test file content') + await fs.chmod(path.join(root, 'folder-a'), '000') + await fs.mkdir(path.join(root, 'folder-b'), {recursive: true}) + await fs.writeFile(path.join(root, 'folder-b', 'file'), 'test file content') + await fs.writeFile(path.join(root, 'file'), 'test file content') + + const itemPaths = await glob(`${root}/**/file`, {}) + await fs.chmod(path.join(root, 'folder-a'), '777') + expect(itemPaths).toEqual([ + path.join(root, 'file'), + path.join(root, 'folder-b', 'file') + ]) + }) + it('does not match file with trailing slash when implicitDescendants=true', async () => { // Create the following layout: // diff --git a/packages/glob/src/internal-globber.ts b/packages/glob/src/internal-globber.ts index 3978d625..74cd1a28 100644 --- a/packages/glob/src/internal-globber.ts +++ b/packages/glob/src/internal-globber.ts @@ -139,12 +139,19 @@ export class DefaultGlobber implements Globber { continue } - // Push the child items in reverse - const childLevel = item.level + 1 - const childItems = (await fs.promises.readdir(item.path)).map( - x => new SearchState(path.join(item.path, x), childLevel) - ) - stack.push(...childItems.reverse()) + try { + // Push the child items in reverse + const childLevel = item.level + 1 + const childItems = (await fs.promises.readdir(item.path)).map( + x => new SearchState(path.join(item.path, x), childLevel) + ) + stack.push(...childItems.reverse()) + } catch (err) { + if (err.code === 'EACCES') { + continue + } + throw err + } } // File else if (match & MatchKind.File) {