prettier-vscode: Extension doesn't notify user of syntax error in config files

Prettier supports a lot of configuration file names, as noted on the following list, from their docs’ configuration page:

Prettier uses cosmiconfig for configuration file support. This means you can configure prettier via:

  • A .prettierrc file, written in YAML or JSON, with optional extensions: .yaml/.yml/.json/.js.
  • A prettier.config.js file that exports an object.
  • A “prettier” key in your package.json file.

I tried both .prettierrc.js and prettier.config.js. Prettier works as usual, following my configurations, but the extension doesn’t seem to look for either of them, falling back to default settings. It outputs the following message: Failed to resolve config for /home/user/example/file/name.js. Falling back to the default config settings.

Just for context, I’m configuring prettier by javascript to be able to require a template configuration from a npm package.

Does anyone else have had the same issue before? I searched for the error message, but couldn’t find any related issue.

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

Version 3.16.0 will now show an error when an invalid configuration file is detected.

Ok, so that’s the part of the code that emits the error message:

const { config: fileOptions, error } = await resolveConfig(fileName, {
    editorconfig: true,
});

if (error) {
    addToOutput(
        `Failed to resolve config for ${fileName}. Falling back to the default config settings.`
    );
}

And that’s resolveConfig definition:

const bundledPrettier = require('prettier') as Prettier;

/**
 * Resolves the prettierconfig for the given file.
 *
 * @param filePath file's path
 */
async function resolveConfig(
    filePath: string,
    options?: { editorconfig?: boolean }
): Promise<ResolveConfigResult> {
    try {
        const config = await bundledPrettier.resolveConfig(filePath, options);
        return { config };
    } catch (error) {
        return { config: null, error };
    }
}

So I tried running prettier.resolveConfig(myFilePath, { editorconfig: true }) just like Prettier VSCode does (with the same prettier version too), but it actually works and resolves the config as expected. I’ll try to debug the extension and see what actually is the value of error.