Skip to content

Commit e95da57

Browse files
authored
Fix error when reading inaccessible directories with gitignore: true and suppressErrors: true (#246)
1 parent 55a3c64 commit e95da57

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

ignore.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ const getIsIgnoredPredicate = (files, cwd) => {
5858

5959
const normalizeOptions = (options = {}) => ({
6060
cwd: toPath(options.cwd) || process.cwd(),
61+
suppressErrors: Boolean(options.suppressErrors),
6162
});
6263

6364
export const isIgnoredByIgnoreFiles = async (patterns, options) => {
64-
const {cwd} = normalizeOptions(options);
65+
const {cwd, suppressErrors} = normalizeOptions(options);
6566

66-
const paths = await fastGlob(patterns, {cwd, ...ignoreFilesGlobOptions});
67+
const paths = await fastGlob(patterns, {cwd, suppressErrors, ...ignoreFilesGlobOptions});
6768

6869
const files = await Promise.all(
6970
paths.map(async filePath => ({
@@ -76,9 +77,9 @@ export const isIgnoredByIgnoreFiles = async (patterns, options) => {
7677
};
7778

7879
export const isIgnoredByIgnoreFilesSync = (patterns, options) => {
79-
const {cwd} = normalizeOptions(options);
80+
const {cwd, suppressErrors} = normalizeOptions(options);
8081

81-
const paths = fastGlob.sync(patterns, {cwd, ...ignoreFilesGlobOptions});
82+
const paths = fastGlob.sync(patterns, {cwd, suppressErrors, ...ignoreFilesGlobOptions});
8283

8384
const files = paths.map(filePath => ({
8485
filePath,

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ const getIgnoreFilesPatterns = options => {
6969
const getFilter = async options => {
7070
const ignoreFilesPatterns = getIgnoreFilesPatterns(options);
7171
return createFilterFunction(
72-
ignoreFilesPatterns.length > 0 && await isIgnoredByIgnoreFiles(ignoreFilesPatterns, {cwd: options.cwd}),
72+
ignoreFilesPatterns.length > 0 && await isIgnoredByIgnoreFiles(ignoreFilesPatterns, options),
7373
);
7474
};
7575

7676
const getFilterSync = options => {
7777
const ignoreFilesPatterns = getIgnoreFilesPatterns(options);
7878
return createFilterFunction(
79-
ignoreFilesPatterns.length > 0 && isIgnoredByIgnoreFilesSync(ignoreFilesPatterns, {cwd: options.cwd}),
79+
ignoreFilesPatterns.length > 0 && isIgnoredByIgnoreFilesSync(ignoreFilesPatterns, options),
8080
);
8181
};
8282

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"get-stream": "^6.0.1",
7474
"glob-stream": "^7.0.0",
7575
"rimraf": "^3.0.2",
76+
"tempy": "^3.0.0",
7677
"tsd": "^0.19.1",
7778
"typescript": "^4.5.5",
7879
"xo": "^0.47.0"

tests/globby.js

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'node:path';
44
import util from 'node:util';
55
import test from 'ava';
66
import getStream from 'get-stream';
7+
import {temporaryDirectory} from 'tempy';
78
import {
89
globby,
910
globbySync,
@@ -261,6 +262,19 @@ test('gitignore option and objectMode option', async t => {
261262
t.truthy(result[0].path);
262263
});
263264

265+
test('gitignore option and suppressErrors option', async t => {
266+
const temporary = temporaryDirectory();
267+
fs.mkdirSync(path.join(temporary, 'foo'));
268+
fs.writeFileSync(path.join(temporary, '.gitignore'), 'baz', 'utf8');
269+
fs.writeFileSync(path.join(temporary, 'bar'), '', 'utf8');
270+
fs.writeFileSync(path.join(temporary, 'baz'), '', 'utf8');
271+
// Block access to "foo", which should be silently ignored.
272+
fs.chmodSync(path.join(temporary, 'foo'), 0o000);
273+
const result = await runGlobby(t, '**/*', {cwd: temporary, gitignore: true, suppressErrors: true});
274+
t.is(result.length, 1);
275+
t.truthy(result.includes('bar'));
276+
});
277+
264278
test('respects ignoreFiles string option', async t => {
265279
const actual = await runGlobby(t, '*', {gitignore: false, ignoreFiles: '.gitignore', onlyFiles: false});
266280
t.false(actual.includes('node_modules'));

0 commit comments

Comments
 (0)