From 91c5235a3f083719cd60f3c1005f6be14b488c6c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 8 Nov 2019 20:51:51 +0800 Subject: [PATCH] Show invalid config filename in error message (#6865) --- CHANGELOG.unreleased.md | 16 ++++++++++++++++ src/cli/util.js | 4 +++- .../__snapshots__/config-invalid.js.snap | 6 +++--- tests_integration/path-serializer.js | 11 ++++++++++- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index f3f737aa3..44ff2e728 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -1406,6 +1406,20 @@ async function f() { } ``` +#### CLI: Display invalid config filename in error message ([#6865] by [@fisker]) + + +```bash +# Input +$ prettier filename.js --config .invalid-config + +# Output (Prettier stable) +Invalid configuration file: ... + +# Output (Prettier master) +Invalid configuration file `.invalid-config`: ... +``` + [#5682]: https://github.com/prettier/prettier/pull/5682 [#6657]: https://github.com/prettier/prettier/pull/6657 [#5910]: https://github.com/prettier/prettier/pull/5910 @@ -1456,6 +1470,7 @@ async function f() { [#6796]: https://github.com/prettier/prettier/pull/6796 [#6848]: https://github.com/prettier/prettier/pull/6848 [#6856]: https://github.com/prettier/prettier/pull/6856 +[#6865]: https://github.com/prettier/prettier/pull/6865 [#6863]: https://github.com/prettier/prettier/pull/6863 [@brainkim]: https://github.com/brainkim [@duailibe]: https://github.com/duailibe @@ -1478,4 +1493,5 @@ async function f() { [@andersk]: https://github.com/andersk [@lydell]: https://github.com/lydell [@aymericbouzy]: https://github.com/aymericbouzy +[@fisker]: https://github.com/fisker [@jridgewell]: https://github.com/jridgewell diff --git a/src/cli/util.js b/src/cli/util.js index 1fa3c5656..e419a11ec 100644 --- a/src/cli/util.js +++ b/src/cli/util.js @@ -284,7 +284,9 @@ function getOptionsOrDie(context, filePath) { context.logger.debug("loaded options `" + JSON.stringify(options) + "`"); return options; } catch (error) { - context.logger.error("Invalid configuration file: " + error.message); + context.logger.error( + `Invalid configuration file \`${filePath}\`: ` + error.message + ); process.exit(2); } } diff --git a/tests_integration/__tests__/__snapshots__/config-invalid.js.snap b/tests_integration/__tests__/__snapshots__/config-invalid.js.snap index 334c91fa2..5d43be4f4 100644 --- a/tests_integration/__tests__/__snapshots__/config-invalid.js.snap +++ b/tests_integration/__tests__/__snapshots__/config-invalid.js.snap @@ -19,7 +19,7 @@ exports[`show warning with unknown option (stdout) 1`] = `""`; exports[`show warning with unknown option (write) 1`] = `Array []`; exports[`throw error for unsupported extension (stderr) 1`] = ` -"[error] Invalid configuration file: No sync loader specified for extension \\".unsupported\\" +"[error] Invalid configuration file \`/tests_integration/cli/config/invalid\`: No sync loader specified for extension \\".unsupported\\" " `; @@ -28,7 +28,7 @@ exports[`throw error for unsupported extension (stdout) 1`] = `""`; exports[`throw error for unsupported extension (write) 1`] = `Array []`; exports[`throw error with invalid config format (stderr) 1`] = ` -"[error] Invalid configuration file: Cannot find module '--invalid--' from '/tests_integration/cli/config/invalid/file' +"[error] Invalid configuration file \`/tests_integration/cli/config/invalid\`: Cannot find module '--invalid--' from '/tests_integration/cli/config/invalid/file' " `; @@ -64,7 +64,7 @@ exports[`throw error with invalid config precedence option (configPrecedence) (s exports[`throw error with invalid config precedence option (configPrecedence) (write) 1`] = `Array []`; exports[`throw error with invalid config target (directory) (stderr) 1`] = ` -"[error] Invalid configuration file: EISDIR: illegal operation on a directory, read +"[error] Invalid configuration file \`/tests_integration/cli/config/invalid\`: EISDIR: illegal operation on a directory, read " `; diff --git a/tests_integration/path-serializer.js b/tests_integration/path-serializer.js index 5158a8253..da80071ce 100644 --- a/tests_integration/path-serializer.js +++ b/tests_integration/path-serializer.js @@ -1,9 +1,18 @@ "use strict"; +const replaceCWD = text => { + const cwd = process.cwd(); + while (text.indexOf(cwd) !== -1) { + text = text.replace(cwd, ""); + } + + return text; +}; + module.exports = { test: value => typeof value === "string" && (value.indexOf("\\") > -1 || value.indexOf(process.cwd()) > -1), print: (value, serializer) => - serializer(value.replace(process.cwd(), "").replace(/\\/g, "/")) + serializer(replaceCWD(value).replace(/\\/g, "/")) };