Skip to content

Commit 4c2741a

Browse files
Throw error on file patterns without any files matched
1 parent 9552e18 commit 4c2741a

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/cli/cli.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import type { Readable } from "node:stream";
66
import type { CLI, Logger, ParsedArgs } from "../types.js";
77
import { EXIT_ERROR, EXIT_FAIL, EXIT_OK } from "../util/constants.js";
88
import { createLogger } from "../util/createLogger.js";
9+
import { FilePatternError } from "../util/FilePatternError.js";
910
import { getErrorMessage } from "../util/getErrorMessage.js";
1011
import { getOptionsFromConfig } from "../util/getOptionsFromConfig.js";
1112
import { isMissingFileError } from "../util/isMissingFileError.js";
13+
import { normalizeToPosix } from "../util/normalizeToPosix.js";
1214
import { parseArgs } from "../util/parseArgs.js";
1315
import { parseFilePatterns } from "../util/parseFilePatterns.js";
14-
import { normalizeToPosix } from "../util/normalizeToPosix.js";
1516
import { printHelp } from "../util/printHelp.js";
1617
import { printVersion } from "../util/printVersion.js";
1718
import { setExitCode } from "../util/setExitCode.js";
@@ -25,7 +26,13 @@ export async function main(cli: CLI): Promise<void> {
2526
const exitCode = await mainUnsafe({ cli, args, logger });
2627
setExitCode(exitCode);
2728
} catch (error) {
28-
logger.error(getErrorMessage(error));
29+
if (error instanceof FilePatternError) {
30+
for (const message of error.messages) {
31+
logger.error(message);
32+
}
33+
} else {
34+
logger.error(getErrorMessage(error));
35+
}
2936
setExitCode(EXIT_ERROR);
3037
}
3138
}

src/util/FilePatternError.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class FilePatternError extends Error {
2+
name = "FilePatternError";
3+
4+
constructor(public messages: string[]) {
5+
super(
6+
`One or more file pattern errors occurred:\n${messages.join("\n")}`,
7+
);
8+
}
9+
}

src/util/parseFilePatterns.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fastGlob from "fast-glob";
33
import * as path from "node:path";
44
import type { CLI } from "../types.js";
55
import { GLOB_IGNORE_PATTERNS } from "./constants.js";
6+
import { FilePatternError } from "./FilePatternError.js";
67
import { lstatSafe } from "./lstatSafe.js";
78
import { normalizeToPosix } from "./normalizeToPosix.js";
89

@@ -12,6 +13,7 @@ export async function parseFilePatterns(
1213
): Promise<string[]> {
1314
const seen: Set<string> = new Set();
1415
const globFileEndingPattern = getGlobFileEndingsPattern(cli.fileEndings);
16+
const errorMessages: string[] = [];
1517

1618
const globOptions: Options = {
1719
dot: true,
@@ -25,9 +27,10 @@ export async function parseFilePatterns(
2527

2628
if (stat != null) {
2729
if (stat.isSymbolicLink()) {
28-
throw new Error(
29-
`Specified pattern "${pattern}" is a symbolic link.`,
30+
errorMessages.push(
31+
`Specified pattern is a symbolic link: ${pattern}`,
3032
);
33+
continue;
3134
}
3235

3336
if (stat.isFile()) {
@@ -40,6 +43,11 @@ export async function parseFilePatterns(
4043
...globOptions,
4144
cwd: absolutePath,
4245
});
46+
if (files.length === 0) {
47+
errorMessages.push(
48+
`No matching files were found in the directory: ${pattern}`,
49+
);
50+
}
4351
for (const file of files) {
4452
seen.add(path.resolve(absolutePath, file));
4553
}
@@ -49,11 +57,20 @@ export async function parseFilePatterns(
4957

5058
const glob = normalizeToPosix(pattern);
5159
const files = await fastGlob(glob, globOptions);
60+
if (files.length === 0) {
61+
errorMessages.push(
62+
`No files matching the pattern were found: ${pattern}`,
63+
);
64+
}
5265
for (const file of files) {
5366
seen.add(path.resolve(file));
5467
}
5568
}
5669

70+
if (errorMessages.length > 0) {
71+
throw new FilePatternError(errorMessages);
72+
}
73+
5774
return Array.from(seen).sort((a, b) => a.localeCompare(b));
5875
}
5976

0 commit comments

Comments
 (0)