Skip to content

Commit 91c5235

Browse files
fiskerevilebottnawi
authored andcommitted
Show invalid config filename in error message (prettier#6865)
1 parent 304acbe commit 91c5235

4 files changed

Lines changed: 32 additions & 5 deletions

File tree

CHANGELOG.unreleased.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,20 @@ async function f() {
14061406
}
14071407
```
14081408

1409+
#### CLI: Display invalid config filename in error message ([#6865] by [@fisker])
1410+
1411+
<!-- prettier-ignore -->
1412+
```bash
1413+
# Input
1414+
$ prettier filename.js --config .invalid-config
1415+
1416+
# Output (Prettier stable)
1417+
Invalid configuration file: ...
1418+
1419+
# Output (Prettier master)
1420+
Invalid configuration file `.invalid-config`: ...
1421+
```
1422+
14091423
[#5682]: https://github.com/prettier/prettier/pull/5682
14101424
[#6657]: https://github.com/prettier/prettier/pull/6657
14111425
[#5910]: https://github.com/prettier/prettier/pull/5910
@@ -1456,6 +1470,7 @@ async function f() {
14561470
[#6796]: https://github.com/prettier/prettier/pull/6796
14571471
[#6848]: https://github.com/prettier/prettier/pull/6848
14581472
[#6856]: https://github.com/prettier/prettier/pull/6856
1473+
[#6865]: https://github.com/prettier/prettier/pull/6865
14591474
[#6863]: https://github.com/prettier/prettier/pull/6863
14601475
[@brainkim]: https://github.com/brainkim
14611476
[@duailibe]: https://github.com/duailibe
@@ -1478,4 +1493,5 @@ async function f() {
14781493
[@andersk]: https://github.com/andersk
14791494
[@lydell]: https://github.com/lydell
14801495
[@aymericbouzy]: https://github.com/aymericbouzy
1496+
[@fisker]: https://github.com/fisker
14811497
[@jridgewell]: https://github.com/jridgewell

src/cli/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ function getOptionsOrDie(context, filePath) {
284284
context.logger.debug("loaded options `" + JSON.stringify(options) + "`");
285285
return options;
286286
} catch (error) {
287-
context.logger.error("Invalid configuration file: " + error.message);
287+
context.logger.error(
288+
`Invalid configuration file \`${filePath}\`: ` + error.message
289+
);
288290
process.exit(2);
289291
}
290292
}

tests_integration/__tests__/__snapshots__/config-invalid.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exports[`show warning with unknown option (stdout) 1`] = `""`;
1919
exports[`show warning with unknown option (write) 1`] = `Array []`;
2020

2121
exports[`throw error for unsupported extension (stderr) 1`] = `
22-
"[error] Invalid configuration file: No sync loader specified for extension \\".unsupported\\"
22+
"[error] Invalid configuration file \`<cwd>/tests_integration/cli/config/invalid\`: No sync loader specified for extension \\".unsupported\\"
2323
"
2424
`;
2525
@@ -28,7 +28,7 @@ exports[`throw error for unsupported extension (stdout) 1`] = `""`;
2828
exports[`throw error for unsupported extension (write) 1`] = `Array []`;
2929
3030
exports[`throw error with invalid config format (stderr) 1`] = `
31-
"[error] Invalid configuration file: Cannot find module '--invalid--' from '<cwd>/tests_integration/cli/config/invalid/file'
31+
"[error] Invalid configuration file \`<cwd>/tests_integration/cli/config/invalid\`: Cannot find module '--invalid--' from '<cwd>/tests_integration/cli/config/invalid/file'
3232
"
3333
`;
3434
@@ -64,7 +64,7 @@ exports[`throw error with invalid config precedence option (configPrecedence) (s
6464
exports[`throw error with invalid config precedence option (configPrecedence) (write) 1`] = `Array []`;
6565
6666
exports[`throw error with invalid config target (directory) (stderr) 1`] = `
67-
"[error] Invalid configuration file: EISDIR: illegal operation on a directory, read
67+
"[error] Invalid configuration file \`<cwd>/tests_integration/cli/config/invalid\`: EISDIR: illegal operation on a directory, read
6868
"
6969
`;
7070
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
"use strict";
22

3+
const replaceCWD = text => {
4+
const cwd = process.cwd();
5+
while (text.indexOf(cwd) !== -1) {
6+
text = text.replace(cwd, "<cwd>");
7+
}
8+
9+
return text;
10+
};
11+
312
module.exports = {
413
test: value =>
514
typeof value === "string" &&
615
(value.indexOf("\\") > -1 || value.indexOf(process.cwd()) > -1),
716
print: (value, serializer) =>
8-
serializer(value.replace(process.cwd(), "<cwd>").replace(/\\/g, "/"))
17+
serializer(replaceCWD(value).replace(/\\/g, "/"))
918
};

0 commit comments

Comments
 (0)