Skip to content

Commit

Permalink
Throw original error when loading external config (#7973)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 10, 2020
1 parent f0207e6 commit aeeb2ff
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
10 changes: 2 additions & 8 deletions src/config/resolve-config.js
Expand Up @@ -18,14 +18,8 @@ const getExplorerMemoized = mem(
if (result && result.config) {
if (typeof result.config === "string") {
const dir = path.dirname(result.filepath);
try {
const modulePath = resolve(result.config, { paths: [dir] });
result.config = eval("require")(modulePath);
} catch (error) {
// Original message contains `__filename`, can't pass tests
error.message = `Cannot find module '${result.config}' from '${dir}'`;
throw error;
}
const modulePath = resolve(result.config, { paths: [dir] });
result.config = eval("require")(modulePath);
}

if (typeof result.config !== "object") {
Expand Down
14 changes: 9 additions & 5 deletions tests_integration/__tests__/__snapshots__/config-invalid.js.snap
@@ -1,5 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`resolves external configuration from package.json (stderr) 1`] = `
"[error] Invalid configuration file \`syntax-error.js\`: THIS ERROR MESSAGE MUST COME FROM THIS syntax-error.js FILE
"
`;

exports[`resolves external configuration from package.json (stdout) 1`] = `""`;

exports[`resolves external configuration from package.json (write) 1`] = `Array []`;

exports[`show warning with kebab-case option key (stderr) 1`] = `
"[warn] Ignored unknown option { \\"print-width\\": 3 }. Did you mean printWidth?
"
Expand Down Expand Up @@ -27,11 +36,6 @@ 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 \`<cwd>/tests_integration/cli/config/invalid\`: Cannot find module '--invalid--' from '<cwd>/tests_integration/cli/config/invalid/file'
"
`;
exports[`throw error with invalid config format (stdout) 1`] = `""`;
exports[`throw error with invalid config format (write) 1`] = `Array []`;
Expand Down
11 changes: 11 additions & 0 deletions tests_integration/__tests__/config-invalid.js
Expand Up @@ -16,6 +16,9 @@ describe("throw error for unsupported extension", () => {
describe("throw error with invalid config format", () => {
runPrettier("cli/config/invalid", ["--config", "file/.prettierrc"]).test({
status: "non-zero",
stderr: expect.stringMatching(
/Cannot (?:resolve|find) module '--invalid--' from/
),
});
});

Expand Down Expand Up @@ -49,6 +52,14 @@ describe("throw error with invalid config precedence option (configPrecedence)",
});
});

describe("resolves external configuration from package.json", () => {
runPrettier("cli/config-external-config-syntax-error", [
"syntax-error.js",
]).test({
status: 2,
});
});

// Tests below require --parser to prevent an error (no parser/filepath specified)

describe("show warning with unknown option", () => {
Expand Down
@@ -0,0 +1,3 @@
{
"prettier": "./syntax-error"
}
@@ -0,0 +1,3 @@
"use strict";

throw new SyntaxError("THIS ERROR MESSAGE MUST COME FROM THIS syntax-error.js FILE");

0 comments on commit aeeb2ff

Please sign in to comment.