From d8ab2b1f6689e6dc42688fca31880ce731b839f0 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 2 Jun 2025 22:53:37 +0800 Subject: [PATCH] Fix exit code and result message when parser can not infer (#17505) --- changelog_unreleased/cli/17505-2.md | 18 ++++++++ changelog_unreleased/cli/17505.md | 14 +++++++ src/cli/format.js | 42 ++++++++++++------- .../__snapshots__/infer-parser.js.snap | 16 +++---- .../line-after-filepath-with-errors.js.snap | 3 +- tests/integration/__tests__/infer-parser.js | 16 +++---- tests/integration/__tests__/patterns-dirs.js | 4 +- 7 files changed, 77 insertions(+), 36 deletions(-) create mode 100644 changelog_unreleased/cli/17505-2.md create mode 100644 changelog_unreleased/cli/17505.md diff --git a/changelog_unreleased/cli/17505-2.md b/changelog_unreleased/cli/17505-2.md new file mode 100644 index 000000000..6c0df4b52 --- /dev/null +++ b/changelog_unreleased/cli/17505-2.md @@ -0,0 +1,18 @@ +#### Fix result message for files can not be formatted (#17505 by @fisker) + +```console +touch unknown +prettier --check unknown + +# Prettier stable +Checking formatting... +unknown +[error] No parser could be inferred for file "". +All matched files use Prettier code style! + +# Prettier main +Checking formatting... +unknown +[error] No parser could be inferred for file "". +Error occurred when checking code style in the above file. +``` diff --git a/changelog_unreleased/cli/17505.md b/changelog_unreleased/cli/17505.md new file mode 100644 index 000000000..4f24ba70d --- /dev/null +++ b/changelog_unreleased/cli/17505.md @@ -0,0 +1,14 @@ +#### Fix exitCode when parser can not infer (#17505 by @fisker) + +```console +touch unknown +prettier --check unknown > /dev/null;echo $? + +# Prettier stable +[error] No parser could be inferred for file "". +0 + +# Prettier main +[error] No parser could be inferred for file "". +2 +``` diff --git a/src/cli/format.js b/src/cli/format.js index eedeb4c9b..c0a50e3fb 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -29,25 +29,21 @@ function handleError(context, filename, error, printedFilename, ignoreUnknown) { const errorIsUndefinedParseError = error instanceof errors.UndefinedParserError; - if (printedFilename) { + if (errorIsUndefinedParseError && ignoreUnknown) { // Can't test on CI, `isTTY` is always false, see comments in `formatFiles` - /* c8 ignore next 3 */ - if ((context.argv.write || ignoreUnknown) && errorIsUndefinedParseError) { - printedFilename.clear(); - } else { - // Add newline to split errors from filename line. - process.stdout.write("\n"); - } + printedFilename?.clear(); + + return true; + } + + if (printedFilename) { + // Add newline to split errors from filename line. + process.stdout.write("\n"); } if (errorIsUndefinedParseError) { - if (ignoreUnknown) { - return; - } - if (!context.argv.check && !context.argv.listDifferent) { - process.exitCode = 2; - } context.logger.error(error.message); + process.exitCode = 2; return; } @@ -280,6 +276,7 @@ async function formatFiles(context) { const cwd = process.cwd(); let numberOfUnformattedFilesFound = 0; + let numberOfFilesWithError = 0; const { performanceTestFlag } = context; if (context.argv.check && !performanceTestFlag) { @@ -383,13 +380,18 @@ async function formatFiles(context) { } output = result.formatted; } catch (error) { - handleError( + const errorIsIgnored = handleError( context, fileNameToDisplay, error, printedFilename, ignoreUnknown, ); + + if (!errorIsIgnored) { + numberOfFilesWithError += 1; + } + continue; } @@ -467,7 +469,15 @@ async function formatFiles(context) { // Print check summary based on expected exit code if (context.argv.check) { - if (numberOfUnformattedFilesFound === 0) { + if (numberOfFilesWithError > 0) { + const files = + numberOfFilesWithError === 1 + ? "the above file" + : `${numberOfFilesWithError} files`; + context.logger.log( + `Error occurred when checking code style in ${files}.`, + ); + } else if (numberOfUnformattedFilesFound === 0) { context.logger.log("All matched files use Prettier code style!"); } else { const files = diff --git a/tests/integration/__tests__/__snapshots__/infer-parser.js.snap b/tests/integration/__tests__/__snapshots__/infer-parser.js.snap index d1b0d7683..da01365ce 100644 --- a/tests/integration/__tests__/__snapshots__/infer-parser.js.snap +++ b/tests/integration/__tests__/__snapshots__/infer-parser.js.snap @@ -3,9 +3,9 @@ exports[`--check with unknown path and no parser multiple files 1`] = ` { "stderr": "[error] No parser could be inferred for file "/infer-parser/FOO". -[warn] foo.js -[warn] Code style issues found in the above file. Run Prettier with --write to fix.", - "stdout": "Checking formatting...", +[warn] foo.js", + "stdout": "Checking formatting... +Error occurred when checking code style in the above file.", } `; @@ -13,7 +13,7 @@ exports[`--check with unknown path and no parser specific file 1`] = ` { "stderr": "[error] No parser could be inferred for file "/infer-parser/FOO".", "stdout": "Checking formatting... -All matched files use Prettier code style!", +Error occurred when checking code style in the above file.", } `; @@ -32,9 +32,9 @@ exports[`--list-different with unknown path and no parser specific file 1`] = ` exports[`--write and --check with unknown path and no parser multiple files 1`] = ` { "stderr": "[error] No parser could be inferred for file "/infer-parser/FOO". -[warn] foo.js -[warn] Code style issues fixed in the above file.", - "stdout": "Checking formatting...", +[warn] foo.js", + "stdout": "Checking formatting... +Error occurred when checking code style in the above file.", "write": [ { "content": "foo(); @@ -49,7 +49,7 @@ exports[`--write and --check with unknown path and no parser specific file 1`] { "stderr": "[error] No parser could be inferred for file "/infer-parser/FOO".", "stdout": "Checking formatting... -All matched files use Prettier code style!", +Error occurred when checking code style in the above file.", } `; diff --git a/tests/integration/__tests__/__snapshots__/line-after-filepath-with-errors.js.snap b/tests/integration/__tests__/__snapshots__/line-after-filepath-with-errors.js.snap index f311c79c0..14b4c4f2b 100644 --- a/tests/integration/__tests__/__snapshots__/line-after-filepath-with-errors.js.snap +++ b/tests/integration/__tests__/__snapshots__/line-after-filepath-with-errors.js.snap @@ -58,7 +58,7 @@ invalid-2.js invalid-2.unknown valid-1.js [[Clear text(process.stdout): valid-1.js]] -All matched files use Prettier code style!", +Error occurred when checking code style in 3 files.", "write": [], } `; @@ -77,7 +77,6 @@ exports[`Line breaking after filepath with errors 4`] = ` "stdout": "invalid-1.js invalid-2.js invalid-2.unknown -[[Clear text(process.stdout): invalid-2.unknown]] valid-1.js [[Clear text(process.stdout): valid-1.js]] valid-1.js 0ms (unchanged)", diff --git a/tests/integration/__tests__/infer-parser.js b/tests/integration/__tests__/infer-parser.js index cbac9cfd5..618c10c8b 100644 --- a/tests/integration/__tests__/infer-parser.js +++ b/tests/integration/__tests__/infer-parser.js @@ -88,14 +88,14 @@ describe("unknown path and no parser", () => { describe("--check with unknown path and no parser", () => { describe("specific file", () => { runCli("cli/infer-parser/", ["--check", "FOO"]).test({ - status: 0, + status: 2, write: [], }); }); describe("multiple files", () => { runCli("cli/infer-parser/", ["--check", "*"]).test({ - status: 1, + status: 2, write: [], }); }); @@ -104,7 +104,7 @@ describe("--check with unknown path and no parser", () => { describe("--list-different with unknown path and no parser", () => { describe("specific file", () => { runCli("cli/infer-parser/", ["--list-different", "FOO"]).test({ - status: 0, + status: 2, stdout: "", write: [], }); @@ -112,7 +112,7 @@ describe("--list-different with unknown path and no parser", () => { describe("multiple files", () => { runCli("cli/infer-parser/", ["--list-different", "*"]).test({ - status: 1, + status: 2, stdout: "foo.js", write: [], }); @@ -138,14 +138,14 @@ describe("--write with unknown path and no parser", () => { describe("--write and --check with unknown path and no parser", () => { describe("specific file", () => { runCli("cli/infer-parser/", ["--check", "--write", "FOO"]).test({ - status: 0, + status: 2, write: [], }); }); describe("multiple files", () => { runCli("cli/infer-parser/", ["--check", "--write", "*"]).test({ - status: 0, + status: 2, }); }); }); @@ -153,7 +153,7 @@ describe("--write and --check with unknown path and no parser", () => { describe("--write and --list-different with unknown path and no parser", () => { describe("specific file", () => { runCli("cli/infer-parser/", ["--list-different", "--write", "FOO"]).test({ - status: 0, + status: 2, stdout: "", write: [], }); @@ -161,7 +161,7 @@ describe("--write and --list-different with unknown path and no parser", () => { describe("multiple files", () => { runCli("cli/infer-parser/", ["--list-different", "--write", "*"]).test({ - status: 0, + status: 2, }); }); }); diff --git a/tests/integration/__tests__/patterns-dirs.js b/tests/integration/__tests__/patterns-dirs.js index 95da23000..7cafa6c3e 100644 --- a/tests/integration/__tests__/patterns-dirs.js +++ b/tests/integration/__tests__/patterns-dirs.js @@ -45,7 +45,7 @@ testPatterns("1b - special characters in dir name", ["dir1", "!dir"], { }); testPatterns("1c", ["dir1", "empty"], { status: 1 }); -testPatterns("2", ["dir1", "dir2/**/*"], { status: 1 }); +testPatterns("2", ["dir1", "dir2/**/*"], { status: 2 }); testPatterns("3", ["nonexistent-dir", "dir2/**/*"], { status: 2 }); @@ -122,7 +122,7 @@ describe("plugins `*`", () => { uppercaseRocksPlugin, ]).test({ write: [], - status: 1, + status: 2, }); });